Class: Rbgo::WaitGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgo/wait_group.rb

Instance Method Summary collapse

Constructor Details

#initialize(init_count = 0) ⇒ WaitGroup

Returns a new instance of WaitGroup.



5
6
7
8
9
# 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

Instance Method Details

#add(count) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# 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

#doneObject



23
24
25
26
27
28
29
30
31
32
33
# 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

#waitObject



35
36
37
38
39
40
41
# File 'lib/rbgo/wait_group.rb', line 35

def wait
  mutex.synchronize do
    while total_count > 0
      cond.wait(mutex)
    end
  end
end