-
Notifications
You must be signed in to change notification settings - Fork 0
/
num_queue.go
55 lines (45 loc) · 859 Bytes
/
num_queue.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
50
51
52
53
54
55
package utils
import (
"sync"
)
type NumQueue struct {
mu sync.Mutex
lastDone uint64
waiters []chan struct{}
}
func NewNumQueue(init uint64) *NumQueue {
return &NumQueue{
lastDone: init,
}
}
func (q *NumQueue) Done(n uint64) {
q.mu.Lock()
defer q.mu.Unlock()
if n <= q.lastDone {
panic("Already done!")
}
pos := int(n - q.lastDone - 1)
for i := 0; i < len(q.waiters) && i <= pos; i++ {
close(q.waiters[i])
}
if pos < len(q.waiters) {
q.waiters = q.waiters[pos+1:]
} else {
q.waiters = make([]chan struct{}, 0, 1000)
}
q.lastDone = n
}
func (q *NumQueue) WaitFor(n uint64) {
q.mu.Lock()
if n <= q.lastDone {
q.mu.Unlock()
return
}
count := int(n - q.lastDone)
for i := len(q.waiters); i < count; i++ {
q.waiters = append(q.waiters, make(chan struct{}))
}
ch := q.waiters[count-1]
q.mu.Unlock()
<-ch
}