From a3412f2da9dc3b8b4dc4811618d77421611f731d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 8 Sep 2020 15:13:23 +0800 Subject: [PATCH] 1 --- listhead.go | 7 +++--- sysutils.go | 9 +++++++ timer.go | 69 +++++++++++++++++++++++++--------------------------- timerlist.go | 2 +- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/listhead.go b/listhead.go index 4252fff..b23cc5e 100644 --- a/listhead.go +++ b/listhead.go @@ -15,15 +15,14 @@ func (this *ListHead) Init() { func (this *ListHead) Del() { this.next.prev = this.prev this.prev.next = this.next - this.Init() } func (this *ListHead) AddTail(pnew *ListHead) { - tmp_prev := this.prev + prev := this.prev + prev.next = pnew this.prev = pnew pnew.next = this - pnew.prev = tmp_prev - tmp_prev.next = pnew + pnew.prev = prev } func (this *ListHead) FirstEntry() interface{} { diff --git a/sysutils.go b/sysutils.go index f6ef0d5..759d525 100644 --- a/sysutils.go +++ b/sysutils.go @@ -4,6 +4,7 @@ import "os" import "net" import "net/http" import "time" +import "reflect" func GetDaySeconds(seconds int64) int64 { return 0 @@ -53,3 +54,11 @@ func ForceCreateDir(dir string) bool { os.MkdirAll(dir, os.ModePerm) return true } + +func TraverseArray(arrayPtr interface{}, callback func(data interface{})) { + array := reflect.ValueOf(arrayPtr).Elem() + for i := 0; i < array.Len(); i++ { + val := array.Index(i) + callback(val) + } +} diff --git a/timer.go b/timer.go index dd00fdb..6b82899 100644 --- a/timer.go +++ b/timer.go @@ -18,12 +18,12 @@ const ( type Timer struct { freeTimerNum int32 - freeTimer ListHead + freeTimerList ListHead runningTimer *TimerList timerTick int64 - getTickCount func () int64 + getTickCount func (interface{}) int64 + getFixedTimerExpires func (interface{}, int32, int32, int64) int64 context interface{} - gcTime int32 cacheTimerNum int32 tv1 [TVR_SIZE]ListHead tv2 [TVN_SIZE]ListHead @@ -33,28 +33,28 @@ type Timer struct { } func (this *Timer) Init( - getTickCount func () int64, + getTickCount func (interface{}) int64, + getFixedTimerExpires func (interface{}, int32, int32, int64) int64, context interface{}, gcTime int32, cacheTimerNum int32) { - this.freeTimer.Init() - for i := 0; i < TVN_SIZE; i += 1 { - this.tv2[i].Init() - this.tv3[i].Init() - this.tv4[i].Init() - this.tv5[i].Init() + initListHeadFunc := func (data interface{}) { + head := data.(*ListHead) + head.Init() } - for i := 0; i < TVR_SIZE; i += 1 { - this.tv1[i].Init() - } - - this.timerTick = getTickCount() + initListHeadFunc(&this.freeTimerList) + TraverseArray(&this.tv1, initListHeadFunc) + TraverseArray(&this.tv2, initListHeadFunc) + TraverseArray(&this.tv3, initListHeadFunc) + TraverseArray(&this.tv4, initListHeadFunc) + TraverseArray(&this.tv5, initListHeadFunc) + this.timerTick = getTickCount(context) this.context = context this.getTickCount = getTickCount - this.gcTime = gcTime + this.getFixedTimerExpires = getFixedTimerExpires this.cacheTimerNum = cacheTimerNum this.AddRepeatTimer( - this.gcTime, + gcTime, func (params *XParams) { }, @@ -66,7 +66,8 @@ func (this *Timer) UnInit() { } func (this *Timer) clear() { - freeTimers := func (head *ListHead) { + freeTimerFunc := func (data interface{}) { + head := data.(*ListHead) for !head.Empty() { timerList := head.FirstEntry().(*TimerList) this.detachTimer(timerList) @@ -75,20 +76,16 @@ func (this *Timer) clear() { } } } - for i := 0; i < TVN_SIZE; i += 1 { - freeTimers(&this.tv2[i]) - freeTimers(&this.tv3[i]) - freeTimers(&this.tv4[i]) - freeTimers(&this.tv5[i]) - } - for i := 0; i < TVR_SIZE; i += 1 { - freeTimers(&this.tv1[i]) - } - freeTimers(&this.freeTimer) + freeTimerFunc(&this.freeTimerList) + TraverseArray(&this.tv1, freeTimerFunc) + TraverseArray(&this.tv2, freeTimerFunc) + TraverseArray(&this.tv3, freeTimerFunc) + TraverseArray(&this.tv4, freeTimerFunc) + TraverseArray(&this.tv5, freeTimerFunc) } func (this *Timer) Update() { - for this.getTickCount() >= this.timerTick { + for this.getTickCount(this.context) >= this.timerTick { index := uint32(this.timerTick & TVR_MASK) if index != 0 && @@ -207,7 +204,7 @@ func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) { this.detachTimer(timerList) timerList.milliSeconds = milliSeconds if timerList.timerType == FIXED_TIMER { - tick := this.getTickCount() + tick := this.getTickCount(this.context) nowTime := time.Now().Unix() todayPassedSeconds := nowTime - GetDaySeconds(nowTime) timerList.expires = (tick - todayPassedSeconds * 1000) + int64(milliSeconds) @@ -217,7 +214,7 @@ func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) { } } } else { - timerList.expires = this.getTickCount() + int64(milliSeconds) + timerList.expires = this.getTickCount(this.context) + int64(milliSeconds) } this.internalAddTimer(timerList) } @@ -252,7 +249,7 @@ func (this *Timer) detachTimer(timerList *TimerList) { } func (this *Timer) addToFreeList(timerList *TimerList) { - this.freeTimer.AddTail(&timerList.entry) + this.freeTimerList.AddTail(&timerList.entry) this.freeTimerNum += 1 } @@ -307,8 +304,8 @@ func (this *Timer) getTimerIndex(index uint32) uint32 { } func (this *Timer) newTimerList() *TimerList { - if !this.freeTimer.Empty() { - timerList := this.freeTimer.FirstEntry().(*TimerList) + if !this.freeTimerList.Empty() { + timerList := this.freeTimerList.FirstEntry().(*TimerList) timerList.entry.DelInit() this.freeTimerNum -= 1 return timerList @@ -320,8 +317,8 @@ func (this *Timer) newTimerList() *TimerList { } func (this *Timer) gcTimerFunc(params *XParams) { - for i := 0; !this.freeTimer.Empty() && this.freeTimerNum > this.cacheTimerNum && i < 1000; i += 1 { - timerList := this.freeTimer.FirstEntry().(*TimerList) + for i := 0; !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum && i < 1000; i += 1 { + timerList := this.freeTimerList.FirstEntry().(*TimerList) timerList.entry.DelInit() this.freeTimerNum -= 1 } diff --git a/timerlist.go b/timerlist.go index 9895dfe..db5e748 100644 --- a/timerlist.go +++ b/timerlist.go @@ -18,7 +18,7 @@ type TimerList struct { } func (this *TimerList) InitTimerList( - timer *Timer, + timer interface{}, timerType int8, millSeconds int32, timerFunc func (params *XParams)) {