Class: Farce::Concurrent::CountDownLatch

Inherits:
Object
  • Object
show all
Includes:
Internal::Noncopyable, Shareable::Delegated
Defined in:
lib/farce/concurrent/count_down_latch.rb

Overview

A replacement for concurrent-ruby's CountDownLatch implementation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Internal::Noncopyable

#duplicable?

Methods included from Shareable

#ractor_shareable?

Constructor Details

#initialize(count = 1) ⇒ CountDownLatch

Returns a new instance of CountDownLatch.

Parameters:

  • count (Integer) (defaults to: 1) —

    the initial count for the latch.

Raises:

  • (ArgumentError) —

    if the count is not a non-negative integer.



15
16
17
18
19
# File 'lib/farce/concurrent/count_down_latch.rb', line 15

def initialize(count = 1)
  raise ArgumentError, "count must be a non-negative integer" unless count.is_a?(Integer) && count >= 0
  @counter = Counter.new(count)
  super()
end

Instance Attribute Details

#count ⇒ BasicObject (readonly)



23
# File 'lib/farce/concurrent/count_down_latch.rb', line 23

def count = @counter.value

Instance Method Details

#count_down

This method returns an undefined value.

Decrements the count of the latch by 1 if it is above 0.



27
# File 'lib/farce/concurrent/count_down_latch.rb', line 27

def count_down = @counter.decrement_if_above(0)

#wait(timeout = nil) ⇒ Boolean

Waits until the count reaches 0 or the optional timeout is reached.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    the maximum time to wait in seconds, or nil to wait indefinitely.

Returns:

  • (Boolean) —

    true if the count reached 0, false if the timeout was reached.



32
# File 'lib/farce/concurrent/count_down_latch.rb', line 32

def wait(timeout = nil) = !!@counter.wait_while_above(0, timeout:) # rubocop:disable Naming/PredicateMethod