From ef6a02719b0fa3bc3d0231ecd8e530eeaf0feab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Mon, 23 Oct 2023 11:50:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20listhead,=20=20=20=20=20ti?= =?UTF-8?q?mer=20=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95=20DeleteRunningTimer?= =?UTF-8?q?()=20=20=E5=88=A0=E9=99=A4=E5=BD=93=E5=89=8D=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- listhead.go | 32 +++++++++++++--------- timer.go | 75 ++++++++++++++++++++++++++++------------------------ timerlist.go | 15 ++++++----- 3 files changed, 68 insertions(+), 54 deletions(-) diff --git a/listhead.go b/listhead.go index 4eeb062..fd66a91 100644 --- a/listhead.go +++ b/listhead.go @@ -92,7 +92,7 @@ func (this *ListHead) DelInit() { func (this *ListHead) Size() int32 { num := int32(0) this.ForEach( - func (data interface{}) bool { + func(data interface{}) bool { num++ return true }) @@ -104,23 +104,29 @@ func (this *ListHead) isCursor() bool { } func (this *ListHead) ForEach(cb ListHead_Foreach_Func) { - 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) { + for pos := this.next; pos != this; pos = pos.next { + if !cb(pos.data) { 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.Init(nil) return l diff --git a/timer.go b/timer.go index b32e909..2896d46 100644 --- a/timer.go +++ b/timer.go @@ -9,36 +9,36 @@ const TVN_MASK = TVN_SIZE - 1 const TVR_MASK = TVR_SIZE - 1 const ( - TIMEOUT_TIMER = 0 + TIMEOUT_TIMER = 0 INTERVAL_TIMER = iota ) type XTimerDestoryHandleNode struct { entry ListHead - cb func() + cb func() } type XTimer struct { - freeTimerNum int32 + freeTimerNum int32 freeTimerList ListHead - runningTimer *XTimerList - timerTick int64 - getTickCount func (interface{}) int64 - context interface{} + runningTimer *XTimerList + timerTick int64 + getTickCount func(interface{}) int64 + context interface{} cacheTimerNum int32 - tv1 [TVR_SIZE]ListHead - tv2 [TVN_SIZE]ListHead - tv3 [TVN_SIZE]ListHead - tv4 [TVN_SIZE]ListHead - tv5 [TVN_SIZE]ListHead + tv1 [TVR_SIZE]ListHead + tv2 [TVN_SIZE]ListHead + tv3 [TVN_SIZE]ListHead + tv4 [TVN_SIZE]ListHead + tv5 [TVN_SIZE]ListHead } func (this *XTimer) Init( - getTickCount func (interface{}) int64, + getTickCount func(interface{}) int64, context interface{}, gcTime int32, - cacheTimerNum int32) { - initListHeadFunc := func (head *ListHead) { + cacheTimerNum int32) { + initListHeadFunc := func(head *ListHead) { head.Init(nil) } initListHeadFunc(&this.freeTimerList) @@ -70,7 +70,7 @@ func (this *XTimer) UnInit() { } func (this *XTimer) clear() { - freeTimerFunc := func (head *ListHead) { + freeTimerFunc := func(head *ListHead) { for !head.Empty() { timerList := head.FirstEntry().(*XTimerList) this.detachTimer(timerList) @@ -102,9 +102,9 @@ func (this *XTimer) Update() { index := uint32(this.timerTick & TVR_MASK) if index == 0 && - this.cascade(&this.tv2, this.getTimerIndex(0)) == 0 && - this.cascade(&this.tv3, this.getTimerIndex(1)) == 0 && - this.cascade(&this.tv4, this.getTimerIndex(2)) == 0 { + this.cascade(&this.tv2, this.getTimerIndex(0)) == 0 && + this.cascade(&this.tv3, this.getTimerIndex(1)) == 0 && + this.cascade(&this.tv4, this.getTimerIndex(2)) == 0 { this.cascade(&this.tv5, this.getTimerIndex(3)) } this.timerTick++ @@ -159,7 +159,7 @@ func (this *XTimer) SetInterval(expireTime int32, cb TimerCb) { 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) } @@ -175,7 +175,7 @@ func (this *XTimer) SetIntervalExWp(expireTime int32, cb TimerCb, attacher *XTim 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.initTimerList(this, TIMEOUT_TIMER, expireTime, cb) 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.initTimerList(this, INTERVAL_TIMER, expireTime, cb) if attacher != nil { @@ -212,7 +212,7 @@ func (this *XTimer) ModifyTimer(timerWp *XTimerWp, expireTime int32) { this.internalModifyTime(timerWp.timer, expireTime) } -func (this *XTimer) Delete(timerWp *XTimerWp) { +func (this *XTimer) Delete(timerWp *XTimerWp) { if timerWp.Expired() { panic("Xtimer Delete expired timer") } @@ -246,13 +246,13 @@ func (this *XTimer) GetIdleTime() int64 { return idleTime } -func (this *XTimer) detachTimer(timerList *XTimerList) { +func (this *XTimer) detachTimer(timerList *XTimerList) { if !timerList.entry.Empty() { timerList.entry.DelInit() } } -func (this *XTimer) addToFreeList(timerList *XTimerList) { +func (this *XTimer) addToFreeList(timerList *XTimerList) { timerList.reset() this.freeTimerList.AddTail(&timerList.entry) this.freeTimerNum++ @@ -265,7 +265,7 @@ func (this *XTimer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 { if !cascadeList.Empty() { pos := cascadeList.next next := pos.next - for pos != &cascadeList { + for pos != &cascadeList { this.internalAddTimer(pos.data.(*XTimerList)) pos = next next = pos.next @@ -290,21 +290,21 @@ func (this *XTimer) internalAddTimer(timerList *XTimerList) { } else if idx < (1 << (TVR_BITS + TVN_BITS)) { index = (uint32)((expires >> TVR_BITS) & TVN_MASK) vec = &this.tv2[index] - } else if idx < (1 << (TVR_BITS + 2 * TVN_BITS)) { - index = (uint32)((expires >> (TVR_BITS + 1 * TVN_BITS)) & TVN_MASK) + } else if idx < (1 << (TVR_BITS + 2*TVN_BITS)) { + index = (uint32)((expires >> (TVR_BITS + 1*TVN_BITS)) & TVN_MASK) vec = &this.tv3[index] - } else if idx < (1 << (TVR_BITS + 3 * TVN_BITS)) { - index = (uint32)((expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK) + } else if idx < (1 << (TVR_BITS + 3*TVN_BITS)) { + index = (uint32)((expires >> (TVR_BITS + 2*TVN_BITS)) & TVN_MASK) vec = &this.tv4[index] } 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.AddTail(&timerList.entry) } 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 { @@ -322,7 +322,7 @@ func (this *XTimer) newTimerList() *XTimerList { func (this *XTimer) gcTimerFunc(ev int32, args *Args) { if ev == TIMER_EXEC_EVENT { count := 0 - for ; !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum; { + for !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum { timerList := this.freeTimerList.FirstEntry().(*XTimerList) timerList.entry.DelInit() this.freeTimerNum-- @@ -372,7 +372,7 @@ func (this *XTimer) internalModifyTime(timer *XTimerList, expireTime int32) { this.internalAddTimer(timer) } -func (this *XTimer) clearAttacher(attacher* XTimerAttacher) { +func (this *XTimer) clearAttacher(attacher *XTimerAttacher) { var workList ListHead attacher.timers.ReplaceInit(&workList) for !workList.Empty() { @@ -380,3 +380,10 @@ func (this *XTimer) clearAttacher(attacher* XTimerAttacher) { this.internalDelete(timer, false, true) } } + +func (this *XTimer) DeleteRunningTimer() { + if this.runningTimer != nil { + this.internalDelete(this.runningTimer, false, true) + this.runningTimer = nil + } +} diff --git a/timerlist.go b/timerlist.go index e3c69b9..a3caca1 100644 --- a/timerlist.go +++ b/timerlist.go @@ -5,7 +5,7 @@ type XTimerWp struct { } type XTimerAttacher struct { - timer *XTimer + timer *XTimer timers ListHead } @@ -15,11 +15,11 @@ func (this *XTimerAttacher) ClearTimers() { type XTimerList struct { destoryHandleList ListHead - entry ListHead - attachEntry ListHead - timerType int8 - expireTime int32 - expires int64 + entry ListHead + attachEntry ListHead + timerType int8 + expireTime int32 + expires int64 cb TimerCb wp *XTimerWp @@ -32,7 +32,7 @@ func (this *XTimerWp) Expired() bool { func (this *XTimerList) initTimerList( timer interface{}, timerType int8, - expireTime int32, + expireTime int32, cb TimerCb) { this.timerType = timerType this.expireTime = expireTime @@ -52,6 +52,7 @@ func (this *XTimerList) reset() { func (this *XTimerList) init() { this.entry.Init(this) this.attachEntry.Init(this) + this.destoryHandleList.Init(nil) } func (this *XTimerList) Attach(timerAttacher *XTimerAttacher) {