-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastchan.go
236 lines (206 loc) · 4.87 KB
/
fastchan.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
package fastchan
import (
"runtime"
"sync/atomic"
"unsafe"
)
func roundUp(v uint32) uint32 {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v |= v >> 32
v++
return v
}
type node struct {
position uint64
data interface{}
}
const (
nodePtrSize = unsafe.Sizeof(&node{})
maskHigh uint64 = 1 << 63
maskLow uint64 = (1 << 63) - 1
)
type FastChan struct {
_padding0 [8]uint64
queue uint64
_padding1 [8]uint64
dequeue uint64
_padding2 [8]uint64
mask uint64
closed uint32
_padding3 [8]uint64
nodes []*node
nodePtr uintptr
_padding4 [8]uint64
}
func (fc *FastChan) init(size uint32) {
size = roundUp(size)
fc.nodes = make([]*node, size)
fc.nodePtr = uintptr(unsafe.Pointer(&fc.nodes[0]))
for i := uint32(0); i < size; i++ {
fc.nodes[i] = &node{position: uint64(i)}
}
fc.mask = uint64(size - 1) // so we don't have to do this with every put/get operation
}
// Put adds the provided item to the queue. If the queue is full, this
// call will block until an item is added to the queue or Close is called
// on the queue. An error will be returned if the queue is disposed.
func (fc *FastChan) Put(item interface{}) {
fc.put(item, false)
}
// Offer adds the provided item to the queue if there is space. If the queue
// is full, this call will return false. An error will be returned if the
// queue is disposed.
func (fc *FastChan) TryPut(item interface{}) bool {
return fc.put(item, true)
}
func (fc *FastChan) put(item interface{}, offer bool) bool {
var (
n *node
pos uint64
cPos uint64
full bool
itemPos uint64
)
for {
if atomic.LoadUint32(&fc.closed) == 1 {
panic("Put on closed fastchan")
}
pos = fc.queue
// The same as n = fc.nodes[pos&fc.mask] but without bounds check
n = *(**node)(unsafe.Pointer(fc.nodePtr + uintptr(pos&fc.mask)*nodePtrSize))
cPos = n.position
full = cPos&maskHigh == maskHigh
itemPos = cPos & maskLow
if !full && itemPos == pos && atomic.CompareAndSwapUint64(&fc.queue, pos, pos+1) {
break
}
if offer {
return false
}
runtime.Gosched()
}
n.data = item
atomic.StoreUint64(&n.position, cPos|maskHigh)
return true
}
func (fc *FastChan) Get() interface{} {
var (
n *node
pos uint64
cPos uint64
full bool
itemPos uint64
)
for {
if atomic.LoadUint32(&fc.closed) == 1 {
panic("Get on closed fastchan")
}
pos = fc.dequeue
// The same as n = fc.nodes[pos&fc.mask] but without bounds check
n = *(**node)(unsafe.Pointer(fc.nodePtr + uintptr(pos&fc.mask)*nodePtrSize))
cPos = n.position
full = cPos&maskHigh == maskHigh
itemPos = cPos & maskLow
if full && itemPos == pos && atomic.CompareAndSwapUint64(&fc.dequeue, pos, pos+1) {
break
}
runtime.Gosched()
}
data := n.data
n.data = nil
atomic.StoreUint64(&n.position, (pos+fc.mask+1)&maskLow)
return data
}
// Len returns the number of items in the queue.
func (fc *FastChan) Len() uint32 {
return uint32(fc.queue - fc.dequeue)
}
// Cap returns the capacity of this ring buffer.
func (fc *FastChan) Cap() uint64 {
return uint64(len(fc.nodes))
}
// Close will dispose of this queue and free any blocked threads
// in the Put and/or Get methods. Calling those methods on a disposed
// queue will return an error.
func (fc *FastChan) Close() {
fc.closed = 1
}
// IsClosed will return a bool indicating if this queue has been
// closed.
func (fc *FastChan) IsClosed() bool {
return fc.closed == 1
}
func New(size uint32) *FastChan {
rb := &FastChan{}
rb.init(size)
return rb
}
// Michael-Scott unbounded queue
// http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf?
type msqueue struct {
head *msnode
tail *msnode
}
type msnode struct {
val int
next *msnode
}
func newMSQueue() *msqueue {
item := &msnode{}
return &msqueue{
head: item,
tail: item,
}
}
func (q *msqueue) push(val int) {
// TODO: use thread-local struct cache
newTail := &msnode{val: val}
for {
tail := q.tail
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&tail.next)),
nil,
unsafe.Pointer(newTail)) {
atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&q.tail)),
unsafe.Pointer(tail),
unsafe.Pointer(newTail))
return
} else {
atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&q.tail)),
unsafe.Pointer(tail),
unsafe.Pointer(tail.next))
}
}
}
func (q *msqueue) pop() (int, bool) {
for {
head := q.head
tail := q.tail
nextHead := head.next
if head == tail {
if nextHead == nil {
return 0, false
} else {
atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&q.tail)),
unsafe.Pointer(tail),
unsafe.Pointer(tail.next))
}
} else {
res := nextHead.val
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&q.head)),
unsafe.Pointer(head),
unsafe.Pointer(nextHead)) {
return res, true
}
}
}
}