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.
13 // A WaitGroup waits for a collection of goroutines to finish.
14 // The main goroutine calls Add to set the number of
15 // goroutines to wait for. Then each of the goroutines
16 // runs and calls Done when finished. At the same time,
17 // Wait can be used to block until all goroutines have finished.
19 // A WaitGroup must not be copied after first use.
20 type WaitGroup
struct {
23 // 64-bit value: high 32 bits are counter, low 32 bits are waiter count.
24 // 64-bit atomic operations require 64-bit alignment, but 32-bit
25 // compilers do not ensure it. So we allocate 12 bytes and then use
26 // the aligned 8 bytes in them as state.
31 func (wg
*WaitGroup
) state() *uint64 {
32 if uintptr(unsafe
.Pointer(&wg
.state1
))%8
== 0 {
33 return (*uint64)(unsafe
.Pointer(&wg
.state1
))
35 return (*uint64)(unsafe
.Pointer(&wg
.state1
[4]))
39 // Add adds delta, which may be negative, to the WaitGroup counter.
40 // If the counter becomes zero, all goroutines blocked on Wait are released.
41 // If the counter goes negative, Add panics.
43 // Note that calls with a positive delta that occur when the counter is zero
44 // must happen before a Wait. Calls with a negative delta, or calls with a
45 // positive delta that start when the counter is greater than zero, may happen
47 // Typically this means the calls to Add should execute before the statement
48 // creating the goroutine or other event to be waited for.
49 // If a WaitGroup is reused to wait for several independent sets of events,
50 // new Add calls must happen after all previous Wait calls have returned.
51 // See the WaitGroup example.
52 func (wg
*WaitGroup
) Add(delta
int) {
55 _
= *statep
// trigger nil deref early
57 // Synchronize decrements with Wait.
58 race
.ReleaseMerge(unsafe
.Pointer(wg
))
63 state
:= atomic
.AddUint64(statep
, uint64(delta
)<<32)
64 v
:= int32(state
>> 32)
66 if race
.Enabled
&& delta
> 0 && v
== int32(delta
) {
67 // The first increment must be synchronized with Wait.
68 // Need to model this as a read, because there can be
69 // several concurrent wg.counter transitions from 0.
70 race
.Read(unsafe
.Pointer(&wg
.sema
))
73 panic("sync: negative WaitGroup counter")
75 if w
!= 0 && delta
> 0 && v
== int32(delta
) {
76 panic("sync: WaitGroup misuse: Add called concurrently with Wait")
81 // This goroutine has set counter to 0 when waiters > 0.
82 // Now there can't be concurrent mutations of state:
83 // - Adds must not happen concurrently with Wait,
84 // - Wait does not increment waiters if it sees counter == 0.
85 // Still do a cheap sanity check to detect WaitGroup misuse.
87 panic("sync: WaitGroup misuse: Add called concurrently with Wait")
89 // Reset waiters count to 0.
92 runtime_Semrelease(&wg
.sema
, false)
96 // Done decrements the WaitGroup counter by one.
97 func (wg
*WaitGroup
) Done() {
101 // Wait blocks until the WaitGroup counter is zero.
102 func (wg
*WaitGroup
) Wait() {
105 _
= *statep
// trigger nil deref early
109 state
:= atomic
.LoadUint64(statep
)
110 v
:= int32(state
>> 32)
113 // Counter is 0, no need to wait.
116 race
.Acquire(unsafe
.Pointer(wg
))
120 // Increment waiters count.
121 if atomic
.CompareAndSwapUint64(statep
, state
, state
+1) {
122 if race
.Enabled
&& w
== 0 {
123 // Wait must be synchronized with the first Add.
124 // Need to model this is as a write to race with the read in Add.
125 // As a consequence, can do the write only for the first waiter,
126 // otherwise concurrent Waits will race with each other.
127 race
.Write(unsafe
.Pointer(&wg
.sema
))
129 runtime_Semacquire(&wg
.sema
)
131 panic("sync: WaitGroup is reused before previous Wait has returned")
135 race
.Acquire(unsafe
.Pointer(wg
))