-
Notifications
You must be signed in to change notification settings - Fork 10
/
completer.go
59 lines (52 loc) · 1.03 KB
/
completer.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
56
57
58
59
// +build linux
package iouring
import (
"sync/atomic"
)
type completer struct {
cq *CompletionQueue
stopCh chan struct{}
seen chan int
}
func newCompleter(cq *CompletionQueue, bufSize int) *completer {
return &completer{
cq: cq,
stopCh: make(chan struct{}, 8),
seen: make(chan int, bufSize),
}
}
func (c *completer) complete(id int) {
c.seen <- id
}
func (c *completer) stop() {
c.stopCh <- struct{}{}
}
func (c *completer) run() {
unacked := map[int]struct{}{}
for {
select {
case <-c.stopCh:
return
case id := <-c.seen:
// TODO: is it bad to see twice?
if _, ok := unacked[id]; !ok {
unacked[id] = struct{}{}
}
head := atomic.LoadUint32(c.cq.Head)
mask := atomic.LoadUint32(c.cq.Mask)
seen := int(0)
// Continue to move the head until the next value
// hasn't arrived yet.
curHead := int(head & mask)
for {
_, ok := unacked[curHead+seen]
if !ok {
break
}
delete(unacked, curHead+seen)
seen++
}
atomic.AddUint32(c.cq.Head, uint32(seen))
}
}
}