/
lfqueue.go
61 lines (55 loc) · 1.05 KB
/
lfqueue.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
package lfqueue
import (
"sync/atomic"
"unsafe"
)
type node struct {
val interface{}
nxt unsafe.Pointer
}
type Q struct {
head unsafe.Pointer
tail unsafe.Pointer
}
func New() (q *Q) {
q = new(Q)
n := unsafe.Pointer(new(node))
q.head = n
q.tail = n
return
}
func (q *Q) enq(val interface{}) {
var t, n unsafe.Pointer
n = unsafe.Pointer(&node{val: val, nxt: nil})
for {
t = q.tail
nxt := ((*node)(t)).nxt
if nxt != nil {
atomic.CompareAndSwapPointer(&q.tail, t, nxt)
} else if atomic.CompareAndSwapPointer(&((*node)(t)).nxt, nil, n) {
break
}
}
atomic.CompareAndSwapPointer(&q.tail, t, n)
}
func (q *Q) deq() (val interface{}, success bool) {
var h, t, n unsafe.Pointer
for {
h = q.head
t = q.tail
n = ((*node)(h)).nxt
if h == t {
if n == nil {
return nil, false
} else {
atomic.CompareAndSwapPointer(&q.tail, t, n)
}
} else {
val = ((*node)(n)).val // Enq(...) write to val may not be visible
if atomic.CompareAndSwapPointer(&q.head, h, n) {
return val, true
}
}
}
panic("Unreachable")
}