// 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

import (
	
	
	
)

// 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.
//
// A WaitGroup must not be copied after first use.
//
// In the terminology of the Go memory model, a call to Done
// “synchronizes before” the return of any Wait call that it unblocks.
type WaitGroup struct {
	noCopy noCopy

	// 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 only guarantee that 64-bit fields are 32-bit aligned.
	// For this reason on 32 bit architectures we need to check in state()
	// if state1 is aligned or not, and dynamically "swap" the field order if
	// needed.
	state1 uint64
	state2 uint32
}

// state returns pointers to the state and sema fields stored within wg.state*.
func ( *WaitGroup) () ( *uint64,  *uint32) {
	if unsafe.Alignof(.state1) == 8 || uintptr(unsafe.Pointer(&.state1))%8 == 0 {
		// state1 is 64-bit aligned: nothing to do.
		return &.state1, &.state2
	} else {
		// state1 is 32-bit aligned but not 64-bit aligned: this means that
		// (&state1)+4 is 64-bit aligned.
		 := (*[3]uint32)(unsafe.Pointer(&.state1))
		return (*uint64)(unsafe.Pointer(&[1])), &[0]
	}
}

// Add adds delta, which may be negative, to the WaitGroup counter.
// If the counter becomes zero, all goroutines blocked on Wait are released.
// If the counter goes negative, Add panics.
//
// 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.
// 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.
// See the WaitGroup example.
func ( *WaitGroup) ( int) {
	,  := .state()
	if race.Enabled {
		_ = * // trigger nil deref early
		if  < 0 {
			// Synchronize decrements with Wait.
			race.ReleaseMerge(unsafe.Pointer())
		}
		race.Disable()
		defer race.Enable()
	}
	 := atomic.AddUint64(, uint64()<<32)
	 := int32( >> 32)
	 := uint32()
	if race.Enabled &&  > 0 &&  == int32() {
		// 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.
		race.Read(unsafe.Pointer())
	}
	if  < 0 {
		panic("sync: negative WaitGroup counter")
	}
	if  != 0 &&  > 0 &&  == int32() {
		panic("sync: WaitGroup misuse: Add called concurrently with Wait")
	}
	if  > 0 ||  == 0 {
		return
	}
	// 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 * !=  {
		panic("sync: WaitGroup misuse: Add called concurrently with Wait")
	}
	// Reset waiters count to 0.
	* = 0
	for ;  != 0; -- {
		runtime_Semrelease(, false, 0)
	}
}

// Done decrements the WaitGroup counter by one.
func ( *WaitGroup) () {
	.Add(-1)
}

// Wait blocks until the WaitGroup counter is zero.
func ( *WaitGroup) () {
	,  := .state()
	if race.Enabled {
		_ = * // trigger nil deref early
		race.Disable()
	}
	for {
		 := atomic.LoadUint64()
		 := int32( >> 32)
		 := uint32()
		if  == 0 {
			// Counter is 0, no need to wait.
			if race.Enabled {
				race.Enable()
				race.Acquire(unsafe.Pointer())
			}
			return
		}
		// Increment waiters count.
		if atomic.CompareAndSwapUint64(, , +1) {
			if race.Enabled &&  == 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.
				race.Write(unsafe.Pointer())
			}
			runtime_Semacquire()
			if * != 0 {
				panic("sync: WaitGroup is reused before previous Wait has returned")
			}
			if race.Enabled {
				race.Enable()
				race.Acquire(unsafe.Pointer())
			}
			return
		}
	}
}