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 Method Summary collapse
-
#await(timeout = nil) ⇒ undefined
Waits until either the latch reaches zero or the timeout is reached.
-
#count ⇒ Integer
Performs an ordered read of the count for this latch.
-
#countdown ⇒ undefined
Performs an ordered decrement operation for this latch.
- #initialize(initial) ⇒ undefined constructor
Constructor Details
#initialize(initial) ⇒ undefined
7 8 9 10 11 12 |
# File 'lib/contender/countdown_latch.rb', line 7 def initialize(initial) @count = initial @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Method Details
#await(timeout = nil) ⇒ undefined
Waits until either the latch reaches zero or the timeout is reached
38 39 40 41 42 43 |
# File 'lib/contender/countdown_latch.rb', line 38 def await(timeout = nil) @mutex.synchronize do return if @count == 0 @condition.wait @mutex, timeout end end |
#count ⇒ Integer
Performs an ordered read of the count for this latch
16 17 18 19 20 |
# File 'lib/contender/countdown_latch.rb', line 16 def count @mutex.synchronize do @count end end |
#countdown ⇒ undefined
Performs an ordered decrement operation for this latch
26 27 28 29 30 31 |
# File 'lib/contender/countdown_latch.rb', line 26 def countdown @mutex.synchronize do @count = @count.pred if @count > 0 @condition.broadcast end end |