更新 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 {
num := int32(0)
this.ForEach(
func (data interface{}) bool {
func(data interface{}) bool {
num++
return true
})
@ -104,11 +104,17 @@ func (this *ListHead) isCursor() bool {
}
func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
for pos := this.next; pos != this; pos = pos.next {
if !cb(pos.data) {
break
}
}
/*
cursor := ListHead{}
cursor.data = &cursor
this.AddHead(&cursor)
defer cursor.Del()
for ; cursor.next != this; {
for cursor.next != this {
oldN := cursor.next
if !cursor.next.isCursor() && !cb(cursor.next.data) {
break
@ -117,10 +123,10 @@ func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
oldN.Del()
cursor.AddTail(oldN)
}
}
}*/
}
func NewListHead () *ListHead {
func NewListHead() *ListHead {
l := new(ListHead)
l.Init(nil)
return l

View File

@ -23,7 +23,7 @@ type XTimer struct {
freeTimerList ListHead
runningTimer *XTimerList
timerTick int64
getTickCount func (interface{}) int64
getTickCount func(interface{}) int64
context interface{}
cacheTimerNum int32
tv1 [TVR_SIZE]ListHead
@ -34,11 +34,11 @@ type XTimer struct {
}
func (this *XTimer) Init(
getTickCount func (interface{}) int64,
getTickCount func(interface{}) int64,
context interface{},
gcTime int32,
cacheTimerNum int32) {
initListHeadFunc := func (head *ListHead) {
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)
@ -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 {
@ -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
}
}

View File

@ -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) {