This commit is contained in:
aozhiwei 2020-09-08 15:13:23 +08:00
parent 8360f367bc
commit a3412f2da9
4 changed files with 46 additions and 41 deletions

View File

@ -15,15 +15,14 @@ func (this *ListHead) Init() {
func (this *ListHead) Del() { func (this *ListHead) Del() {
this.next.prev = this.prev this.next.prev = this.prev
this.prev.next = this.next this.prev.next = this.next
this.Init()
} }
func (this *ListHead) AddTail(pnew *ListHead) { func (this *ListHead) AddTail(pnew *ListHead) {
tmp_prev := this.prev prev := this.prev
prev.next = pnew
this.prev = pnew this.prev = pnew
pnew.next = this pnew.next = this
pnew.prev = tmp_prev pnew.prev = prev
tmp_prev.next = pnew
} }
func (this *ListHead) FirstEntry() interface{} { func (this *ListHead) FirstEntry() interface{} {

View File

@ -4,6 +4,7 @@ import "os"
import "net" import "net"
import "net/http" import "net/http"
import "time" import "time"
import "reflect"
func GetDaySeconds(seconds int64) int64 { func GetDaySeconds(seconds int64) int64 {
return 0 return 0
@ -53,3 +54,11 @@ func ForceCreateDir(dir string) bool {
os.MkdirAll(dir, os.ModePerm) os.MkdirAll(dir, os.ModePerm)
return true 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)
}
}

View File

@ -18,12 +18,12 @@ const (
type Timer struct { type Timer struct {
freeTimerNum int32 freeTimerNum int32
freeTimer ListHead freeTimerList ListHead
runningTimer *TimerList runningTimer *TimerList
timerTick int64 timerTick int64
getTickCount func () int64 getTickCount func (interface{}) int64
getFixedTimerExpires func (interface{}, int32, int32, int64) int64
context interface{} context interface{}
gcTime int32
cacheTimerNum int32 cacheTimerNum int32
tv1 [TVR_SIZE]ListHead tv1 [TVR_SIZE]ListHead
tv2 [TVN_SIZE]ListHead tv2 [TVN_SIZE]ListHead
@ -33,28 +33,28 @@ type Timer struct {
} }
func (this *Timer) Init( func (this *Timer) Init(
getTickCount func () int64, getTickCount func (interface{}) int64,
getFixedTimerExpires func (interface{}, int32, int32, int64) int64,
context interface{}, context interface{},
gcTime int32, gcTime int32,
cacheTimerNum int32) { cacheTimerNum int32) {
this.freeTimer.Init() initListHeadFunc := func (data interface{}) {
for i := 0; i < TVN_SIZE; i += 1 { head := data.(*ListHead)
this.tv2[i].Init() head.Init()
this.tv3[i].Init()
this.tv4[i].Init()
this.tv5[i].Init()
} }
for i := 0; i < TVR_SIZE; i += 1 { initListHeadFunc(&this.freeTimerList)
this.tv1[i].Init() TraverseArray(&this.tv1, initListHeadFunc)
} TraverseArray(&this.tv2, initListHeadFunc)
TraverseArray(&this.tv3, initListHeadFunc)
this.timerTick = getTickCount() TraverseArray(&this.tv4, initListHeadFunc)
TraverseArray(&this.tv5, initListHeadFunc)
this.timerTick = getTickCount(context)
this.context = context this.context = context
this.getTickCount = getTickCount this.getTickCount = getTickCount
this.gcTime = gcTime this.getFixedTimerExpires = getFixedTimerExpires
this.cacheTimerNum = cacheTimerNum this.cacheTimerNum = cacheTimerNum
this.AddRepeatTimer( this.AddRepeatTimer(
this.gcTime, gcTime,
func (params *XParams) { func (params *XParams) {
}, },
@ -66,7 +66,8 @@ func (this *Timer) UnInit() {
} }
func (this *Timer) clear() { func (this *Timer) clear() {
freeTimers := func (head *ListHead) { freeTimerFunc := func (data interface{}) {
head := data.(*ListHead)
for !head.Empty() { for !head.Empty() {
timerList := head.FirstEntry().(*TimerList) timerList := head.FirstEntry().(*TimerList)
this.detachTimer(timerList) this.detachTimer(timerList)
@ -75,20 +76,16 @@ func (this *Timer) clear() {
} }
} }
} }
for i := 0; i < TVN_SIZE; i += 1 { freeTimerFunc(&this.freeTimerList)
freeTimers(&this.tv2[i]) TraverseArray(&this.tv1, freeTimerFunc)
freeTimers(&this.tv3[i]) TraverseArray(&this.tv2, freeTimerFunc)
freeTimers(&this.tv4[i]) TraverseArray(&this.tv3, freeTimerFunc)
freeTimers(&this.tv5[i]) TraverseArray(&this.tv4, freeTimerFunc)
} TraverseArray(&this.tv5, freeTimerFunc)
for i := 0; i < TVR_SIZE; i += 1 {
freeTimers(&this.tv1[i])
}
freeTimers(&this.freeTimer)
} }
func (this *Timer) Update() { func (this *Timer) Update() {
for this.getTickCount() >= this.timerTick { for this.getTickCount(this.context) >= this.timerTick {
index := uint32(this.timerTick & TVR_MASK) index := uint32(this.timerTick & TVR_MASK)
if index != 0 && if index != 0 &&
@ -207,7 +204,7 @@ func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) {
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() tick := this.getTickCount(this.context)
nowTime := time.Now().Unix() nowTime := time.Now().Unix()
todayPassedSeconds := nowTime - GetDaySeconds(nowTime) todayPassedSeconds := nowTime - GetDaySeconds(nowTime)
timerList.expires = (tick - todayPassedSeconds * 1000) + int64(milliSeconds) timerList.expires = (tick - todayPassedSeconds * 1000) + int64(milliSeconds)
@ -217,7 +214,7 @@ func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) {
} }
} }
} else { } else {
timerList.expires = this.getTickCount() + int64(milliSeconds) timerList.expires = this.getTickCount(this.context) + int64(milliSeconds)
} }
this.internalAddTimer(timerList) this.internalAddTimer(timerList)
} }
@ -252,7 +249,7 @@ func (this *Timer) detachTimer(timerList *TimerList) {
} }
func (this *Timer) addToFreeList(timerList *TimerList) { func (this *Timer) addToFreeList(timerList *TimerList) {
this.freeTimer.AddTail(&timerList.entry) this.freeTimerList.AddTail(&timerList.entry)
this.freeTimerNum += 1 this.freeTimerNum += 1
} }
@ -307,8 +304,8 @@ func (this *Timer) getTimerIndex(index uint32) uint32 {
} }
func (this *Timer) newTimerList() *TimerList { func (this *Timer) newTimerList() *TimerList {
if !this.freeTimer.Empty() { if !this.freeTimerList.Empty() {
timerList := this.freeTimer.FirstEntry().(*TimerList) timerList := this.freeTimerList.FirstEntry().(*TimerList)
timerList.entry.DelInit() timerList.entry.DelInit()
this.freeTimerNum -= 1 this.freeTimerNum -= 1
return timerList return timerList
@ -320,8 +317,8 @@ func (this *Timer) newTimerList() *TimerList {
} }
func (this *Timer) gcTimerFunc(params *XParams) { func (this *Timer) gcTimerFunc(params *XParams) {
for i := 0; !this.freeTimer.Empty() && this.freeTimerNum > this.cacheTimerNum && i < 1000; i += 1 { for i := 0; !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum && i < 1000; i += 1 {
timerList := this.freeTimer.FirstEntry().(*TimerList) timerList := this.freeTimerList.FirstEntry().(*TimerList)
timerList.entry.DelInit() timerList.entry.DelInit()
this.freeTimerNum -= 1 this.freeTimerNum -= 1
} }

View File

@ -18,7 +18,7 @@ type TimerList struct {
} }
func (this *TimerList) InitTimerList( func (this *TimerList) InitTimerList(
timer *Timer, timer interface{},
timerType int8, timerType int8,
millSeconds int32, millSeconds int32,
timerFunc func (params *XParams)) { timerFunc func (params *XParams)) {