Class: Concurrent::MutexCountDownLatch
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
Concurrent::Synchronization::LockableObject
|
Defined in: | lib/concurrent-ruby/concurrent/atomic/mutex_count_down_latch.rb |
Overview
**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.
A synchronization object that allows one thread to wait on multiple other threads. The thread that will wait creates a CountDownLatch
and sets the initial value (normally equal to the number of other threads). The initiating thread passes the latch to the other threads then waits for the other threads by calling the #wait method. Each of the other threads calls #count_down when done with its work. When the latch counter reaches zero the waiting thread is unblocked and continues with its work. A CountDownLatch
can be used only once. Its value cannot be reset.
Class Method Summary
-
.new(count = 1) ⇒ MutexCountDownLatch
constructor
Create a new
CountDownLatch
with the initial #count.
Instance Method Summary
-
#count ⇒ Fixnum
The current value of the counter.
-
#count_down
Signal the latch to decrement the counter.
-
#wait(timeout = nil) ⇒ Boolean
Block on the latch until the counter reaches zero or until
timeout
is reached.
Synchronization::LockableObject
- Inherited
Constructor Details
.new(count = 1) ⇒ MutexCountDownLatch
Create a new CountDownLatch
with the initial #count.
# File 'lib/concurrent-ruby/concurrent/atomic/mutex_count_down_latch.rb', line 12
def initialize(count = 1) Utility::NativeInteger.ensure_integer_and_bounds count Utility::NativeInteger.ensure_positive count super() synchronize { ns_initialize count } end
Instance Method Details
#count ⇒ Fixnum
The current value of the counter.
# File 'lib/concurrent-ruby/concurrent/atomic/mutex_count_down_latch.rb', line 34
def count synchronize { @count } end
#count_down
Signal the latch to decrement the counter. Will signal all blocked threads when the #count reaches zero.
# File 'lib/concurrent-ruby/concurrent/atomic/mutex_count_down_latch.rb', line 26
def count_down synchronize do @count -= 1 if @count > 0 ns_broadcast if @count == 0 end end
#wait(timeout = nil) ⇒ Boolean
Block on the latch until the counter reaches zero or until timeout
is reached.
# File 'lib/concurrent-ruby/concurrent/atomic/mutex_count_down_latch.rb', line 21
def wait(timeout = nil) synchronize { ns_wait_until(timeout) { @count == 0 } } end