Class: Garcon::Condition
Overview
Condition is a better implementation of standard Ruby ConditionVariable. The biggest difference is the wait return value: Condition#wait returns Condition::Result which make possible to know if waiting thread has been woken up by an another thread (using #signal or #broadcast) or due to timeout.
Every #wait must be guarded by a locked Mutex or a ThreadError will be risen. Although it’s not mandatory, it’s recommended to call also #signal and #broadcast within the same mutex
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#broadcast ⇒ true
Wakes up all waiting threads.
-
#initialize ⇒ Condition
constructor
A new instance of Condition.
-
#signal ⇒ true
Wakes up a waiting thread.
- #wait(mutex, timeout = nil) ⇒ Result
Constructor Details
#initialize ⇒ Condition
Returns a new instance of Condition.
59 60 61 |
# File 'lib/garcon/task/condition.rb', line 59 def initialize @condition = ConditionVariable.new end |
Instance Method Details
#broadcast ⇒ true
Wakes up all waiting threads
94 95 96 97 |
# File 'lib/garcon/task/condition.rb', line 94 def broadcast @condition.broadcast true end |
#signal ⇒ true
Wakes up a waiting thread
86 87 88 89 |
# File 'lib/garcon/task/condition.rb', line 86 def signal @condition.signal true end |
#wait(mutex, timeout = nil) ⇒ Result
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/garcon/task/condition.rb', line 72 def wait(mutex, timeout = nil) start_time = Garcon.monotonic_time @condition.wait(mutex, timeout) if timeout.nil? Result.new(nil) else Result.new(start_time + timeout - Garcon.monotonic_time) end end |