-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggq.go
171 lines (154 loc) · 3.34 KB
/
ggq.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ggq
import (
"runtime"
"sync"
"sync/atomic"
"unsafe"
"github.com/charlesderek/actor-model/log"
)
type Consumer[T any] interface {
Consume([]T)
}
const cacheLinePadding = 64
const (
slotEmpty = iota
slotBusy
slotCommitted
)
const (
stateRunning = iota
stateClosed
)
type slot[T any] struct {
item T
atomic.Uint32
}
type GGQ[T any] struct {
_ [cacheLinePadding]byte
written atomic.Uint32
_ [cacheLinePadding - unsafe.Sizeof(atomic.Uint32{})]byte
read atomic.Uint32
_ [cacheLinePadding - unsafe.Sizeof(atomic.Uint32{})]byte
state atomic.Uint32
_ [cacheLinePadding - unsafe.Sizeof(atomic.Uint32{})]byte
isIdling atomic.Bool
_ [cacheLinePadding - unsafe.Sizeof(atomic.Bool{})]byte
buffer []slot[T]
_ [cacheLinePadding]byte
mask uint32
consumer Consumer[T]
itemBuffer []T
cond *sync.Cond
}
func New[T any](size uint32, consumer Consumer[T]) *GGQ[T] {
if !isPOW2(size) {
log.Fatalw("the size of the queue need to be a number that is the power of 2", log.M{})
}
return &GGQ[T]{
buffer: make([]slot[T], size),
mask: size - 1,
consumer: consumer,
itemBuffer: make([]T, size+1),
cond: sync.NewCond(nil),
}
}
func (q *GGQ[T]) Write(val T) {
slot := &q.buffer[q.written.Add(1)&q.mask]
for !slot.CompareAndSwap(slotEmpty, slotBusy) {
switch slot.Load() {
case slotBusy, slotCommitted:
runtime.Gosched()
case slotEmpty:
continue
}
}
slot.item = val
slot.Store(slotCommitted)
}
func (q *GGQ[T]) ReadN() (T, bool) {
var lower, upper uint32
current := q.read.Load()
for {
lower = current + 1
upper = q.written.Load()
if lower <= upper {
q.Consume(lower, upper)
q.read.Store(upper)
current = upper
runtime.Gosched()
} else if upper := q.written.Load(); lower <= upper {
runtime.Gosched()
} else if !q.state.CompareAndSwap(stateClosed, stateRunning) {
var mu sync.Mutex
q.cond.L = &mu
q.isIdling.Store(true)
mu.Lock()
q.cond.Wait()
mu.Unlock()
q.isIdling.Store(false)
} else {
break
}
}
var t T
return t, true
}
// Awake the queue if its in the idle state.
func (q *GGQ[T]) Awake() {
if q.isIdling.Load() {
q.cond.Signal()
}
}
func (q *GGQ[T]) IsIdle() bool {
return q.isIdling.Load()
}
func (q *GGQ[T]) Consume(lower, upper uint32) {
consumed := 0
for ; lower <= upper; lower++ {
slot := &q.buffer[lower&q.mask]
for !slot.CompareAndSwap(slotCommitted, slotBusy) {
switch slot.Load() {
case slotBusy:
runtime.Gosched()
case slotCommitted:
continue
}
}
q.itemBuffer[consumed] = slot.item
slot.Store(slotEmpty)
consumed++
}
q.consumer.Consume(q.itemBuffer[:consumed])
}
// ReadN gives way better performance, due to batching messages with
// lock os thread.
func (q *GGQ[T]) Read() (T, bool) {
slot := &q.buffer[q.read.Add(1)&q.mask]
for !slot.CompareAndSwap(slotCommitted, slotBusy) {
switch slot.Load() {
case slotBusy:
runtime.Gosched()
case slotEmpty:
if q.state.CompareAndSwap(stateClosed, stateRunning) {
var t T
return t, true
}
runtime.Gosched()
case slotCommitted:
continue
}
}
item := slot.item
slot.Store(slotEmpty)
return item, false
}
func (q *GGQ[T]) Close() {
q.state.Store(stateClosed)
q.cond.Signal()
}
func isPOW2(n uint32) bool {
if n <= 0 {
return false
}
return (n & (n - 1)) == 0
}