Class: ActiveSupport::Concurrency::Latch

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/concurrency/latch.rb

Instance Method Summary collapse

Constructor Details

#initialize(count = 1) ⇒ Latch

Returns a new instance of Latch


7
8
9
10
11
# File 'activesupport/lib/active_support/concurrency/latch.rb', line 7

def initialize(count = 1)
  @count = count
  @lock = Monitor.new
  @cv = @lock.new_cond
end

Instance Method Details

#awaitObject


20
21
22
23
24
# File 'activesupport/lib/active_support/concurrency/latch.rb', line 20

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

#releaseObject


13
14
15
16
17
18
# File 'activesupport/lib/active_support/concurrency/latch.rb', line 13

def release
  @lock.synchronize do
    @count -= 1 if @count > 0
    @cv.broadcast if @count.zero?
  end
end