Class: Async::Condition
- Inherits:
-
Object
- Object
- Async::Condition
- Defined in:
- lib/async/condition.rb
Overview
A synchronization primative, which allows fibers to wait until a particular condition is triggered.
Instance Method Summary collapse
-
#initialize ⇒ Condition
constructor
A new instance of Condition.
-
#signal(value = nil) ⇒ void
Signal to a given task that it should resume operations.
-
#wait ⇒ Object
Queue up the current fiber and wait on yielding the task.
Constructor Details
#initialize ⇒ Condition
Returns a new instance of Condition.
28 29 30 |
# File 'lib/async/condition.rb', line 28 def initialize @waiting = [] end |
Instance Method Details
#signal(value = nil) ⇒ void
This method returns an undefined value.
Signal to a given task that it should resume operations.
44 45 46 47 48 49 50 51 |
# File 'lib/async/condition.rb', line 44 def signal(value = nil) # TODO: Should we hot-swap @waiting - so that tasks can wait on this condition again? while task = @waiting.pop task.resume(value) end return nil end |
#wait ⇒ Object
Queue up the current fiber and wait on yielding the task.
34 35 36 37 38 |
# File 'lib/async/condition.rb', line 34 def wait @waiting << Fiber.current Task.yield end |