更新 listhead, timer 添加方法 DeleteRunningTimer() 删除当前运行定时器。

This commit is contained in:
殷勇 2023-10-23 11:50:27 +08:00
parent 72f4aa8132
commit ef6a02719b
3 changed files with 68 additions and 54 deletions

View File

@ -92,7 +92,7 @@ func (this *ListHead) DelInit() {
func (this *ListHead) Size() int32 { func (this *ListHead) Size() int32 {
num := int32(0) num := int32(0)
this.ForEach( this.ForEach(
func (data interface{}) bool { func(data interface{}) bool {
num++ num++
return true return true
}) })
@ -104,23 +104,29 @@ func (this *ListHead) isCursor() bool {
} }
func (this *ListHead) ForEach(cb ListHead_Foreach_Func) { func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
cursor := ListHead{} for pos := this.next; pos != this; pos = pos.next {
cursor.data = &cursor if !cb(pos.data) {
this.AddHead(&cursor)
defer cursor.Del()
for ; cursor.next != this; {
oldN := cursor.next
if !cursor.next.isCursor() && !cb(cursor.next.data) {
break break
} }
if cursor.next == oldN {
oldN.Del()
cursor.AddTail(oldN)
}
} }
/*
cursor := ListHead{}
cursor.data = &cursor
this.AddHead(&cursor)
defer cursor.Del()
for cursor.next != this {
oldN := cursor.next
if !cursor.next.isCursor() && !cb(cursor.next.data) {
break
}
if cursor.next == oldN {
oldN.Del()
cursor.AddTail(oldN)
}
}*/
} }
func NewListHead () *ListHead { func NewListHead() *ListHead {
l := new(ListHead) l := new(ListHead)
l.Init(nil) l.Init(nil)
return l return l

View File

@ -9,36 +9,36 @@ const TVN_MASK = TVN_SIZE - 1
const TVR_MASK = TVR_SIZE - 1 const TVR_MASK = TVR_SIZE - 1
const ( const (
TIMEOUT_TIMER = 0 TIMEOUT_TIMER = 0
INTERVAL_TIMER = iota INTERVAL_TIMER = iota
) )
type XTimerDestoryHandleNode struct { type XTimerDestoryHandleNode struct {
entry ListHead entry ListHead
cb func() cb func()
} }
type XTimer struct { type XTimer struct {
freeTimerNum int32 freeTimerNum int32
freeTimerList ListHead freeTimerList ListHead
runningTimer *XTimerList runningTimer *XTimerList
timerTick int64 timerTick int64
getTickCount func (interface{}) int64 getTickCount func(interface{}) int64
context interface{} context interface{}
cacheTimerNum int32 cacheTimerNum int32
tv1 [TVR_SIZE]ListHead tv1 [TVR_SIZE]ListHead
tv2 [TVN_SIZE]ListHead tv2 [TVN_SIZE]ListHead
tv3 [TVN_SIZE]ListHead tv3 [TVN_SIZE]ListHead
tv4 [TVN_SIZE]ListHead tv4 [TVN_SIZE]ListHead
tv5 [TVN_SIZE]ListHead tv5 [TVN_SIZE]ListHead
} }
func (this *XTimer) Init( func (this *XTimer) Init(
getTickCount func (interface{}) int64, getTickCount func(interface{}) int64,
context interface{}, context interface{},
gcTime int32, gcTime int32,
cacheTimerNum int32) { cacheTimerNum int32) {
initListHeadFunc := func (head *ListHead) { initListHeadFunc := func(head *ListHead) {
head.Init(nil) head.Init(nil)
} }
initListHeadFunc(&this.freeTimerList) initListHeadFunc(&this.freeTimerList)
@ -70,7 +70,7 @@ func (this *XTimer) UnInit() {
} }
func (this *XTimer) clear() { func (this *XTimer) clear() {
freeTimerFunc := func (head *ListHead) { freeTimerFunc := func(head *ListHead) {
for !head.Empty() { for !head.Empty() {
timerList := head.FirstEntry().(*XTimerList) timerList := head.FirstEntry().(*XTimerList)
this.detachTimer(timerList) this.detachTimer(timerList)
@ -102,9 +102,9 @@ func (this *XTimer) Update() {
index := uint32(this.timerTick & TVR_MASK) index := uint32(this.timerTick & TVR_MASK)
if index == 0 && if index == 0 &&
this.cascade(&this.tv2, this.getTimerIndex(0)) == 0 && this.cascade(&this.tv2, this.getTimerIndex(0)) == 0 &&
this.cascade(&this.tv3, this.getTimerIndex(1)) == 0 && this.cascade(&this.tv3, this.getTimerIndex(1)) == 0 &&
this.cascade(&this.tv4, this.getTimerIndex(2)) == 0 { this.cascade(&this.tv4, this.getTimerIndex(2)) == 0 {
this.cascade(&this.tv5, this.getTimerIndex(3)) this.cascade(&this.tv5, this.getTimerIndex(3))
} }
this.timerTick++ this.timerTick++
@ -159,7 +159,7 @@ func (this *XTimer) SetInterval(expireTime int32, cb TimerCb) {
this.internalSetInterval(expireTime, cb, nil, nil) this.internalSetInterval(expireTime, cb, nil, nil)
} }
func (this *XTimer) SetIntervalEx(expireTime int32, cb TimerCb, attacher *XTimerAttacher) { func (this *XTimer) SetIntervalEx(expireTime int32, cb TimerCb, attacher *XTimerAttacher) {
this.internalSetInterval(expireTime, cb, attacher, nil) this.internalSetInterval(expireTime, cb, attacher, nil)
} }
@ -175,7 +175,7 @@ func (this *XTimer) SetIntervalExWp(expireTime int32, cb TimerCb, attacher *XTim
return wp return wp
} }
func (this *XTimer) internalSetTimeout(expireTime int32, cb TimerCb, attacher *XTimerAttacher, wpPp**XTimerWp) { func (this *XTimer) internalSetTimeout(expireTime int32, cb TimerCb, attacher *XTimerAttacher, wpPp **XTimerWp) {
timer := this.newTimerList() timer := this.newTimerList()
timer.initTimerList(this, TIMEOUT_TIMER, expireTime, cb) timer.initTimerList(this, TIMEOUT_TIMER, expireTime, cb)
if attacher != nil { if attacher != nil {
@ -190,7 +190,7 @@ func (this *XTimer) internalSetTimeout(expireTime int32, cb TimerCb, attacher *X
} }
} }
func (this *XTimer) internalSetInterval(expireTime int32, cb TimerCb, attacher *XTimerAttacher, wpPp**XTimerWp) { func (this *XTimer) internalSetInterval(expireTime int32, cb TimerCb, attacher *XTimerAttacher, wpPp **XTimerWp) {
timer := this.newTimerList() timer := this.newTimerList()
timer.initTimerList(this, INTERVAL_TIMER, expireTime, cb) timer.initTimerList(this, INTERVAL_TIMER, expireTime, cb)
if attacher != nil { if attacher != nil {
@ -212,7 +212,7 @@ func (this *XTimer) ModifyTimer(timerWp *XTimerWp, expireTime int32) {
this.internalModifyTime(timerWp.timer, expireTime) this.internalModifyTime(timerWp.timer, expireTime)
} }
func (this *XTimer) Delete(timerWp *XTimerWp) { func (this *XTimer) Delete(timerWp *XTimerWp) {
if timerWp.Expired() { if timerWp.Expired() {
panic("Xtimer Delete expired timer") panic("Xtimer Delete expired timer")
} }
@ -246,13 +246,13 @@ func (this *XTimer) GetIdleTime() int64 {
return idleTime return idleTime
} }
func (this *XTimer) detachTimer(timerList *XTimerList) { func (this *XTimer) detachTimer(timerList *XTimerList) {
if !timerList.entry.Empty() { if !timerList.entry.Empty() {
timerList.entry.DelInit() timerList.entry.DelInit()
} }
} }
func (this *XTimer) addToFreeList(timerList *XTimerList) { func (this *XTimer) addToFreeList(timerList *XTimerList) {
timerList.reset() timerList.reset()
this.freeTimerList.AddTail(&timerList.entry) this.freeTimerList.AddTail(&timerList.entry)
this.freeTimerNum++ this.freeTimerNum++
@ -265,7 +265,7 @@ func (this *XTimer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
if !cascadeList.Empty() { if !cascadeList.Empty() {
pos := cascadeList.next pos := cascadeList.next
next := pos.next next := pos.next
for pos != &cascadeList { for pos != &cascadeList {
this.internalAddTimer(pos.data.(*XTimerList)) this.internalAddTimer(pos.data.(*XTimerList))
pos = next pos = next
next = pos.next next = pos.next
@ -290,21 +290,21 @@ func (this *XTimer) internalAddTimer(timerList *XTimerList) {
} else if idx < (1 << (TVR_BITS + TVN_BITS)) { } else if idx < (1 << (TVR_BITS + TVN_BITS)) {
index = (uint32)((expires >> TVR_BITS) & TVN_MASK) index = (uint32)((expires >> TVR_BITS) & TVN_MASK)
vec = &this.tv2[index] vec = &this.tv2[index]
} else if idx < (1 << (TVR_BITS + 2 * TVN_BITS)) { } else if idx < (1 << (TVR_BITS + 2*TVN_BITS)) {
index = (uint32)((expires >> (TVR_BITS + 1 * TVN_BITS)) & TVN_MASK) index = (uint32)((expires >> (TVR_BITS + 1*TVN_BITS)) & TVN_MASK)
vec = &this.tv3[index] vec = &this.tv3[index]
} else if idx < (1 << (TVR_BITS + 3 * TVN_BITS)) { } else if idx < (1 << (TVR_BITS + 3*TVN_BITS)) {
index = (uint32)((expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK) index = (uint32)((expires >> (TVR_BITS + 2*TVN_BITS)) & TVN_MASK)
vec = &this.tv4[index] vec = &this.tv4[index]
} else { } else {
index = (uint32)((expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK) index = (uint32)((expires >> (TVR_BITS + 3*TVN_BITS)) & TVN_MASK)
vec = &this.tv5[index] vec = &this.tv5[index]
} }
vec.AddTail(&timerList.entry) vec.AddTail(&timerList.entry)
} }
func (this *XTimer) getTimerIndex(index uint32) uint32 { func (this *XTimer) getTimerIndex(index uint32) uint32 {
return (uint32)((this.timerTick >> (TVR_BITS + index * TVN_BITS)) & TVN_MASK); return (uint32)((this.timerTick >> (TVR_BITS + index*TVN_BITS)) & TVN_MASK)
} }
func (this *XTimer) newTimerList() *XTimerList { func (this *XTimer) newTimerList() *XTimerList {
@ -322,7 +322,7 @@ func (this *XTimer) newTimerList() *XTimerList {
func (this *XTimer) gcTimerFunc(ev int32, args *Args) { func (this *XTimer) gcTimerFunc(ev int32, args *Args) {
if ev == TIMER_EXEC_EVENT { if ev == TIMER_EXEC_EVENT {
count := 0 count := 0
for ; !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum; { for !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum {
timerList := this.freeTimerList.FirstEntry().(*XTimerList) timerList := this.freeTimerList.FirstEntry().(*XTimerList)
timerList.entry.DelInit() timerList.entry.DelInit()
this.freeTimerNum-- this.freeTimerNum--
@ -372,7 +372,7 @@ func (this *XTimer) internalModifyTime(timer *XTimerList, expireTime int32) {
this.internalAddTimer(timer) this.internalAddTimer(timer)
} }
func (this *XTimer) clearAttacher(attacher* XTimerAttacher) { func (this *XTimer) clearAttacher(attacher *XTimerAttacher) {
var workList ListHead var workList ListHead
attacher.timers.ReplaceInit(&workList) attacher.timers.ReplaceInit(&workList)
for !workList.Empty() { for !workList.Empty() {
@ -380,3 +380,10 @@ func (this *XTimer) clearAttacher(attacher* XTimerAttacher) {
this.internalDelete(timer, false, true) this.internalDelete(timer, false, true)
} }
} }
func (this *XTimer) DeleteRunningTimer() {
if this.runningTimer != nil {
this.internalDelete(this.runningTimer, false, true)
this.runningTimer = nil
}
}

View File

@ -5,7 +5,7 @@ type XTimerWp struct {
} }
type XTimerAttacher struct { type XTimerAttacher struct {
timer *XTimer timer *XTimer
timers ListHead timers ListHead
} }
@ -15,11 +15,11 @@ func (this *XTimerAttacher) ClearTimers() {
type XTimerList struct { type XTimerList struct {
destoryHandleList ListHead destoryHandleList ListHead
entry ListHead entry ListHead
attachEntry ListHead attachEntry ListHead
timerType int8 timerType int8
expireTime int32 expireTime int32
expires int64 expires int64
cb TimerCb cb TimerCb
wp *XTimerWp wp *XTimerWp
@ -32,7 +32,7 @@ func (this *XTimerWp) Expired() bool {
func (this *XTimerList) initTimerList( func (this *XTimerList) initTimerList(
timer interface{}, timer interface{},
timerType int8, timerType int8,
expireTime int32, expireTime int32,
cb TimerCb) { cb TimerCb) {
this.timerType = timerType this.timerType = timerType
this.expireTime = expireTime this.expireTime = expireTime
@ -52,6 +52,7 @@ func (this *XTimerList) reset() {
func (this *XTimerList) init() { func (this *XTimerList) init() {
this.entry.Init(this) this.entry.Init(this)
this.attachEntry.Init(this) this.attachEntry.Init(this)
this.destoryHandleList.Init(nil)
} }
func (this *XTimerList) Attach(timerAttacher *XTimerAttacher) { func (this *XTimerList) Attach(timerAttacher *XTimerAttacher) {