Class: Tusk::Latch

Inherits:
Object
  • Object
show all
Defined in:
lib/tusk/latch.rb

Instance Method Summary collapse

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

#awaitObject



18
19
20
21
22
# File 'lib/tusk/latch.rb', line 18

def await
  @lock.synchronize do
    @cv.wait_while { @count > 0 }
  end
end

#releaseObject



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