-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwg.go
43 lines (34 loc) · 611 Bytes
/
nwg.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
package rest
import "sync"
type numeralWaitGroup struct {
cnt int
cd *sync.Cond
lk sync.RWMutex
}
func newNWG() *numeralWaitGroup {
res := &numeralWaitGroup{}
res.cd = sync.NewCond(res.lk.RLocker())
return res
}
func (n *numeralWaitGroup) Add(delta int) {
n.lk.Lock()
defer n.lk.Unlock()
n.cnt += delta
if delta < 0 {
n.cd.Broadcast()
}
}
func (n *numeralWaitGroup) Done() {
n.Add(-1)
}
// Wait waits until there is less or equal than "min" tasks
func (n *numeralWaitGroup) Wait(min int) {
n.lk.RLock()
defer n.lk.RUnlock()
for {
if n.cnt <= min {
return
}
n.cd.Wait()
}
}