Class: Polyphony::ConditionVariable
- Defined in:
- lib/polyphony/core/sync.rb
Overview
Implements a fiber-aware ConditionVariable
Instance Method Summary collapse
-
#broadcast ⇒ Object
Resumes all waiting fibers.
-
#initialize ⇒ ConditionVariable
constructor
Initializes the condition variable.
-
#signal ⇒ Fiber
Signals the condition variable, causing the first fiber in the waiting queue to be resumed.
-
#wait(mutex, timeout = nil) ⇒ any
Waits for the condition variable to be signalled.
Constructor Details
#initialize ⇒ ConditionVariable
Initializes the condition variable.
196 197 198 |
# File 'lib/polyphony/core/sync.rb', line 196 def initialize @queue = Polyphony::Queue.new end |
Instance Method Details
#broadcast ⇒ Object
Resumes all waiting fibers.
230 231 232 233 234 235 236 |
# File 'lib/polyphony/core/sync.rb', line 230 def broadcast return if @queue.empty? while (fiber = @queue.shift) fiber.schedule end end |
#signal ⇒ Fiber
Signals the condition variable, causing the first fiber in the waiting queue to be resumed.
222 223 224 225 226 227 |
# File 'lib/polyphony/core/sync.rb', line 222 def signal return if @queue.empty? fiber = @queue.shift fiber.schedule end |
#wait(mutex, timeout = nil) ⇒ any
Waits for the condition variable to be signalled.
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/polyphony/core/sync.rb', line 205 def wait(mutex, timeout = nil) mutex.conditional_release @queue << Fiber.current if timeout move_on_after(timeout, with_value: false) { Polyphony.backend_wait_event(true); true } else Polyphony.backend_wait_event(true) true end ensure mutex.conditional_reacquire end |