Class: Tusk::Latch
- Inherits:
-
Object
- Object
- Tusk::Latch
- Defined in:
- lib/tusk/latch.rb
Instance Method Summary collapse
- #await ⇒ Object
-
#initialize(count = 1) ⇒ Latch
constructor
A new instance of Latch.
- #release ⇒ Object
Constructor Details
#initialize(count = 1) ⇒ Latch
Returns a new instance of Latch.
5 6 7 8 9 |
# File 'lib/tusk/latch.rb', line 5 def initialize(count = 1) @count = count @lock = Monitor.new @cv = @lock.new_cond end |
Instance Method Details
#await ⇒ Object
18 19 20 21 22 |
# File 'lib/tusk/latch.rb', line 18 def await @lock.synchronize do @cv.wait_while { @count > 0 } end end |
#release ⇒ Object
11 12 13 14 15 16 |
# File 'lib/tusk/latch.rb', line 11 def release @lock.synchronize do @count -= 1 if @count > 0 @cv.broadcast if @count.zero? end end |