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
-
#initialize ⇒ Condition
constructor
Create a new 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.
- #waiting? ⇒ Boolean
Constructor Details
#initialize ⇒ Condition
Create a new condition.
15 16 17 |
# File 'lib/async/condition.rb', line 15 def initialize @ready = ::Thread::Queue.new end |
Instance Method Details
#empty? ⇒ Boolean
26 27 28 |
# File 'lib/async/condition.rb', line 26 def empty? @ready.num_waiting.zero? end |
#signal(value = nil) ⇒ Object
Signal to a given task that it should resume operations.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/async/condition.rb', line 37 def signal(value = nil) return if empty? ready = self.exchange ready.num_waiting.times do ready.push(value) end ready.close return nil end |
#wait ⇒ Object
Queue up the current fiber and wait on yielding the task.
21 22 23 |
# File 'lib/async/condition.rb', line 21 def wait @ready.pop end |
#waiting? ⇒ Boolean
31 32 33 |
# File 'lib/async/condition.rb', line 31 def waiting? !self.empty? end |