-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.go
266 lines (230 loc) · 6.03 KB
/
ring.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
package goring
/*
implement: from dpdk ring
atomic cas,
*/
import (
"fmt"
"runtime"
"sync/atomic"
"unsafe"
"github.com/jursonmo/goring/internel/cpu" //copy from golang runtime
)
type pad struct {
x [cpu.CacheLinePadSize]byte
}
type ringheadtail struct {
head uint32 /**< Prod/consumer head. */
//pad [cpu.CacheLinePadSize - unsafe.Sizeof(uint32(0))%cpu.CacheLinePadSize]byte
tail uint32 /**< Prod/consumer tail. */
single bool /**< True if single prod/cons */
}
type ringAttr struct {
size uint32 // PowerOfTwo
mask uint32 // for &
cap uint32 //
}
type Ring struct {
ringAttr
//pad0 pad
_ [cpu.CacheLinePadSize - unsafe.Sizeof(ringAttr{})%cpu.CacheLinePadSize]byte
prod ringheadtail
_ [cpu.CacheLinePadSize - unsafe.Sizeof(ringheadtail{})%cpu.CacheLinePadSize]byte
cons ringheadtail
_ [cpu.CacheLinePadSize - unsafe.Sizeof(ringheadtail{})%cpu.CacheLinePadSize]byte
cache []interface{}
// 常用的字段放结构体前面, 不常用的字段放在结构体的后面
name string
}
type RingQueueBehavior byte
const (
ISENQUEUE bool = true
RING_QUEUE_FIXED RingQueueBehavior = 0 /* Enq/Deq a fixed number of items from a ring */
RING_QUEUE_VARIABLE RingQueueBehavior = 1 /* Enq/Deq as many items as possible from ring */
)
func init() {
fmt.Printf("goring env: GOMAXPROCS:%d, cpu.CacheLinePadSize:%d, Sizeof(ringAttr{}):%d\n", runtime.GOMAXPROCS(0),
cpu.CacheLinePadSize, unsafe.Sizeof(ringAttr{}))
}
func IsPowerOfTwo(n int) bool {
return n&(n-1) == 0
}
func NewRing(name string, n int, isSP, isSC bool) (*Ring, error) {
if !IsPowerOfTwo(n) {
return nil, fmt.Errorf("n:%d is not PowerOfTwo", n)
}
nn := uint32(n)
r := &Ring{}
r.cap = nn
r.size = nn
r.mask = nn - 1
r.prod.single = isSP
r.cons.single = isSC
r.cache = make([]interface{}, n)
r.name = name
return r, nil
}
func (r *Ring) String() string {
return fmt.Sprintf("name:%s, size:%d, prod:[%s], cons:[%s]", r.name, r.size, &r.prod, &r.cons)
}
func (ht *ringheadtail) String() string {
return fmt.Sprintf("h-%d, t-%d, s-%v", ht.head, ht.tail, ht.single)
}
func (r *Ring) EnqueueBulk(obj interface{}, n int) int {
return int(r.enqueue(obj, uint32(n), r.prod.single, RING_QUEUE_FIXED))
}
func (r *Ring) EnqueueOne(obj interface{}) int {
return int(r.enqueue(obj, 1, r.prod.single, RING_QUEUE_FIXED))
}
func (r *Ring) Enqueue(obj interface{}) int {
return int(r.enqueue(obj, 1, r.prod.single, RING_QUEUE_FIXED))
}
func (r *Ring) enqueue(obj interface{}, n uint32, isSP bool, bbehavior RingQueueBehavior) uint32 {
var prodHead, prodNext uint32
var objs []interface{}
var isbulk bool
if n > 1 {
objs, isbulk = obj.([]interface{})
if !isbulk {
return 0
}
}
n = r.ringMoveProdHead(&prodHead, &prodNext, n, isSP, bbehavior)
if n == 0 {
return 0
}
//copy to ring
if isbulk {
start := prodHead & r.mask
if start+n < r.size {
copyn := copy(r.cache[start:], objs[:n])
if copyn != int(n) {
panic("copyn != int(n)")
}
} else {
copy1 := copy(r.cache[start:r.size], objs)
copy2 := copy(r.cache[:start+n-r.size], objs[copy1:]) //objs[r.size-start:]
if copy1+copy2 != int(n) {
panic("copy1+copy2 != int(n) ")
}
}
} else {
r.cache[prodHead&r.mask] = obj
}
updateTail(&r.prod, prodHead, prodNext, isSP, ISENQUEUE)
return n
}
func (r *Ring) ringMoveProdHead(oldHead, newHead *uint32, n uint32, isSP bool, behavior RingQueueBehavior) uint32 {
max := n
cap := r.cap
free := uint32(0)
succ := false
for {
n = max //reset to initial value every loop
*oldHead = atomic.LoadUint32(&r.prod.head)
free = cap + atomic.LoadUint32(&r.cons.tail) - *oldHead
if free < n {
if behavior == RING_QUEUE_FIXED {
n = 0
} else {
n = free
}
}
if n == 0 {
return 0
}
*newHead = *oldHead + n
if isSP {
r.prod.head = *newHead
succ = true
} else {
succ = atomic.CompareAndSwapUint32(&r.prod.head, *oldHead, *newHead)
}
if succ {
break
}
}
return n
}
func (r *Ring) Dequeue(obj interface{}, n int) []interface{} {
return r.dequeue(obj, uint32(n), r.cons.single, RING_QUEUE_FIXED)
}
func (r *Ring) DequeueVariable(obj interface{}, n int) []interface{} {
return r.dequeue(obj, uint32(n), r.cons.single, RING_QUEUE_VARIABLE)
}
//var objs [128]interface{}
func (r *Ring) dequeue(obj interface{}, n uint32, isSC bool,
behavior RingQueueBehavior) (objs []interface{}) {
var consHead, consNext uint32
var isbulk bool
//if n > 1 {
objs, isbulk = obj.([]interface{}) //must be []interface{}
if !isbulk {
return
}
//}
n = r.ringMoveConsHead(&consHead, &consNext, n, isSC, behavior)
if n == 0 {
objs = objs[:0]
return
}
// get ring data to objs
//objs = make([]interface{}, n) // here make a low performent
start := consHead & r.mask
if start+n < r.size {
copy(objs[:], r.cache[start:start+n])
} else {
copyn := copy(objs[:], r.cache[start:r.size])
copy(objs[copyn:], r.cache[:start+n-r.size])
}
updateTail(&r.cons, consHead, consNext, isSC, false)
objs = objs[:n]
return
}
func (r *Ring) ringMoveConsHead(oldHead, newHead *uint32, n uint32,
isSC bool, behavior RingQueueBehavior) uint32 {
max := n
entries := uint32(0)
succ := false
for {
n = max
*oldHead = atomic.LoadUint32(&r.cons.head)
entries = atomic.LoadUint32(&r.prod.tail) - *oldHead
if entries < n {
if behavior == RING_QUEUE_FIXED {
n = 0
} else {
n = entries
}
}
if n == 0 {
return 0
}
*newHead = *oldHead + n
if isSC {
atomic.StoreUint32(&r.cons.head, *newHead)
succ = true
} else {
succ = atomic.CompareAndSwapUint32(&r.cons.head, *oldHead, *newHead)
}
if succ {
break
}
}
return n
}
func updateTail(ht *ringheadtail, oldVal, newVal uint32, isSingle bool, isEnqueue bool) {
if !isSingle {
for {
//多个生产者, 需要等前面的生产者按次序更新完tail值后,当前的生产者才能更新tail。
//这个次序就是多个生产者同时cas去抢ringheadtail.head 的次序
//同理,多个消费者也一样
if atomic.LoadUint32(&ht.tail) != oldVal {
runtime.Gosched()
} else {
break
}
}
}
atomic.StoreUint32(&ht.tail, newVal)
}