Class: Salus::CountDownLatch
- Inherits:
-
Object
- Object
- Salus::CountDownLatch
- Includes:
- Lockable
- Defined in:
- lib/salus/thread/latch.rb
Overview
Based on code from github.com/ruby-concurrency/concurrent-ruby/
Instance Method Summary collapse
- #count ⇒ Object
- #count_down ⇒ Object
-
#initialize(to = 1) ⇒ CountDownLatch
constructor
A new instance of CountDownLatch.
- #wait(timeout = nil) ⇒ Object
Methods included from Lockable
#broadcast, #signal, #synchronize, #wait_until
Constructor Details
#initialize(to = 1) ⇒ CountDownLatch
Returns a new instance of CountDownLatch.
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
#count ⇒ Object
18 19 20 |
# File 'lib/salus/thread/latch.rb', line 18 def count synchronize { @count } end |
#count_down ⇒ Object
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 |