Class: Async::Barrier
- Inherits:
-
Object
- Object
- Async::Barrier
- Defined in:
- lib/async/barrier.rb
Overview
A semaphore is used to control access to a common resource in a concurrent system. A useful way to think of a semaphore as used in the real-world systems is as a record of how many units of a particular resource are available, coupled with operations to adjust that record safely (i.e. to avoid race conditions) as units are required or become free, and, if necessary, wait until a unit of the resource becomes available.
Instance Attribute Summary collapse
-
#tasks ⇒ Object
readonly
All tasks which have been invoked into the barrier.
Instance Method Summary collapse
- #async(*arguments, parent: (@parent or Task.current), **options, &block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(parent: nil) ⇒ Barrier
constructor
A new instance of Barrier.
- #size ⇒ Object
-
#wait ⇒ Object
Wait for tasks in FIFO order.
Constructor Details
#initialize(parent: nil) ⇒ Barrier
Returns a new instance of Barrier.
28 29 30 31 32 |
# File 'lib/async/barrier.rb', line 28 def initialize(parent: nil) @tasks = [] @parent = parent end |
Instance Attribute Details
#tasks ⇒ Object (readonly)
All tasks which have been invoked into the barrier.
35 36 37 |
# File 'lib/async/barrier.rb', line 35 def tasks @tasks end |
Instance Method Details
#async(*arguments, parent: (@parent or Task.current), **options, &block) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/async/barrier.rb', line 41 def async(*arguments, parent: (@parent or Task.current), **, &block) task = parent.async(*arguments, **, &block) @tasks << task return task end |
#empty? ⇒ Boolean
49 50 51 |
# File 'lib/async/barrier.rb', line 49 def empty? @tasks.empty? end |
#size ⇒ Object
37 38 39 |
# File 'lib/async/barrier.rb', line 37 def size @tasks.size end |
#wait ⇒ Object
Wait for tasks in FIFO order.
54 55 56 57 58 |
# File 'lib/async/barrier.rb', line 54 def wait while task = @tasks.shift task.wait end end |