Class: Contender::CountdownLatch
- Inherits:
-
Object
- Object
- Contender::CountdownLatch
- Defined in:
- lib/contender/countdown_latch.rb
Overview
Synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads complete
Instance Attribute Summary collapse
- #count ⇒ Integer readonly
Instance Method Summary collapse
-
#await(timeout = nil) ⇒ Boolean
Waits until either the latch reaches zero or the timeout is reached.
-
#countdown ⇒ undefined
Performs an ordered decrement operation for this latch.
- #initialize(initial) ⇒ undefined constructor
Constructor Details
#initialize(initial) ⇒ undefined
10 11 12 13 14 15 16 17 |
# File 'lib/contender/countdown_latch.rb', line 10 def initialize(initial) raise ArgumentError if initial < 0 @count = initial @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Attribute Details
#count ⇒ Integer (readonly)
6 7 8 |
# File 'lib/contender/countdown_latch.rb', line 6 def count @count end |
Instance Method Details
#await(timeout = nil) ⇒ Boolean
Waits until either the latch reaches zero or the timeout is reached
35 36 37 38 39 40 41 42 |
# File 'lib/contender/countdown_latch.rb', line 35 def await(timeout = nil) @mutex.synchronize do return true if @count == 0 @condition.wait @mutex, timeout @count == 0 end end |
#countdown ⇒ undefined
Performs an ordered decrement operation for this latch
23 24 25 26 27 28 |
# File 'lib/contender/countdown_latch.rb', line 23 def countdown @mutex.synchronize do @count -= 1 if @count > 0 @condition.broadcast if @count == 0 end end |