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
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Is any fiber waiting on this notification?.
-
#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.
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?
51 52 53 |
# File 'lib/async/condition.rb', line 51 def empty? @waiting.empty? end |
#signal(value = nil) ⇒ void
This method returns an undefined value.
Signal to a given task that it should resume operations.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/async/condition.rb', line 59 def signal(value = nil) waiting = @waiting @waiting = [] waiting.each do |fiber| fiber.resume(value) if fiber.alive? end return nil end |
#wait ⇒ Object
Queue up the current fiber and wait on yielding the task.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/async/condition.rb', line 36 def wait fiber = Fiber.current @waiting << fiber Task.yield # It would be nice if there was a better construct for this. We only need to invoke #delete if the task was not resumed normally. This can only occur with `raise` and `throw`. But there is no easy way to detect this. # ensure when not return or ensure when raise, throw rescue Exception @waiting.delete(fiber) raise end |