From a3ab5210642d106d6cde01dfb33f44456676ffbc Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 8 Sep 2020 17:00:41 +0800 Subject: [PATCH] 1 --- timer.go | 22 ++++++++++------------ timerlist.go | 27 +++++++++++++++++++++------ xparams.go | 4 ++++ xvalue.go | 5 +++++ 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/timer.go b/timer.go index 23ebd01..b761ace 100644 --- a/timer.go +++ b/timer.go @@ -20,7 +20,7 @@ type Timer struct { runningTimer *TimerList timerTick int64 getTickCount func (interface{}) int64 - getFixedTimerExpires func (interface{}, int32, int32, int64) int64 + getFixedTimerExpires func (interface{}, bool, int32, int64) int64 context interface{} cacheTimerNum int32 tv1 [TVR_SIZE]ListHead @@ -32,7 +32,7 @@ type Timer struct { func (this *Timer) Init( getTickCount func (interface{}) int64, - getFixedTimerExpires func (interface{}, int32, int32, int64) int64, + getFixedTimerExpires func (interface{}, bool, int32, int64) int64, context interface{}, gcTime int32, cacheTimerNum int32) { @@ -108,15 +108,9 @@ func (this *Timer) Update() { if this.runningTimer != nil { switch this.runningTimer.timerType { case REPEAT_TIMER, FIXED_TIMER: - if timerList.timerType == FIXED_TIMER { - timerList.fixedTimierExecuteTimes++ - } - this.ModifyTimer(timerList, timerList.milliSeconds) + this.modifyTimerEx(timerList, timerList.milliSeconds, false) case DEADLINE_TIMER: this.detachTimer(timerList) - if timerList.attachEntry.Empty() { - timerList.attachEntry.DelInit() - } this.addToFreeList(timerList) } } @@ -198,14 +192,18 @@ func (this *Timer) AddFixedTimerEx( 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) timerList.milliSeconds = milliSeconds if timerList.timerType == FIXED_TIMER { tick := this.getTickCount(this.context) timerList.expires = this.getFixedTimerExpires( this.context, - timerList.fixedTimierExecuteTimes, + isFirstAdd, milliSeconds, tick) } else { @@ -244,6 +242,7 @@ func (this *Timer) detachTimer(timerList *TimerList) { } func (this *Timer) addToFreeList(timerList *TimerList) { + timerList.Reset() this.freeTimerList.AddTail(&timerList.entry) this.freeTimerNum++ } @@ -301,7 +300,6 @@ func (this *Timer) getTimerIndex(index uint32) uint32 { func (this *Timer) newTimerList() *TimerList { if !this.freeTimerList.Empty() { timerList := this.freeTimerList.FirstEntry().(*TimerList) - timerList.entry.DelInit() this.freeTimerNum-- return timerList } else { diff --git a/timerlist.go b/timerlist.go index db5e748..d808489 100644 --- a/timerlist.go +++ b/timerlist.go @@ -1,16 +1,11 @@ package q5 -type ITimerList interface { - SetTimerAfterFunc(timer_after_func func(params *XParams)) -} - type TimerList struct { entry ListHead attachEntry ListHead timerType int8 milliSeconds int32 expires int64 - fixedTimierExecuteTimes int32 timerFunc func (params *XParams) timerAfterFunc func (params *XParams) @@ -24,6 +19,26 @@ func (this *TimerList) InitTimerList( timerFunc func (params *XParams)) { this.timerType = timerType this.milliSeconds = millSeconds - this.fixedTimierExecuteTimes = 0 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 +} diff --git a/xparams.go b/xparams.go index dc55fa9..08d6f4a 100644 --- a/xparams.go +++ b/xparams.go @@ -8,6 +8,10 @@ type XParams struct { } func (this *XParams) Reset() *XParams { + this.Sender.Reset() + this.Param1.Reset() + this.Param2.Reset() + this.Param3.Reset() return this } diff --git a/xvalue.go b/xvalue.go index bf17cb9..34f19ff 100644 --- a/xvalue.go +++ b/xvalue.go @@ -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 { return this._type == XVT_UNDEFINED }