-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaoq.go
58 lines (49 loc) · 1.41 KB
/
taoq.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
package taoq
import (
"fmt"
"runtime"
"sync/atomic"
)
// 队列大小,必须是2的N次方
// 最大=2^64-2
const queueSize uint64 = 4096
const indexMask uint64 = queueSize - 1
type TaoQ[T any] struct {
padding1 [8]uint64
lastCommittedIndex uint64
padding2 [8]uint64
nextFreeIndex uint64
padding3 [8]uint64
readerIndex uint64
padding4 [8]uint64
contents [queueSize]T
padding5 [8]uint64
}
// New 返回给定类型的新队列
func New[T any]() *TaoQ[T] {
return &TaoQ[T]{lastCommittedIndex: 0, nextFreeIndex: 1, readerIndex: 1}
}
func (self *TaoQ[T]) Write(value T) {
myIndex := atomic.AddUint64(&self.nextFreeIndex, 1) - 1
for myIndex > (atomic.LoadUint64(&self.readerIndex) + queueSize - 2) {
runtime.Gosched()
}
self.contents[myIndex&indexMask] = value
for !atomic.CompareAndSwapUint64(&self.lastCommittedIndex, myIndex-1, myIndex) {
runtime.Gosched()
}
}
func (self *TaoQ[T]) Read() T {
var myIndex = atomic.AddUint64(&self.readerIndex, 1) - 1
for myIndex > atomic.LoadUint64(&self.lastCommittedIndex) {
runtime.Gosched()
}
return self.contents[myIndex&indexMask]
}
func (self *TaoQ[T]) Dump() {
fmt.Printf("lastCommitted:%3d,nextFree:%3d,readerIndex:%3dcontents:", self.lastCommittedIndex, self.nextFreeIndex, self.readerIndex)
for index, value := range self.contents {
fmt.Printf("%5v:%5v", index, value)
}
fmt.Println()
}