更新 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,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

View File

@ -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
}
}

View File

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