-
Notifications
You must be signed in to change notification settings - Fork 0
/
esQueue.go
267 lines (226 loc) · 5.38 KB
/
esQueue.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package esqueue
import (
"fmt"
"runtime"
"sync/atomic"
)
// esQueue
type esCache[T any] struct {
putNo uint32
getNo uint32
value T
}
// lock free queue
type EsQueue[T any] struct {
capacity uint32
capMod uint32
putPos uint32
getPos uint32
cache []esCache[T]
zero T
}
func NewEsQueue[T any](capacity uint32) *EsQueue[T] {
q := new(EsQueue[T])
q.capacity = minQuantity(capacity)
q.capMod = q.capacity - 1
q.putPos = 0
q.getPos = 0
q.cache = make([]esCache[T], q.capacity)
for i := range q.cache {
cache := &q.cache[i]
cache.getNo = uint32(i)
cache.putNo = uint32(i)
}
cache := &q.cache[0]
cache.getNo = q.capacity
cache.putNo = q.capacity
return q
}
func (q *EsQueue[T]) String() string {
getPos := atomic.LoadUint32(&q.getPos)
putPos := atomic.LoadUint32(&q.putPos)
return fmt.Sprintf("ListQueue{capacity: %v, capMod: %v, putPos: %v, getPos: %v}",
q.capacity, q.capMod, putPos, getPos)
}
func (q *EsQueue[T]) Capaciity() uint32 {
return q.capacity
}
func (q *EsQueue[T]) Quantity() uint32 {
var putPos, getPos uint32
var quantity uint32
getPos = atomic.LoadUint32(&q.getPos)
putPos = atomic.LoadUint32(&q.putPos)
if putPos >= getPos {
quantity = putPos - getPos
} else {
quantity = q.capMod + (putPos - getPos)
}
return quantity
}
// put queue functions
func (q *EsQueue[T]) Put(val T) (ok bool, quantity uint32) {
var putPos, putPosNew, getPos, posCnt uint32
var cache *esCache[T]
capMod := q.capMod
getPos = atomic.LoadUint32(&q.getPos)
putPos = atomic.LoadUint32(&q.putPos)
if putPos >= getPos {
posCnt = putPos - getPos
} else {
posCnt = capMod + (putPos - getPos)
}
if posCnt >= capMod-1 {
runtime.Gosched()
return false, posCnt
}
putPosNew = putPos + 1
if !atomic.CompareAndSwapUint32(&q.putPos, putPos, putPosNew) {
runtime.Gosched()
return false, posCnt
}
cache = &q.cache[putPosNew&capMod]
for {
getNo := atomic.LoadUint32(&cache.getNo)
putNo := atomic.LoadUint32(&cache.putNo)
if putPosNew == putNo && getNo == putNo {
cache.value = val
atomic.AddUint32(&cache.putNo, q.capacity)
return true, posCnt + 1
} else {
runtime.Gosched()
}
}
}
// puts queue functions
func (q *EsQueue[T]) Puts(values []T) (puts, quantity uint32) {
var putPos, putPosNew, getPos, posCnt, putCnt uint32
capMod := q.capMod
getPos = atomic.LoadUint32(&q.getPos)
putPos = atomic.LoadUint32(&q.putPos)
if putPos >= getPos {
posCnt = putPos - getPos
} else {
posCnt = capMod + (putPos - getPos)
}
if posCnt >= capMod-1 {
runtime.Gosched()
return 0, posCnt
}
if capPuts, size := q.capacity-posCnt, uint32(len(values)); capPuts >= size {
putCnt = size
} else {
putCnt = capPuts
}
putPosNew = putPos + putCnt
if !atomic.CompareAndSwapUint32(&q.putPos, putPos, putPosNew) {
runtime.Gosched()
return 0, posCnt
}
for posNew, v := putPos+1, uint32(0); v < putCnt; posNew, v = posNew+1, v+1 {
var cache = &q.cache[posNew&capMod]
for {
getNo := atomic.LoadUint32(&cache.getNo)
putNo := atomic.LoadUint32(&cache.putNo)
if posNew == putNo && getNo == putNo {
cache.value = values[v]
atomic.AddUint32(&cache.putNo, q.capacity)
break
} else {
runtime.Gosched()
}
}
}
return putCnt, posCnt + putCnt
}
// get queue functions
func (q *EsQueue[T]) Get() (val T, ok bool, quantity uint32) {
var putPos, getPos, getPosNew, posCnt uint32
var cache *esCache[T]
capMod := q.capMod
putPos = atomic.LoadUint32(&q.putPos)
getPos = atomic.LoadUint32(&q.getPos)
if putPos >= getPos {
posCnt = putPos - getPos
} else {
posCnt = capMod + (putPos - getPos)
}
if posCnt < 1 {
runtime.Gosched()
return q.zero, false, posCnt
}
getPosNew = getPos + 1
if !atomic.CompareAndSwapUint32(&q.getPos, getPos, getPosNew) {
runtime.Gosched()
return q.zero, false, posCnt
}
cache = &q.cache[getPosNew&capMod]
for {
getNo := atomic.LoadUint32(&cache.getNo)
putNo := atomic.LoadUint32(&cache.putNo)
if getPosNew == getNo && getNo == putNo-q.capacity {
val = cache.value
cache.value = q.zero
atomic.AddUint32(&cache.getNo, q.capacity)
return val, true, posCnt - 1
} else {
runtime.Gosched()
}
}
}
// gets queue functions
func (q *EsQueue[T]) Gets(values []T) (gets, quantity uint32) {
var putPos, getPos, getPosNew, posCnt, getCnt uint32
capMod := q.capMod
putPos = atomic.LoadUint32(&q.putPos)
getPos = atomic.LoadUint32(&q.getPos)
if putPos >= getPos {
posCnt = putPos - getPos
} else {
posCnt = capMod + (putPos - getPos)
}
if posCnt < 1 {
runtime.Gosched()
return 0, posCnt
}
if size := uint32(len(values)); posCnt >= size {
getCnt = size
} else {
getCnt = posCnt
}
getPosNew = getPos + getCnt
if !atomic.CompareAndSwapUint32(&q.getPos, getPos, getPosNew) {
runtime.Gosched()
return 0, posCnt
}
for posNew, v := getPos+1, uint32(0); v < getCnt; posNew, v = posNew+1, v+1 {
var cache = &q.cache[posNew&capMod]
for {
getNo := atomic.LoadUint32(&cache.getNo)
putNo := atomic.LoadUint32(&cache.putNo)
if posNew == getNo && getNo == putNo-q.capacity {
values[v] = cache.value
cache.value = q.zero
getNo = atomic.AddUint32(&cache.getNo, q.capacity)
break
} else {
runtime.Gosched()
}
}
}
return getCnt, posCnt - getCnt
}
// round 到最近的2的倍数
func minQuantity(v uint32) uint32 {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++
return v
}
func Delay(z int) {
for x := z; x > 0; x-- {
}
}