Class: AtomicRuby::AtomicCountDownLatch

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic-ruby/atomic_count_down_latch.rb

Defined Under Namespace

Classes: AlreadyCountedDownError, InvalidCountError

Instance Method Summary collapse

Constructor Details

#initialize(count) ⇒ AtomicCountDownLatch

Returns a new instance of AtomicCountDownLatch.



10
11
12
13
14
15
16
17
18
# File 'lib/atomic-ruby/atomic_count_down_latch.rb', line 10

def initialize(count)
  unless count.is_a?(Integer)
    raise InvalidCountError, "expected count to be an `Integer`, got #{count.class}"
  end

  @count = Atom.new(count)

  Ractor.make_shareable(self)
end

Instance Method Details

#countObject



20
21
22
# File 'lib/atomic-ruby/atomic_count_down_latch.rb', line 20

def count
  @count.value
end

#count_downObject



24
25
26
27
28
29
30
# File 'lib/atomic-ruby/atomic_count_down_latch.rb', line 24

def count_down
  unless @count.value > 0
    raise AlreadyCountedDownError, "count has already reached zero"
  end

  @count.swap { |current_value| current_value - 1 }
end

#waitObject



32
33
34
# File 'lib/atomic-ruby/atomic_count_down_latch.rb', line 32

def wait
  Thread.pass while @count.value > 0
end