Class: CountDownLatch
- Inherits:
-
Object
- Object
- CountDownLatch
- Defined in:
- app/count_down_latch.rb
Overview
A latch that allows you to wait for a maximum number of seconds.
Instance Method Summary collapse
- #await(timeout:) ⇒ Object
- #count ⇒ Object
- #count_down ⇒ Object
-
#initialize(count) ⇒ CountDownLatch
constructor
A new instance of CountDownLatch.
Constructor Details
#initialize(count) ⇒ CountDownLatch
Returns a new instance of CountDownLatch.
5 6 7 8 9 |
# File 'app/count_down_latch.rb', line 5 def initialize(count) @count = count @mutex = Mutex.new @resource = ConditionVariable.new end |
Instance Method Details
#await(timeout:) ⇒ Object
25 26 27 28 29 30 |
# File 'app/count_down_latch.rb', line 25 def await(timeout:) @mutex.synchronize do @resource.wait(@mutex, timeout) if @count.positive? raise 'timed out while waiting' unless @count.zero? end end |
#count ⇒ Object
21 22 23 |
# File 'app/count_down_latch.rb', line 21 def count @mutex.synchronize { @count } end |
#count_down ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'app/count_down_latch.rb', line 11 def count_down @mutex.synchronize do if @count.positive? @count -= 1 @resource.signal if @count.zero? end @count end end |