Class: QuackConcurrency::ConditionVariable
- Inherits:
-
Object
- Object
- QuackConcurrency::ConditionVariable
- Defined in:
- lib/quack_concurrency/condition_variable.rb,
lib/quack_concurrency/condition_variable/waitable.rb
Overview
ConditionVariable is similar to ::ConditionVariable
.
A a few differences include:
-
#wait supports passing a ReentrantMutex and Mutex
-
methods have been added to get information on waiting threads
Direct Known Subclasses
Defined Under Namespace
Classes: Waitable
Instance Method Summary collapse
-
#any_waiting_threads? ⇒ Boolean
Checks if any threads are waiting on it.
-
#broadcast ⇒ self
Resumes all threads waiting on it.
-
#initialize ⇒ ConditionVariable
constructor
Creates a new ConditionVariable concurrency tool.
-
#next_waitable_to_wake ⇒ Waitable
private
Returns the Waitable representing the next thread to be woken.
-
#signal ⇒ self
Resumes next thread waiting on it if one exists.
-
#wait(mutex, timeout = nil) ⇒ self
Puts this thread to sleep until another thread resumes it.
-
#waitable_woken(waitable) ⇒ void
private
Remove a Waitable whose thread has been woken.
-
#waiting_threads_count ⇒ Integer
Returns the number of threads currently waiting on it.
Constructor Details
#initialize ⇒ ConditionVariable
Creates a new QuackConcurrency::ConditionVariable concurrency tool.
12 13 14 15 16 |
# File 'lib/quack_concurrency/condition_variable.rb', line 12 def initialize @mutex = ::Mutex.new @waitables = [] @waitables_to_resume = [] end |
Instance Method Details
#any_waiting_threads? ⇒ Boolean
Checks if any threads are waiting on it.
20 21 22 |
# File 'lib/quack_concurrency/condition_variable.rb', line 20 def any_waiting_threads? waiting_threads_count >= 1 end |
#broadcast ⇒ self
Resumes all threads waiting on it.
26 27 28 29 30 31 |
# File 'lib/quack_concurrency/condition_variable.rb', line 26 def broadcast @mutex.synchronize do signal_next until @waitables_to_resume.empty? end self end |
#next_waitable_to_wake ⇒ Waitable
37 38 39 |
# File 'lib/quack_concurrency/condition_variable.rb', line 37 def next_waitable_to_wake @mutex.synchronize { @waitables.first } end |
#signal ⇒ self
Resumes next thread waiting on it if one exists.
43 44 45 46 47 48 |
# File 'lib/quack_concurrency/condition_variable.rb', line 43 def signal @mutex.synchronize do signal_next if @waitables_to_resume.any? end self end |
#wait(mutex, timeout = nil) ⇒ self
Will block until resumed
Puts this thread to sleep until another thread resumes it. Threads will be woken in the chronological order that this was called.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/quack_concurrency/condition_variable.rb', line 59 def wait(mutex, timeout = nil) validate_mutex(mutex) validate_timeout(timeout) waitable = waitable_for_current_thread @mutex.synchronize do @waitables.push(waitable) @waitables_to_resume.push(waitable) end waitable.wait(mutex, timeout) self end |
#waitable_woken(waitable) ⇒ 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.
Remove a Waitable whose thread has been woken.
74 75 76 |
# File 'lib/quack_concurrency/condition_variable.rb', line 74 def waitable_woken(waitable) @mutex.synchronize { @waitables.delete(waitable) } end |
#waiting_threads_count ⇒ Integer
Returns the number of threads currently waiting on it.
80 81 82 |
# File 'lib/quack_concurrency/condition_variable.rb', line 80 def waiting_threads_count @waitables.length end |