Class: Async::Condition
- Inherits:
-
Object
- Object
- Async::Condition
- Defined in:
- lib/async/condition.rb
Overview
A synchronization primitive, which allows fibers to wait until a particular condition is (edge) triggered.
Direct Known Subclasses
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Is any fiber waiting on this notification?.
-
#initialize ⇒ Condition
constructor
A new instance of Condition.
-
#signal(value = nil) ⇒ Object
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.
30 31 32 |
# File 'lib/async/condition.rb', line 30 def initialize @waiting = [] end |
Instance Method Details
#empty? ⇒ Boolean
Is any fiber waiting on this notification?
63 64 65 |
# File 'lib/async/condition.rb', line 63 def empty? @waiting.empty? end |
#signal(value = nil) ⇒ Object
Signal to a given task that it should resume operations.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/async/condition.rb', line 69 def signal(value = nil) waiting = @waiting @waiting = [] waiting.each do |fiber| Fiber.scheduler.resume(fiber, value) if fiber.alive? end return nil end |
#wait ⇒ Object
Queue up the current fiber and wait on yielding the task.
52 53 54 55 56 57 58 59 |
# File 'lib/async/condition.rb', line 52 def wait queue = Queue.new(Fiber.current) @waiting << queue Fiber.scheduler.transfer ensure queue.nullify end |