class Rbgo::WaitGroup

Attributes

cond[RW]
mutex[RW]
total_count[RW]

Public Class Methods

new(init_count = 0) click to toggle source
# File lib/rbgo/wait_group.rb, line 5
def initialize(init_count = 0)
  self.total_count = [0, init_count.to_i].max
  self.mutex       = Mutex.new
  self.cond        = ConditionVariable.new
end

Public Instance Methods

add(count) click to toggle source
# File lib/rbgo/wait_group.rb, line 11
def add(count)
  count = count.to_i
  mutex.synchronize do
    c = total_count + count
    if c < 0
      raise RuntimeError.new('WaitGroup counts < 0')
    else
      self.total_count = c
    end
  end
end
done() click to toggle source
# File lib/rbgo/wait_group.rb, line 23
def done
  mutex.synchronize do
    c = total_count - 1
    if c < 0
      raise RuntimeError.new('WaitGroup counts < 0')
    else
      self.total_count = c
    end
    cond.broadcast if c == 0
  end
end
wait() click to toggle source
# File lib/rbgo/wait_group.rb, line 35
def wait
  mutex.synchronize do
    while total_count > 0
      cond.wait(mutex)
    end
  end
end