Class: QuackConcurrency::ConditionVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/quack_concurrency/condition_variable.rb

Overview

Note:

duck type for ‘::Thread::ConditionVariable`

Instance Method Summary collapse

Constructor Details

#initializeConditionVariable

Creates a new QuackConcurrency::ConditionVariable concurrency tool.



8
9
10
11
# File 'lib/quack_concurrency/condition_variable.rb', line 8

def initialize
  @waiting_threads = []
  @mutex = ::Mutex.new
end

Instance Method Details

#any_waiting_threads?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns if any ‘Threads` are currently waiting.

Returns:

  • (Boolean)


16
17
18
# File 'lib/quack_concurrency/condition_variable.rb', line 16

def any_waiting_threads?
  waiting_threads_count >= 1
end

#broadcastself

Wakes up all ‘Threads` currently waiting.

Returns:

  • (self)


22
23
24
25
26
27
# File 'lib/quack_concurrency/condition_variable.rb', line 22

def broadcast
  @mutex.synchronize do
    signal_next until @waiting_threads.empty?
  end
  self
end

#signalself

Wakes up the next waiting ‘Thread`, if any exist.

Returns:

  • (self)


31
32
33
34
35
36
# File 'lib/quack_concurrency/condition_variable.rb', line 31

def signal
  @mutex.synchronize do
    signal_next if @waiting_threads.any?
  end
  self
end

#sleep(duration) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sleeps the current ‘Thread`.

Parameters:

  • duration (nil, Numeric)

    time to sleep in seconds



42
43
44
45
46
47
48
49
# File 'lib/quack_concurrency/condition_variable.rb', line 42

def sleep(duration)
  if duration == nil || duration == Float::INFINITY
    Thread.stop
  else
    Thread.sleep(duration)
  end
  nil
end

#wait(mutex = nil, timeout = nil) ⇒ self

Sleeps the current ‘Thread` until #signal or #broadcast wake it. If a Mutex is given, the Mutex will be unlocked before sleeping and relocked when woken.

Parameters:

  • mutex (nil, Mutex) (defaults to: nil)
  • timeout (nil, Numeric) (defaults to: nil)

    maximum time to wait, specified in seconds

Returns:

  • (self)

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/quack_concurrency/condition_variable.rb', line 57

def wait(mutex = nil, timeout = nil)
  validate_mutex(mutex)
  if timeout != nil && !timeout.is_a?(Numeric)
    raise ArgumentError, "'timeout' argument must be nil or a Numeric"
  end
  @mutex.synchronize { @waiting_threads.push(caller) }
  if mutex
    if mutex.respond_to?(:unlock!)
      mutex.unlock! { sleep(timeout) }
    else
      mutex.unlock
      sleep(timeout)
      mutex.lock
    end
  else
    sleep(timeout)
  end
  @mutex.synchronize { @waiting_threads.delete(caller) }
  self
end

#waiting_threads_countInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the number of ‘Thread`s currently waiting.

Returns:

  • (Integer)


81
82
83
# File 'lib/quack_concurrency/condition_variable.rb', line 81

def waiting_threads_count
  @waiting_threads_sleepers.length
end