generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwait-group.go
49 lines (41 loc) · 900 Bytes
/
wait-group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pants
import (
"sync"
"sync/atomic"
)
// Tracker
type Tracker func(count int32)
// TrackWaitGroup returns a trackable wait group for the native sync
// wait group specified; useful for debugging purposes.
func TrackWaitGroup(wg *sync.WaitGroup, add, done Tracker) WaitGroup {
return &TrackableWaitGroup{
wg: wg,
add: add,
done: done,
}
}
// TrackableWaitGroup
type TrackableWaitGroup struct {
wg *sync.WaitGroup
counter int32
add, done Tracker
}
// Add
func (t *TrackableWaitGroup) Add(delta int) {
n := atomic.AddInt32(&t.counter, int32(delta)) //nolint:gosec // ok
t.wg.Add(delta)
t.add(n)
}
// Done
func (t *TrackableWaitGroup) Done() {
n := atomic.AddInt32(&t.counter, int32(-1))
t.wg.Done()
t.done(n)
}
// Wait
func (t *TrackableWaitGroup) Wait() {
t.wg.Wait()
}
func (t *TrackableWaitGroup) Count() int32 {
return atomic.LoadInt32(&t.counter)
}