Class: Salus::CountDownLatch

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/salus/thread/latch.rb

Overview

Instance Method Summary collapse

Methods included from Lockable

#broadcast, #signal, #synchronize, #wait_until

Constructor Details

#initialize(to = 1) ⇒ CountDownLatch

Returns a new instance of CountDownLatch.

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/salus/thread/latch.rb', line 6

def initialize(to=1)
  synchronize { @count = to.to_i }
  raise ArgumentError, "cannot count down from negative integer" unless @count >= 0
end

Instance Method Details

#countObject



18
19
20
# File 'lib/salus/thread/latch.rb', line 18

def count
  synchronize { @count }
end

#count_downObject



11
12
13
14
15
16
# File 'lib/salus/thread/latch.rb', line 11

def count_down
  synchronize do
    @count -= 1 if @count >  0
    broadcast   if @count == 0
  end
end

#wait(timeout = nil) ⇒ Object



22
23
24
25
26
# File 'lib/salus/thread/latch.rb', line 22

def wait(timeout=nil)
  synchronize do
    wait_until(timeout) { @count == 0 }
  end
end