Sha256: 477c6b0a96da6f9b0211e6653f0d9590b9aa40172a98eb4075bf0e2a6109d66e

Contents?: true

Size: 623 Bytes

Versions: 4

Compression:

Stored size: 623 Bytes

Contents

module Salus
  # Based on code from https://github.com/ruby-concurrency/concurrent-ruby/
  class CountDownLatch
    include Lockable

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

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

    def count
      synchronize { @count }
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
salus-0.2.1 lib/salus/thread/latch.rb
salus-0.2.0 lib/salus/thread/latch.rb
salus-0.1.3 lib/salus/thread/latch.rb
salus-0.1.2 lib/salus/thread/latch.rb