Sha256: 962ab40df2ac322589a3d12d86a2d29cb5b47a0d0bfaf6156df46dbe07991ba9

Contents?: true

Size: 711 Bytes

Versions: 5

Compression:

Stored size: 711 Bytes

Contents

require "agent/errors"

module Agent
  class WaitGroup
    attr_reader :count

    def initialize
      @count = 0
      @mutex = Mutex.new
      @cvar  = ConditionVariable.new
    end

    def wait
      @mutex.synchronize do
        while @count > 0
          @cvar.wait(@mutex)
        end
      end
    end

    def add(delta)
      @mutex.synchronize do
        modify_count(delta)
      end
    end

    def done
      @mutex.synchronize do
        modify_count(-1)
      end
    end

  protected

    # Expects to be called inside of the mutex
    def modify_count(delta)
      @count += delta
      raise Errors::NegativeWaitGroupCount if @count < 0
      @cvar.signal if @count == 0
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
agent-0.12.0 lib/agent/wait_group.rb
agent-0.11.0 lib/agent/wait_group.rb
agent-0.10.0 lib/agent/wait_group.rb
agent-0.9.1 lib/agent/wait_group.rb
agent-0.9.0 lib/agent/wait_group.rb