|
|
|
@ -16,7 +16,7 @@ func NewWaitGroup(maxCount int) WaitGroup {
|
|
|
|
|
return WaitGroup{wg: &sync.WaitGroup{}, maxCount: uint32(maxCount)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (swg *WaitGroup) Add(delta int) {
|
|
|
|
|
func (w *WaitGroup) Add(delta int) {
|
|
|
|
|
var Udelta uint32
|
|
|
|
|
if delta < 0 {
|
|
|
|
|
Udelta = uint32(-delta - 1)
|
|
|
|
@ -24,32 +24,32 @@ func (swg *WaitGroup) Add(delta int) {
|
|
|
|
|
Udelta = uint32(delta)
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
allC := atomic.LoadUint32(&swg.allCount)
|
|
|
|
|
if atomic.LoadUint32(&swg.maxCount) == 0 || atomic.LoadUint32(&swg.maxCount) >= allC+uint32(delta) {
|
|
|
|
|
allC := atomic.LoadUint32(&w.allCount)
|
|
|
|
|
if atomic.LoadUint32(&w.maxCount) == 0 || atomic.LoadUint32(&w.maxCount) >= allC+uint32(delta) {
|
|
|
|
|
if delta < 0 {
|
|
|
|
|
atomic.AddUint32(&swg.allCount, ^uint32(Udelta))
|
|
|
|
|
atomic.AddUint32(&w.allCount, ^uint32(Udelta))
|
|
|
|
|
} else {
|
|
|
|
|
atomic.AddUint32(&swg.allCount, uint32(Udelta))
|
|
|
|
|
atomic.AddUint32(&w.allCount, uint32(Udelta))
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(time.Microsecond)
|
|
|
|
|
}
|
|
|
|
|
swg.wg.Add(delta)
|
|
|
|
|
w.wg.Add(delta)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (swg *WaitGroup) Done() {
|
|
|
|
|
swg.Add(-1)
|
|
|
|
|
func (w *WaitGroup) Done() {
|
|
|
|
|
w.Add(-1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (swg *WaitGroup) Wait() {
|
|
|
|
|
swg.wg.Wait()
|
|
|
|
|
func (w *WaitGroup) Wait() {
|
|
|
|
|
w.wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (swg *WaitGroup) GetMaxWaitNum() int {
|
|
|
|
|
return int(atomic.LoadUint32(&swg.maxCount))
|
|
|
|
|
func (w *WaitGroup) GetMaxWaitNum() int {
|
|
|
|
|
return int(atomic.LoadUint32(&w.maxCount))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (swg *WaitGroup) SetMaxWaitNum(num int) {
|
|
|
|
|
atomic.AddUint32(&swg.maxCount, uint32(num))
|
|
|
|
|
func (w *WaitGroup) SetMaxWaitNum(num int) {
|
|
|
|
|
atomic.AddUint32(&w.maxCount, uint32(num))
|
|
|
|
|
}
|
|
|
|
|