Files
engine/common/ring_lock.go
2022-02-02 10:39:39 +08:00

128 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import (
"context"
"reflect"
"sync"
"sync/atomic"
)
type LockFrame[T any] struct {
DataFrame[T]
sync.RWMutex
}
type LockRing[T any] struct {
RingBuffer[LockFrame[T]]
ctx context.Context
Flag *int32
}
func (lr *LockRing[T]) Init(ctx context.Context, n int) *LockRing[T] {
var flag int32
if lr == nil {
lr = &LockRing[T]{}
}
lr.ctx = ctx
lr.RingBuffer.Init(n)
lr.Flag = &flag
lr.Value.Lock()
return lr
}
func (rb *LockRing[T]) Read() *DataFrame[T] {
current := rb.Value
current.RLock()
defer current.RUnlock()
return &current.DataFrame
}
func (rb *LockRing[T]) Step() {
last := &rb.Value
if atomic.CompareAndSwapInt32(rb.Flag, 0, 1) {
current := rb.MoveNext()
current.Lock()
last.Unlock()
//Flag不为1代表被Dispose了但尚未处理Done
if !atomic.CompareAndSwapInt32(rb.Flag, 1, 0) {
current.Unlock()
}
}
}
func (rb *LockRing[T]) Write(value T) {
last := &rb.Value
last.Value = value
if atomic.CompareAndSwapInt32(rb.Flag, 0, 1) {
current := rb.MoveNext()
current.Lock()
last.Unlock()
//Flag不为1代表被Dispose了但尚未处理Done
if !atomic.CompareAndSwapInt32(rb.Flag, 1, 0) {
current.Unlock()
}
}
}
func (rb *LockRing[T]) Dispose() {
current := &rb.Value
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 *LockRing[T]) read() reflect.Value {
return reflect.ValueOf(rb.Read())
}
func (rb *LockRing[T]) nextRead() reflect.Value {
rb.MoveNext()
return rb.read()
}
func (rb *LockRing[T]) condition() bool {
return rb.ctx.Err() == nil && *rb.Flag != 2
}
// ReadLoop 循环读取,采用了反射机制,不适用高性能场景
// handler入参可以传入回调函数或者channel
func (rb *LockRing[T]) ReadLoop(handler interface{}, async bool) {
if async {
rb.ReadLoopConditionalGo(handler, rb.condition)
} else {
rb.ReadLoopConditional(handler, rb.condition)
}
}
// goon判断函数用来判断是否继续读取,返回false将终止循环
func (rb *LockRing[T]) 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)
}
}
}
// goon判断函数用来判断是否继续读取,返回false将终止循环
func (r *LockRing[T]) ReadLoopConditionalGo(handler interface{}, goon func() bool) {
switch t := reflect.ValueOf(handler); t.Kind() {
case reflect.Chan:
for v := r.read(); goon(); v = r.nextRead() {
t.Send(v)
}
case reflect.Func:
for args := []reflect.Value{r.read()}; goon(); args[0] = r.nextRead() {
go t.Call(args)
}
}
}