Class: Contender::CountdownLatch

Inherits:
Object
  • Object
show all
Defined in:
lib/contender/countdown_latch.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(initial) ⇒ undefined

Parameters:

  • initial (Integer)


7
8
9
10
11
12
# File 'lib/contender/countdown_latch.rb', line 7

def initialize(initial)
  @count = initial

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

Instance Method Details

#await(timeout = nil) ⇒ undefined

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

Parameters:

  • timeout (Float) (defaults to: nil)

Returns:

  • (undefined)


38
39
40
41
42
43
# File 'lib/contender/countdown_latch.rb', line 38

def await(timeout = nil)
  @mutex.synchronize do
    return if @count == 0
    @condition.wait @mutex, timeout
  end
end

#countInteger

Performs an ordered read of the count for this latch

Returns:

  • (Integer)


16
17
18
19
20
# File 'lib/contender/countdown_latch.rb', line 16

def count
  @mutex.synchronize do
    @count
  end
end

#countdownundefined

Performs an ordered decrement operation for this latch

Returns:

  • (undefined)


26
27
28
29
30
31
# File 'lib/contender/countdown_latch.rb', line 26

def countdown
  @mutex.synchronize do
    @count = @count.pred if @count > 0
    @condition.broadcast
  end
end