1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // A WaitGroup waits for a collection of goroutines to finish.
10 // The main goroutine calls Add to set the number of
11 // goroutines to wait for. Then each of the goroutines
12 // runs and calls Done when finished. At the same time,
13 // Wait can be used to block until all goroutines have finished.
14 type WaitGroup struct {
21 // WaitGroup creates a new semaphore each time the old semaphore
22 // is released. This is to avoid the following race:
26 // G1: Wait() // Context switch after Unlock() and before Semacquire().
27 // G2: Done() // Release semaphore: sema == 1, waiters == 0. G1 doesn't run yet.
28 // G3: Wait() // Finds counter == 0, waiters == 0, doesn't block.
29 // G3: Add(1) // Makes counter == 1, waiters == 0.
31 // G3: Wait() // G1 still hasn't run, G3 finds sema == 1, unblocked! Bug.
33 // Add adds delta, which may be negative, to the WaitGroup counter.
34 // If the counter becomes zero, all goroutines blocked on Wait() are released.
35 // If the counter goes negative, Add panics.
36 func (wg *WaitGroup) Add(delta int) {
37 v := atomic.AddInt32(&wg.counter, int32(delta))
39 panic("sync: negative WaitGroup counter")
41 if v > 0 || atomic.LoadInt32(&wg.waiters) == 0 {
45 for i := int32(0); i < wg.waiters; i++ {
46 runtime_Semrelease(wg.sema)
53 // Done decrements the WaitGroup counter.
54 func (wg *WaitGroup) Done() {
58 // Wait blocks until the WaitGroup counter is zero.
59 func (wg *WaitGroup) Wait() {
60 if atomic.LoadInt32(&wg.counter) == 0 {
64 atomic.AddInt32(&wg.waiters, 1)
65 // This code is racing with the unlocked path in Add above.
66 // The code above modifies counter and then reads waiters.
67 // We must modify waiters and then read counter (the opposite order)
68 // to avoid missing an Add.
69 if atomic.LoadInt32(&wg.counter) == 0 {
70 atomic.AddInt32(&wg.waiters, -1)