This commit is contained in:
aozhiwei 2020-09-08 17:00:41 +08:00
parent 7ab75e421e
commit a3ab521064
4 changed files with 40 additions and 18 deletions

View File

@ -20,7 +20,7 @@ type Timer struct {
runningTimer *TimerList runningTimer *TimerList
timerTick int64 timerTick int64
getTickCount func (interface{}) int64 getTickCount func (interface{}) int64
getFixedTimerExpires func (interface{}, int32, int32, int64) int64 getFixedTimerExpires func (interface{}, bool, int32, int64) int64
context interface{} context interface{}
cacheTimerNum int32 cacheTimerNum int32
tv1 [TVR_SIZE]ListHead tv1 [TVR_SIZE]ListHead
@ -32,7 +32,7 @@ type Timer struct {
func (this *Timer) Init( func (this *Timer) Init(
getTickCount func (interface{}) int64, getTickCount func (interface{}) int64,
getFixedTimerExpires func (interface{}, int32, int32, int64) int64, getFixedTimerExpires func (interface{}, bool, int32, int64) int64,
context interface{}, context interface{},
gcTime int32, gcTime int32,
cacheTimerNum int32) { cacheTimerNum int32) {
@ -108,15 +108,9 @@ func (this *Timer) Update() {
if this.runningTimer != nil { if this.runningTimer != nil {
switch this.runningTimer.timerType { switch this.runningTimer.timerType {
case REPEAT_TIMER, FIXED_TIMER: case REPEAT_TIMER, FIXED_TIMER:
if timerList.timerType == FIXED_TIMER { this.modifyTimerEx(timerList, timerList.milliSeconds, false)
timerList.fixedTimierExecuteTimes++
}
this.ModifyTimer(timerList, timerList.milliSeconds)
case DEADLINE_TIMER: case DEADLINE_TIMER:
this.detachTimer(timerList) this.detachTimer(timerList)
if timerList.attachEntry.Empty() {
timerList.attachEntry.DelInit()
}
this.addToFreeList(timerList) this.addToFreeList(timerList)
} }
} }
@ -198,14 +192,18 @@ func (this *Timer) AddFixedTimerEx(
return timerList return timerList
} }
func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) { func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) {
this.modifyTimerEx(timerList, milliSeconds, true)
}
func (this *Timer) modifyTimerEx(timerList *TimerList, milliSeconds int32, isFirstAdd bool) {
this.detachTimer(timerList) this.detachTimer(timerList)
timerList.milliSeconds = milliSeconds timerList.milliSeconds = milliSeconds
if timerList.timerType == FIXED_TIMER { if timerList.timerType == FIXED_TIMER {
tick := this.getTickCount(this.context) tick := this.getTickCount(this.context)
timerList.expires = this.getFixedTimerExpires( timerList.expires = this.getFixedTimerExpires(
this.context, this.context,
timerList.fixedTimierExecuteTimes, isFirstAdd,
milliSeconds, milliSeconds,
tick) tick)
} else { } else {
@ -244,6 +242,7 @@ func (this *Timer) detachTimer(timerList *TimerList) {
} }
func (this *Timer) addToFreeList(timerList *TimerList) { func (this *Timer) addToFreeList(timerList *TimerList) {
timerList.Reset()
this.freeTimerList.AddTail(&timerList.entry) this.freeTimerList.AddTail(&timerList.entry)
this.freeTimerNum++ this.freeTimerNum++
} }
@ -301,7 +300,6 @@ func (this *Timer) getTimerIndex(index uint32) uint32 {
func (this *Timer) newTimerList() *TimerList { func (this *Timer) newTimerList() *TimerList {
if !this.freeTimerList.Empty() { if !this.freeTimerList.Empty() {
timerList := this.freeTimerList.FirstEntry().(*TimerList) timerList := this.freeTimerList.FirstEntry().(*TimerList)
timerList.entry.DelInit()
this.freeTimerNum-- this.freeTimerNum--
return timerList return timerList
} else { } else {

View File

@ -1,16 +1,11 @@
package q5 package q5
type ITimerList interface {
SetTimerAfterFunc(timer_after_func func(params *XParams))
}
type TimerList struct { type TimerList struct {
entry ListHead entry ListHead
attachEntry ListHead attachEntry ListHead
timerType int8 timerType int8
milliSeconds int32 milliSeconds int32
expires int64 expires int64
fixedTimierExecuteTimes int32
timerFunc func (params *XParams) timerFunc func (params *XParams)
timerAfterFunc func (params *XParams) timerAfterFunc func (params *XParams)
@ -24,6 +19,26 @@ func (this *TimerList) InitTimerList(
timerFunc func (params *XParams)) { timerFunc func (params *XParams)) {
this.timerType = timerType this.timerType = timerType
this.milliSeconds = millSeconds this.milliSeconds = millSeconds
this.fixedTimierExecuteTimes = 0
this.timerFunc = timerFunc this.timerFunc = timerFunc
} }
func (this *TimerList) Reset() {
if !this.entry.Empty() {
this.entry.DelInit()
}
if !this.attachEntry.Empty() {
this.attachEntry.DelInit()
}
this.timerFunc = nil
this.timerAfterFunc = nil
this.params.Reset()
}
func (this *TimerList) Init() {
this.entry.Init()
this.attachEntry.Init()
}
func (this *TimerList) SetTimerAfterFunc(timerAfterFunc func(*XParams)) {
this.timerAfterFunc = timerAfterFunc
}

View File

@ -8,6 +8,10 @@ type XParams struct {
} }
func (this *XParams) Reset() *XParams { func (this *XParams) Reset() *XParams {
this.Sender.Reset()
this.Param1.Reset()
this.Param2.Reset()
this.Param3.Reset()
return this return this
} }

View File

@ -27,6 +27,11 @@ func (this *XValue) GetType() int8 {
} }
} }
func (this *XValue) Reset() {
this._type = XVT_UNDEFINED
this._val = nil
}
func (this *XValue) IsUndefined() bool { func (this *XValue) IsUndefined() bool {
return this._type == XVT_UNDEFINED return this._type == XVT_UNDEFINED
} }