class WaitGroup

Public Class Methods

new() click to toggle source
# File lib/channel/waitgroup.rb, line 2
def initialize
  @mutex = Mutex.new
  @count = 0
  @waiting = []
end

Public Instance Methods

add(delta) click to toggle source
# File lib/channel/waitgroup.rb, line 8
def add(delta)
  sync! do
    @count += delta
    fail 'negative WaitGroup counter' if @count < 0
    if @waiting.any? && delta > 0 && @count == delta
      fail 'misuse: add called concurrently with wait'
    end
    wake!
  end
end
done() click to toggle source
# File lib/channel/waitgroup.rb, line 19
def done
  add(-1)
end
wait() click to toggle source
# File lib/channel/waitgroup.rb, line 27
def wait
  sync! do
    @waiting << Thread.current
    @mutex.sleep until done?
  end
end

Private Instance Methods

done?() click to toggle source
# File lib/channel/waitgroup.rb, line 23
        def done?
  @count == 0
end
sync!(&block) click to toggle source
# File lib/channel/waitgroup.rb, line 38
        def sync!(&block)
  @mutex.synchronize(&block)
end
wake!() click to toggle source
# File lib/channel/waitgroup.rb, line 34
        def wake!
  @waiting.each { |t| t.wakeup if t.alive? }
end