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 {
|
|
|
|
m Mutex
|
2011-09-16 17:47:21 +02:00
|
|
|
counter int32
|
|
|
|
waiters int32
|
2011-03-17 00:05:44 +01:00
|
|
|
sema *uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitGroup creates a new semaphore each time the old semaphore
|
|
|
|
// is released. This is to avoid the following race:
|
|
|
|
//
|
|
|
|
// G1: Add(1)
|
|
|
|
// G1: go G2()
|
|
|
|
// G1: Wait() // Context switch after Unlock() and before Semacquire().
|
|
|
|
// G2: Done() // Release semaphore: sema == 1, waiters == 0. G1 doesn't run yet.
|
|
|
|
// G3: Wait() // Finds counter == 0, waiters == 0, doesn't block.
|
|
|
|
// G3: Add(1) // Makes counter == 1, waiters == 0.
|
|
|
|
// G3: go G4()
|
|
|
|
// G3: Wait() // G1 still hasn't run, G3 finds sema == 1, unblocked! Bug.
|
|
|
|
|
|
|
|
// 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
|
|
|
//
|
|
|
|
// Note that calls with positive delta must happen before the call to Wait,
|
|
|
|
// or else Wait may wait for too small a group. Typically this means the calls
|
|
|
|
// to Add should execute before the statement creating the goroutine or
|
|
|
|
// other event to be waited for. See the WaitGroup example.
|
2011-03-17 00:05:44 +01:00
|
|
|
func (wg *WaitGroup) Add(delta int) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
2013-07-16 08:54:42 +02:00
|
|
|
_ = wg.m.state
|
2012-10-23 06:31:11 +02:00
|
|
|
raceReleaseMerge(unsafe.Pointer(wg))
|
|
|
|
raceDisable()
|
|
|
|
defer raceEnable()
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
v := atomic.AddInt32(&wg.counter, int32(delta))
|
|
|
|
if v < 0 {
|
2012-10-03 07:27:36 +02:00
|
|
|
panic("sync: negative WaitGroup counter")
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if v > 0 || atomic.LoadInt32(&wg.waiters) == 0 {
|
|
|
|
return
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
wg.m.Lock()
|
|
|
|
for i := int32(0); i < wg.waiters; i++ {
|
2012-03-02 21:01:37 +01:00
|
|
|
runtime_Semrelease(wg.sema)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
wg.waiters = 0
|
|
|
|
wg.sema = nil
|
2011-03-17 00:05:44 +01:00
|
|
|
wg.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done decrements the WaitGroup counter.
|
|
|
|
func (wg *WaitGroup) Done() {
|
|
|
|
wg.Add(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until the WaitGroup counter is zero.
|
|
|
|
func (wg *WaitGroup) Wait() {
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
2013-07-16 08:54:42 +02:00
|
|
|
_ = wg.m.state
|
2012-10-23 06:31:11 +02:00
|
|
|
raceDisable()
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if atomic.LoadInt32(&wg.counter) == 0 {
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(wg))
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
return
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
wg.m.Lock()
|
2011-09-16 17:47:21 +02:00
|
|
|
atomic.AddInt32(&wg.waiters, 1)
|
|
|
|
// This code is racing with the unlocked path in Add above.
|
|
|
|
// The code above modifies counter and then reads waiters.
|
|
|
|
// We must modify waiters and then read counter (the opposite order)
|
|
|
|
// to avoid missing an Add.
|
|
|
|
if atomic.LoadInt32(&wg.counter) == 0 {
|
|
|
|
atomic.AddInt32(&wg.waiters, -1)
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(wg))
|
|
|
|
raceDisable()
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
wg.m.Unlock()
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if wg.sema == nil {
|
|
|
|
wg.sema = new(uint32)
|
|
|
|
}
|
|
|
|
s := wg.sema
|
|
|
|
wg.m.Unlock()
|
2012-03-02 21:01:37 +01:00
|
|
|
runtime_Semacquire(s)
|
2012-10-23 06:31:11 +02:00
|
|
|
if raceenabled {
|
|
|
|
raceEnable()
|
|
|
|
raceAcquire(unsafe.Pointer(wg))
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|