class Contender::CountdownLatch

Synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads complete

Attributes

count[R]

@return [Integer]

Public Class Methods

new(initial) click to toggle source

@param [Integer] initial @return [undefined]

# File lib/contender/countdown_latch.rb, line 10
def initialize(initial)
  raise ArgumentError if initial < 0

  @count = initial

  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Public Instance Methods

await(timeout = nil) click to toggle source

Waits until either the latch reaches zero or the timeout is reached

@api public @param [Float] timeout @return [Boolean] True if the latch reached zero, false if the timeout was reached

# File lib/contender/countdown_latch.rb, line 35
def await(timeout = nil)
  @mutex.synchronize do
    return true if @count == 0
    @condition.wait @mutex, timeout

    @count == 0
  end
end
countdown() click to toggle source

Performs an ordered decrement operation for this latch

@api public @return [undefined]

# File lib/contender/countdown_latch.rb, line 23
def countdown
  @mutex.synchronize do
    @count -= 1 if @count > 0
    @condition.broadcast if @count == 0
  end
end