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
- #waiting_count ⇒ Object
Constructor Details
#initialize ⇒ Condition
Create a new condition.
16 17 18 |
# File 'lib/async/condition.rb', line 16 def initialize @ready = ::Thread::Queue.new end |
Instance Method Details
#empty? ⇒ Boolean
27 28 29 |
# File 'lib/async/condition.rb', line 27 def empty? @ready.num_waiting.zero? end |
#signal(value = nil) ⇒ Object
Signal to a given task that it should resume operations.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/async/condition.rb', line 43 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.
22 23 24 |
# File 'lib/async/condition.rb', line 22 def wait @ready.pop end |
#waiting? ⇒ Boolean
32 33 34 |
# File 'lib/async/condition.rb', line 32 def waiting? !self.empty? end |
#waiting_count ⇒ Object
37 38 39 |
# File 'lib/async/condition.rb', line 37 def waiting_count @ready.num_waiting end |