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. Signalling the condition directly resumes the waiting fibers and thus blocks the caller.
Direct Known Subclasses
Defined Under Namespace
Classes: Signal
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Is any fiber waiting on this notification?.
-
#initialize ⇒ Condition
constructor
A new instance of Condition.
-
#resume(value = nil, task: Task.current) ⇒ void
Signal to a given task that it should resume operations.
-
#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
#empty? ⇒ Boolean
Is any fiber waiting on this notification?
42 43 44 |
# File 'lib/async/condition.rb', line 42 def empty? @waiting.empty? end |
#resume(value = nil, task: Task.current) ⇒ void
This method returns an undefined value.
Signal to a given task that it should resume operations.
63 64 65 66 67 68 69 70 71 |
# File 'lib/async/condition.rb', line 63 def resume(value = nil, task: Task.current) return if @waiting.empty? task.reactor << Signal.new(@waiting, value) @waiting = [] return nil end |
#signal(value = nil) ⇒ void
This method returns an undefined value.
Signal to a given task that it should resume operations.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/async/condition.rb', line 50 def signal(value = nil) waiting = @waiting @waiting = [] waiting.each do |fiber| fiber.resume(value) if fiber.alive? end return nil end |