-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathmpsc.go
211 lines (178 loc) · 5.09 KB
/
mpsc.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
// this is a lock-free implementation of MPSC queue (Multiple Producers Single Consumer)
package lib
import (
"math"
"sync/atomic"
"unsafe"
)
type queueMPSC struct {
lock uint32
head *itemMPSC
tail *itemMPSC
length int64
}
type queueLimitMPSC struct {
lock uint32
head *itemMPSC
tail *itemMPSC
length int64
limit int64
flush bool
}
type QueueMPSC interface {
Push(value any) bool
Pop() (any, bool)
Item() ItemMPSC
// Len returns the number of items in the queue
Len() int64
// Size returns the limit for the queue. -1 - for unlimited
Size() int64
Lock() bool
Unlock() bool
}
func NewQueueMPSC() QueueMPSC {
emptyItem := &itemMPSC{}
return &queueMPSC{
head: emptyItem,
tail: emptyItem,
}
}
// NewQueueLimitMPSC creates MPSC queue with limited length. Enabling "flush" options
// makes this queue flush out the tail item if the limit has been reached.
// Warning: enabled "flush" option also makes this queue unusable
// for the concurrent environment
func NewQueueLimitMPSC(limit int64, flush bool) QueueMPSC {
if limit < 1 {
limit = math.MaxInt64
}
emptyItem := &itemMPSC{}
return &queueLimitMPSC{
limit: limit,
flush: flush,
head: emptyItem,
tail: emptyItem,
}
}
type ItemMPSC interface {
Next() ItemMPSC
Value() any
Clear()
}
type itemMPSC struct {
value any
next *itemMPSC
}
// Push place the given value in the queue head (FIFO). Returns always true
func (q *queueMPSC) Push(value any) bool {
i := &itemMPSC{
value: value,
}
atomic.AddInt64(&q.length, 1)
old_head := (*itemMPSC)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&q.head)), unsafe.Pointer(i)))
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&old_head.next)), unsafe.Pointer(i))
return true
}
// Push place the given value in the queue head (FIFO). Returns false if exceeded the limit
func (q *queueLimitMPSC) Push(value any) bool {
if q.Len()+1 > q.limit {
if q.flush == false {
return false
}
// flush one item to keep the length within the limit
q.Pop()
}
atomic.AddInt64(&q.length, 1)
i := &itemMPSC{
value: value,
}
old_head := (*itemMPSC)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&q.head)), unsafe.Pointer(i)))
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&old_head.next)), unsafe.Pointer(i))
return true
}
// Pop takes the item from the queue tail. Returns false if the queue is empty. Can be used in a single consumer (goroutine) only.
func (q *queueMPSC) Pop() (any, bool) {
tail_next := (*itemMPSC)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&q.tail.next))))
if tail_next == nil {
return nil, false
}
value := tail_next.value
tail_next.value = nil // let the GC free this item
// TODO a little race condition happens with node.process.go:1500 within the running a new goroutine
// to handle process mailbox (invoking Item() method).
// nothing serios, but we should use atomic operation here to set the q.tail
q.tail = tail_next
atomic.AddInt64(&q.length, -1)
return value, true
}
// Pop takes the item from the queue tail. Returns false if the queue is empty. Can be used in a single consumer (goroutine) only.
func (q *queueLimitMPSC) Pop() (any, bool) {
tail_next := (*itemMPSC)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&q.tail.next))))
if tail_next == nil {
return nil, false
}
value := tail_next.value
tail_next.value = nil // let the GC free this item
q.tail = tail_next
atomic.AddInt64(&q.length, -1)
return value, true
}
func (q *queueMPSC) Len() int64 {
return atomic.LoadInt64(&q.length)
}
func (q *queueMPSC) Size() int64 {
return -1 // unlimited
}
func (q *queueMPSC) Lock() bool {
return atomic.SwapUint32(&q.lock, 1) == 0
}
func (q *queueMPSC) Unlock() bool {
return atomic.SwapUint32(&q.lock, 0) == 1
}
// Len returns queue length
func (q *queueLimitMPSC) Len() int64 {
return atomic.LoadInt64(&q.length)
}
func (q *queueLimitMPSC) Size() int64 {
return q.limit
}
func (q *queueLimitMPSC) Lock() bool {
return atomic.SwapUint32(&q.lock, 1) == 0
}
func (q *queueLimitMPSC) Unlock() bool {
return atomic.SwapUint32(&q.lock, 0) == 1
}
// Item returns the tail item of the queue. Returns nil if queue is empty.
func (q *queueMPSC) Item() ItemMPSC {
item := (*itemMPSC)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&q.tail.next))))
if item == nil {
return nil
}
return item
}
// Item returns the tail item of the queue. Returns nil if queue is empty.
func (q *queueLimitMPSC) Item() ItemMPSC {
item := (*itemMPSC)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&q.tail.next))))
if item == nil {
return nil
}
return item
}
//
// ItemMPSC interface implementation
//
// Next provides walking through the queue. Returns nil if the last item is reached.
func (i *itemMPSC) Next() ItemMPSC {
next := (*itemMPSC)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&i.next))))
if next == nil {
return nil
}
return next
}
// Value returns stored value of the queue item
func (i *itemMPSC) Value() any {
return i.value
}
// Clear sets the value to nil. It doesn't remove this item from the queue. Can be used in a signle consumer (goroutine) only.
func (i *itemMPSC) Clear() {
i.value = nil
}