Class: QuackConcurrency::ConditionVariable
- Inherits:
-
Object
- Object
- QuackConcurrency::ConditionVariable
- Defined in:
- lib/quack_concurrency/condition_variable.rb
Overview
duck type for ‘::Thread::ConditionVariable`
Instance Method Summary collapse
-
#any_waiting_threads? ⇒ Boolean
private
Returns if any ‘Threads` are currently waiting.
-
#broadcast ⇒ self
Wakes up all ‘Threads` currently waiting.
-
#initialize ⇒ ConditionVariable
constructor
Creates a new ConditionVariable concurrency tool.
-
#signal ⇒ self
Wakes up the next waiting ‘Thread`, if any exist.
-
#sleep(duration) ⇒ void
private
Sleeps the current ‘Thread`.
-
#wait(mutex = nil, timeout = nil) ⇒ self
Sleeps the current ‘Thread` until #signal or #broadcast wake it.
-
#waiting_threads_count ⇒ Integer
private
Returns the number of ‘Thread`s currently waiting.
Constructor Details
#initialize ⇒ ConditionVariable
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.
16 17 18 |
# File 'lib/quack_concurrency/condition_variable.rb', line 16 def any_waiting_threads? waiting_threads_count >= 1 end |
#broadcast ⇒ self
Wakes up all ‘Threads` currently waiting.
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 |
#signal ⇒ self
Wakes up the next waiting ‘Thread`, if any exist.
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`.
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.
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_count ⇒ Integer
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.
81 82 83 |
# File 'lib/quack_concurrency/condition_variable.rb', line 81 def waiting_threads_count @waiting_threads_sleepers.length end |