1
This commit is contained in:
parent
924eed81d1
commit
a8b0821963
95
timer.go
95
timer.go
@ -20,7 +20,7 @@ type Timer struct {
|
||||
runningTimer *TimerList
|
||||
timerTick int64
|
||||
getTickCount func () int64
|
||||
content interface{}
|
||||
context interface{}
|
||||
gcTime int32
|
||||
cacheTimerNum int32
|
||||
tv1 [TVR_SIZE]ListHead
|
||||
@ -28,19 +28,36 @@ type Timer struct {
|
||||
tv3 [TVN_SIZE]ListHead
|
||||
tv4 [TVN_SIZE]ListHead
|
||||
tv5 [TVN_SIZE]ListHead
|
||||
workList ListHead
|
||||
cascadeList ListHead
|
||||
}
|
||||
|
||||
func (this *Timer) Init(
|
||||
getTickCount func () int64,
|
||||
content interface{},
|
||||
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()
|
||||
}
|
||||
for i := 0; i < TVR_SIZE; i += 1 {
|
||||
this.tv1[i].Init()
|
||||
}
|
||||
|
||||
this.timerTick = getTickCount()
|
||||
this.context = context
|
||||
this.getTickCount = getTickCount
|
||||
this.content = content
|
||||
this.gcTime = gcTime
|
||||
this.cacheTimerNum = cacheTimerNum
|
||||
this.AddRepeatTimer(
|
||||
this.gcTime,
|
||||
func (params *XParams) {
|
||||
|
||||
},
|
||||
this.gcTimerFunc)
|
||||
}
|
||||
|
||||
func (this *Timer) UnInit() {
|
||||
@ -48,23 +65,43 @@ func (this *Timer) UnInit() {
|
||||
}
|
||||
|
||||
func (this *Timer) clear() {
|
||||
|
||||
freeTimers := func (head *ListHead) {
|
||||
for !head.Empty() {
|
||||
timerList := head.FirstEntry().(*TimerList)
|
||||
this.detachTimer(timerList)
|
||||
if !timerList.attachEntry.Empty() {
|
||||
timerList.attachEntry.DelInit()
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
func (this *Timer) Update() {
|
||||
for this.getTickCount() >= this.timerTick {
|
||||
index := uint32(this.timerTick & TVR_MASK)
|
||||
|
||||
if index == 0 &&
|
||||
this.cascade(&this.tv2, this.getTimerIndex(index)) == 0 &&
|
||||
this.cascade(&this.tv3, this.getTimerIndex(index)) == 0 &&
|
||||
this.cascade(&this.tv4, this.getTimerIndex(index)) == 0 {
|
||||
if index != 0 &&
|
||||
this.cascade(&this.tv2, this.getTimerIndex(index)) != 0 &&
|
||||
this.cascade(&this.tv3, this.getTimerIndex(index)) != 0 &&
|
||||
this.cascade(&this.tv4, this.getTimerIndex(index)) != 0 {
|
||||
this.cascade(&this.tv5, this.getTimerIndex(index))
|
||||
}
|
||||
this.timerTick += 1
|
||||
this.tv1[index].ReplaceInit(&this.workList)
|
||||
for !this.workList.Empty() {
|
||||
timer_list := this.workList.FirstEntry().(*TimerList)
|
||||
|
||||
var workList ListHead
|
||||
this.tv1[index].ReplaceInit(&workList)
|
||||
for !workList.Empty() {
|
||||
timer_list := workList.FirstEntry().(*TimerList)
|
||||
this.runningTimer = timer_list
|
||||
if timer_list.timerFunc != nil {
|
||||
timer_list.timerFunc(&timer_list.params)
|
||||
@ -110,6 +147,24 @@ func (this *Timer) AddDeadLineTimerEx(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Timer) AddRepeatTimer(
|
||||
milli_seconds int32,
|
||||
init_func func (params* XParams),
|
||||
timer_func func (params* XParams)) ITimerList {
|
||||
return this.AddRepeatTimerEx(milli_seconds, init_func, timer_func, nil)
|
||||
}
|
||||
|
||||
func (this *Timer) AddRepeatTimerEx(
|
||||
milli_seconds int32,
|
||||
init_func func (params* XParams),
|
||||
timer_func func (params* XParams),
|
||||
install_func func (timer_list ITimerList, params* XParams)) ITimerList {
|
||||
if init_func != nil {
|
||||
init_func(new(XParams))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Timer) ModifyTimer(timerList *TimerList, milli_seconds int32) {
|
||||
|
||||
}
|
||||
@ -151,16 +206,16 @@ func (this *Timer) internalAddTimer(timer_list *TimerList) {
|
||||
index = (uint32)(expires & TVR_MASK)
|
||||
vec = &this.tv1[index]
|
||||
} else if idx < (1 << (TVR_BITS + TVN_BITS)) {
|
||||
index = (uint32)(expires & TVR_MASK)
|
||||
index = (uint32)((expires >> TVR_BITS) & TVR_MASK)
|
||||
vec = &this.tv2[index]
|
||||
} else if idx < (1 << (TVR_BITS + 2 * TVN_BITS)) {
|
||||
index = (uint32)(expires & TVR_MASK)
|
||||
index = (uint32)((expires >> (TVR_BITS + 1 * TVN_BITS)) & TVR_MASK)
|
||||
vec = &this.tv3[index]
|
||||
} else if idx < (1 << (TVR_BITS + 3 * TVN_BITS)) {
|
||||
index = (uint32)(expires & TVR_MASK)
|
||||
index = (uint32)((expires >> (TVR_BITS + 2 * TVN_BITS)) & TVR_MASK)
|
||||
vec = &this.tv4[index]
|
||||
} else {
|
||||
index = (uint32)(expires & TVR_MASK)
|
||||
index = (uint32)((expires >> (TVR_BITS + 3 * TVN_BITS)) & TVR_MASK)
|
||||
vec = &this.tv5[index]
|
||||
}
|
||||
vec.AddTail(&timer_list.entry)
|
||||
@ -169,3 +224,11 @@ func (this *Timer) internalAddTimer(timer_list *TimerList) {
|
||||
func (this *Timer) getTimerIndex(index uint32) uint32 {
|
||||
return (uint32)((this.timerTick >> (TVR_BITS + index * TVN_BITS)) & TVN_MASK);
|
||||
}
|
||||
|
||||
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)
|
||||
timerList.entry.DelInit()
|
||||
this.freeTimerNum -= 1
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user