forked from Monibuca/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring.go
More file actions
153 lines (132 loc) · 3.16 KB
/
ring.go
File metadata and controls
153 lines (132 loc) · 3.16 KB
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
package engine
import (
"container/ring"
"context"
"reflect"
"sync"
"sync/atomic"
"time"
)
type DataItem struct {
Timestamp time.Time
Sequence int
Value interface{}
}
// TODO: 池化,泛型
type LockItem struct {
DataItem
sync.RWMutex
}
type RingBuffer struct {
*ring.Ring
Size int
Flag *int32
context.Context
}
func (rb *RingBuffer) Init(ctx context.Context, n int) *RingBuffer {
var flag int32
if rb == nil {
rb = &RingBuffer{Context: ctx, Ring: ring.New(n), Flag: &flag}
} else {
rb.Ring = ring.New(n)
rb.Size = n
rb.Context = ctx
rb.Flag = &flag
}
for x := rb.Ring; x.Value == nil; x = x.Next() {
x.Value = new(LockItem)
}
rb.Current().Lock()
return rb
}
func (rb RingBuffer) Clone() *RingBuffer {
return &rb
}
func (rb RingBuffer) SubRing(rr *ring.Ring) *RingBuffer {
rb.Ring = rr
return &rb
}
func (rb *RingBuffer) CurrentValue() interface{} {
return rb.Current().Value
}
func (rb *RingBuffer) NextValue() interface{} {
return rb.Next().Value.(*LockItem).Value
}
func (rb *RingBuffer) Current() *LockItem {
return rb.Ring.Value.(*LockItem)
}
func (rb *RingBuffer) MoveNext() {
rb.Ring = rb.Next()
}
func (rb *RingBuffer) GetNext() *LockItem {
rb.MoveNext()
return rb.Current()
}
func (rb *RingBuffer) Read() interface{} {
current := rb.Current()
current.RLock()
defer current.RUnlock()
return current.Value
}
func (rb *RingBuffer) Step() {
last := rb.Current()
if atomic.CompareAndSwapInt32(rb.Flag, 0, 1) {
current := rb.GetNext()
current.Lock()
last.Unlock()
//Flag不为1代表被Dispose了,但尚未处理Done
if !atomic.CompareAndSwapInt32(rb.Flag, 1, 0) {
current.Unlock()
}
}
}
func (rb *RingBuffer) Write(value interface{}) {
last := rb.Current()
last.Value = value
if atomic.CompareAndSwapInt32(rb.Flag, 0, 1) {
current := rb.GetNext()
current.Lock()
last.Unlock()
//Flag不为1代表被Dispose了,但尚未处理Done
if !atomic.CompareAndSwapInt32(rb.Flag, 1, 0) {
current.Unlock()
}
}
}
func (rb *RingBuffer) Dispose() {
current := rb.Current()
if atomic.CompareAndSwapInt32(rb.Flag, 0, 2) {
current.Unlock()
} else if atomic.CompareAndSwapInt32(rb.Flag, 1, 2) {
//当前是1代表正在写入,此时变成2,但是Done的任务得交给NextW来处理
} else if atomic.CompareAndSwapInt32(rb.Flag, 0, 2) {
current.Unlock()
}
}
func (rb *RingBuffer) read() reflect.Value {
return reflect.ValueOf(rb.Read())
}
func (rb *RingBuffer) nextRead() reflect.Value {
rb.MoveNext()
return rb.read()
}
// ReadLoop 循环读取,采用了反射机制,不适用高性能场景
// handler入参可以传入回调函数或者channel
func (rb *RingBuffer) ReadLoop(handler interface{}) {
rb.ReadLoopConditional(handler, func() bool {
return rb.Err() == nil && *rb.Flag != 2
})
}
// goon判断函数用来判断是否继续读取,返回false将终止循环
func (rb *RingBuffer) ReadLoopConditional(handler interface{}, goon func() bool) {
switch t := reflect.ValueOf(handler); t.Kind() {
case reflect.Chan:
for v := rb.read(); goon(); v = rb.nextRead() {
t.Send(v)
}
case reflect.Func:
for args := []reflect.Value{rb.read()}; goon(); args[0] = rb.nextRead() {
t.Call(args)
}
}
}