Class: Green::ConditionVariable
- Inherits:
-
Object
- Object
- Green::ConditionVariable
- Defined in:
- lib/green/semaphore.rb
Instance Method Summary collapse
- #_wakeup(mutex, green) ⇒ Object
-
#broadcast ⇒ Object
Wakes up all threads waiting for this lock.
-
#initialize ⇒ ConditionVariable
constructor
A new instance of ConditionVariable.
-
#signal ⇒ Object
Wakes up the first thread in line waiting for this lock.
-
#wait(mutex, timeout = nil) ⇒ Object
Releases the lock held in
mutex
and waits; reacquires the lock on wakeup.
Constructor Details
#initialize ⇒ ConditionVariable
Returns a new instance of ConditionVariable.
124 125 126 |
# File 'lib/green/semaphore.rb', line 124 def initialize @waiters = [] end |
Instance Method Details
#_wakeup(mutex, green) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/green/semaphore.rb', line 144 def _wakeup(mutex, green) if alive = green.alive? Green.hub.callback { mutex._wakeup(green) } end alive end |
#broadcast ⇒ Object
Wakes up all threads waiting for this lock.
166 167 168 169 170 171 172 |
# File 'lib/green/semaphore.rb', line 166 def broadcast @waiters.each do |mutex, green| _wakeup(mutex, green) end @waiters.clear self end |
#signal ⇒ Object
Wakes up the first thread in line waiting for this lock.
156 157 158 159 160 161 |
# File 'lib/green/semaphore.rb', line 156 def signal while (pair = @waiters.shift) break if _wakeup(*pair) end self end |
#wait(mutex, timeout = nil) ⇒ Object
Releases the lock held in mutex
and waits; reacquires the lock on wakeup.
If timeout
is given, this method returns after timeout
seconds passed, even if no other thread doesn’t signal.
134 135 136 137 138 139 140 141 142 |
# File 'lib/green/semaphore.rb', line 134 def wait(mutex, timeout=nil) current = Green.current pair = [mutex, current] @waiters << pair mutex.sleep timeout do @waiters.delete pair end self end |