172 lines
4.1 KiB
Go
172 lines
4.1 KiB
Go
package q5
|
|
|
|
const CONFIG_BASE_SMALL = false
|
|
const TVN_BITS = 6
|
|
const TVR_BITS = 8
|
|
const TVN_SIZE = 1 << TVN_BITS
|
|
const TVR_SIZE = 1 << TVR_BITS
|
|
const TVN_MASK = TVN_SIZE - 1
|
|
const TVR_MASK = TVR_SIZE - 1
|
|
|
|
const (
|
|
DEADLINE_TIMER = 0
|
|
REPEAT_TIMER = iota
|
|
FIXED_TIMER = iota
|
|
)
|
|
|
|
type Timer struct {
|
|
freeTimerNum int32
|
|
freeTimer ListHead
|
|
runningTimer *TimerList
|
|
timerTick int64
|
|
getTickCount func () int64
|
|
content interface{}
|
|
gcTime int32
|
|
cacheTimerNum int32
|
|
tv1 [TVR_SIZE]ListHead
|
|
tv2 [TVN_SIZE]ListHead
|
|
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{},
|
|
gcTime int32,
|
|
cacheTimerNum int32) {
|
|
this.getTickCount = getTickCount
|
|
this.content = content
|
|
this.gcTime = gcTime
|
|
this.cacheTimerNum = cacheTimerNum
|
|
}
|
|
|
|
func (this *Timer) UnInit() {
|
|
this.clear()
|
|
}
|
|
|
|
func (this *Timer) clear() {
|
|
|
|
}
|
|
|
|
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 {
|
|
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)
|
|
this.runningTimer = timer_list
|
|
if timer_list.timerFunc != nil {
|
|
timer_list.timerFunc(&timer_list.params)
|
|
}
|
|
if timer_list.timerAfterFunc != nil {
|
|
timer_list.timerAfterFunc(&timer_list.params)
|
|
}
|
|
if this.runningTimer != nil {
|
|
switch this.runningTimer.timerType {
|
|
case REPEAT_TIMER, FIXED_TIMER:
|
|
if timer_list.timerType == FIXED_TIMER {
|
|
timer_list.fixedTimierExecuteTimes += 1
|
|
}
|
|
this.ModifyTimer(timer_list, timer_list.milliSeconds)
|
|
case DEADLINE_TIMER:
|
|
this.detachTimer(timer_list)
|
|
if timer_list.attachEntry.Empty() {
|
|
timer_list.attachEntry.DelInit()
|
|
}
|
|
this.addToFreeList(timer_list)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this.runningTimer = nil
|
|
}
|
|
|
|
func (this *Timer) AddDeadLineTimer(
|
|
milli_seconds int32,
|
|
init_func func (params* XParams),
|
|
timer_func func (params* XParams)) ITimerList {
|
|
return this.AddDeadLineTimerEx(milli_seconds, init_func, timer_func, nil)
|
|
}
|
|
|
|
func (this *Timer) AddDeadLineTimerEx(
|
|
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) {
|
|
|
|
}
|
|
|
|
func (this *Timer) detachTimer(timerList *TimerList) {
|
|
|
|
}
|
|
|
|
func (this *Timer) addToFreeList(timerList *TimerList) {
|
|
|
|
}
|
|
|
|
func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
|
|
tv[index].ReplaceInit(&this.cascadeList)
|
|
|
|
if !this.cascadeList.Empty() {
|
|
pos := this.cascadeList.next
|
|
next := pos.next
|
|
for pos != &this.cascadeList {
|
|
this.internalAddTimer(pos.data.(*TimerList))
|
|
pos = next
|
|
next = pos.next
|
|
}
|
|
}
|
|
this.cascadeList.Init()
|
|
return index
|
|
}
|
|
|
|
func (this *Timer) internalAddTimer(timer_list *TimerList) {
|
|
expires := timer_list.expires
|
|
idx := expires - this.timerTick
|
|
var vec *ListHead
|
|
var index uint32
|
|
|
|
if idx < 0 {
|
|
index = (uint32)(this.timerTick & TVR_MASK)
|
|
vec = &this.tv1[index]
|
|
} else if idx < TVR_SIZE {
|
|
index = (uint32)(expires & TVR_MASK)
|
|
vec = &this.tv1[index]
|
|
} else if idx < (1 << (TVR_BITS + TVN_BITS)) {
|
|
index = (uint32)(expires & TVR_MASK)
|
|
vec = &this.tv2[index]
|
|
} else if idx < (1 << (TVR_BITS + 2 * TVN_BITS)) {
|
|
index = (uint32)(expires & TVR_MASK)
|
|
vec = &this.tv3[index]
|
|
} else if idx < (1 << (TVR_BITS + 3 * TVN_BITS)) {
|
|
index = (uint32)(expires & TVR_MASK)
|
|
vec = &this.tv4[index]
|
|
} else {
|
|
index = (uint32)(expires & TVR_MASK)
|
|
vec = &this.tv5[index]
|
|
}
|
|
vec.AddTail(&timer_list.entry)
|
|
}
|
|
|
|
func (this *Timer) getTimerIndex(index uint32) uint32 {
|
|
return (uint32)((this.timerTick >> (TVR_BITS + index * TVN_BITS)) & TVN_MASK);
|
|
}
|