-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconcurrentQueue.go
46 lines (39 loc) · 939 Bytes
/
concurrentQueue.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
package dbscan
import (
"sync/atomic"
"unsafe"
)
type queueNode struct {
Value uint
Next unsafe.Pointer
}
type ConcurrentQueue_InsertOnly struct {
Head unsafe.Pointer
Size uint64
}
func NewConcurrentQueue_InsertOnly() *ConcurrentQueue_InsertOnly {
var q = new(ConcurrentQueue_InsertOnly)
q.Head = unsafe.Pointer(new(queueNode))
return q
}
func (self *ConcurrentQueue_InsertOnly) Add(value uint) {
var node = new(queueNode)
node.Value = value
node.Next = self.Head
for atomic.CompareAndSwapPointer(&self.Head, node.Next, unsafe.Pointer(node)) == false {
node.Next = self.Head
}
atomic.AddUint64(&self.Size, 1)
}
func (self *ConcurrentQueue_InsertOnly) Slice() []uint {
var (
result = make([]uint, 0, self.Size)
node = (*queueNode)(self.Head)
)
// for node.Next != nil {
for i := uint64(0); i < self.Size; i += 1 {
result = append(result, node.Value)
node = (*queueNode)(node.Next)
}
return result
}