2011-03-17 00:05:44 +01:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sync
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2011-03-17 00:05:44 +01:00
|
|
|
|
|
|
|
// A WaitGroup waits for a collection of goroutines to finish.
|
|
|
|
// The main goroutine calls Add to set the number of
|
|
|
|
// goroutines to wait for. Then each of the goroutines
|
|
|
|
// runs and calls Done when finished. At the same time,
|
|
|
|
// Wait can be used to block until all goroutines have finished.
|
|
|
|
type WaitGroup struct {
|
2015-10-31 01:59:47 +01:00
|
|
|
// 64-bit value: high 32 bits are counter, low 32 bits are waiter count.
|
|
|
|
// 64-bit atomic operations require 64-bit alignment, but 32-bit
|
|
|
|
// compilers do not ensure it. So we allocate 12 bytes and then use
|
|
|
|
// the aligned 8 bytes in them as state.
|
|
|
|
state1 [12]byte
|
|
|
|
sema uint32
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (wg *WaitGroup) state() *uint64 {
|
|
|
|
if uintptr(unsafe.Pointer(&wg.state1))%8 == 0 {
|
|
|
|
return (*uint64)(unsafe.Pointer(&wg.state1))
|
|
|
|
} else {
|
|
|
|
return (*uint64)(unsafe.Pointer(&wg.state1[4]))
|
|
|
|
}
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
|
|
|
|
// Add adds delta, which may be negative, to the WaitGroup counter.
|
2013-07-16 08:54:42 +02:00
|
|
|
// If the counter becomes zero, all goroutines blocked on Wait are released.
|
2012-10-03 07:27:36 +02:00
|
|
|
// If the counter goes negative, Add panics.
|
2013-07-16 08:54:42 +02:00
|
|
|
//
|
2015-01-15 01:27:56 +01:00
|
|
|
// Note that calls with a positive delta that occur when the counter is zero
|
|
|
|
// must happen before a Wait. Calls with a negative delta, or calls with a
|
|
|
|
// positive delta that start when the counter is greater than zero, may happen
|
|
|
|
// at any time.
|
|
|
|
// Typically this means the calls to Add should execute before the statement
|
|
|
|
// creating the goroutine or other event to be waited for.
|
2015-10-31 01:59:47 +01:00
|
|
|
// If a WaitGroup is reused to wait for several independent sets of events,
|
|
|
|
// new Add calls must happen after all previous Wait calls have returned.
|
2015-01-15 01:27:56 +01:00
|
|
|
// See the WaitGroup example.
|
2011-03-17 00:05:44 +01:00
|
|
|
func (wg *WaitGroup) Add(delta int) {
|
2015-10-31 01:59:47 +01:00
|
|
|
statep := wg.state()
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
2015-10-31 01:59:47 +01:00
|
|
|
_ = *statep // trigger nil deref early
|
2013-11-06 20:49:01 +01:00
|
|
|
if delta < 0 {
|
|
|
|
// Synchronize decrements with Wait.
|
|
|
|
raceReleaseMerge(unsafe.Pointer(wg))
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
raceDisable()
|
|
|
|
defer raceEnable()
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
state := atomic.AddUint64(statep, uint64(delta)<<32)
|
|
|
|
v := int32(state >> 32)
|
|
|
|
w := uint32(state)
|
2013-11-06 20:49:01 +01:00
|
|
|
if raceenabled {
|
|
|
|
if delta > 0 && v == int32(delta) {
|
|
|
|
// The first increment must be synchronized with Wait.
|
|
|
|
// Need to model this as a read, because there can be
|
|
|
|
// several concurrent wg.counter transitions from 0.
|
|
|
|
raceRead(unsafe.Pointer(&wg.sema))
|
|
|
|
}
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if v < 0 {
|
2012-10-03 07:27:36 +02:00
|
|
|
panic("sync: negative WaitGroup counter")
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
if w != 0 && delta > 0 && v == int32(delta) {
|
|
|
|
panic("sync: WaitGroup misuse: Add called concurrently with Wait")
|
|
|
|
}
|
|
|
|
if v > 0 || w == 0 {
|
2011-09-16 17:47:21 +02:00
|
|
|
return
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
// This goroutine has set counter to 0 when waiters > 0.
|
|
|
|
// Now there can't be concurrent mutations of state:
|
|
|
|
// - Adds must not happen concurrently with Wait,
|
|
|
|
// - Wait does not increment waiters if it sees counter == 0.
|
|
|
|
// Still do a cheap sanity check to detect WaitGroup misuse.
|
|
|
|
if *statep != state {
|
|
|
|
panic("sync: WaitGroup misuse: Add called concurrently with Wait")
|
|
|
|
}
|
|
|
|
// Reset waiters count to 0.
|
|
|
|
*statep = 0
|
|
|
|
for ; w != 0; w-- {
|
|
|
|
runtime_Semrelease(&wg.sema)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done decrements the WaitGroup counter.
|
|
|
|
func (wg *WaitGroup) Done() {
|
|
|
|
wg.Add(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until the WaitGroup counter is zero.
|
|
|
|
func (wg *WaitGroup) Wait() {
|
2015-10-31 01:59:47 +01:00
|
|
|
statep := wg.state()
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
2015-10-31 01:59:47 +01:00
|
|
|
_ = *statep // trigger nil deref early
|
2012-10-23 06:31:11 +02:00
|
|
|
raceDisable()
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
for {
|
|
|
|
state := atomic.LoadUint64(statep)
|
|
|
|
v := int32(state >> 32)
|
|
|
|
w := uint32(state)
|
|
|
|
if v == 0 {
|
|
|
|
// Counter is 0, no need to wait.
|
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(wg))
|
|
|
|
}
|
|
|
|
return
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
// Increment waiters count.
|
|
|
|
if atomic.CompareAndSwapUint64(statep, state, state+1) {
|
|
|
|
if raceenabled && w == 0 {
|
|
|
|
// Wait must be synchronized with the first Add.
|
|
|
|
// Need to model this is as a write to race with the read in Add.
|
|
|
|
// As a consequence, can do the write only for the first waiter,
|
|
|
|
// otherwise concurrent Waits will race with each other.
|
|
|
|
raceWrite(unsafe.Pointer(&wg.sema))
|
|
|
|
}
|
|
|
|
runtime_Semacquire(&wg.sema)
|
|
|
|
if *statep != 0 {
|
|
|
|
panic("sync: WaitGroup is reused before previous Wait has returned")
|
|
|
|
}
|
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(wg))
|
|
|
|
}
|
|
|
|
return
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|