1
This commit is contained in:
parent
a8b0821963
commit
d1c788bb06
104
timer.go
104
timer.go
@ -1,5 +1,7 @@
|
|||||||
package q5
|
package q5
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
const CONFIG_BASE_SMALL = false
|
const CONFIG_BASE_SMALL = false
|
||||||
const TVN_BITS = 6
|
const TVN_BITS = 6
|
||||||
const TVR_BITS = 8
|
const TVR_BITS = 8
|
||||||
@ -132,7 +134,7 @@ func (this *Timer) Update() {
|
|||||||
func (this *Timer) AddDeadLineTimer(
|
func (this *Timer) AddDeadLineTimer(
|
||||||
milli_seconds int32,
|
milli_seconds int32,
|
||||||
init_func func (params* XParams),
|
init_func func (params* XParams),
|
||||||
timer_func func (params* XParams)) ITimerList {
|
timer_func func (params* XParams)) *TimerList {
|
||||||
return this.AddDeadLineTimerEx(milli_seconds, init_func, timer_func, nil)
|
return this.AddDeadLineTimerEx(milli_seconds, init_func, timer_func, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,17 +142,23 @@ func (this *Timer) AddDeadLineTimerEx(
|
|||||||
milli_seconds int32,
|
milli_seconds int32,
|
||||||
init_func func (params* XParams),
|
init_func func (params* XParams),
|
||||||
timer_func func (params* XParams),
|
timer_func func (params* XParams),
|
||||||
install_func func (timer_list ITimerList, params* XParams)) ITimerList {
|
install_func func (timerList *TimerList, params* XParams)) *TimerList {
|
||||||
|
timerList := this.newTimerList()
|
||||||
|
timerList.params.Reset()
|
||||||
if init_func != nil {
|
if init_func != nil {
|
||||||
init_func(new(XParams))
|
init_func(&timerList.params)
|
||||||
}
|
}
|
||||||
return nil
|
timerList.InitTimerList(this, DEADLINE_TIMER, milli_seconds, timer_func)
|
||||||
|
if install_func != nil {
|
||||||
|
install_func(timerList, &timerList.params)
|
||||||
|
}
|
||||||
|
return timerList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Timer) AddRepeatTimer(
|
func (this *Timer) AddRepeatTimer(
|
||||||
milli_seconds int32,
|
milli_seconds int32,
|
||||||
init_func func (params* XParams),
|
init_func func (params* XParams),
|
||||||
timer_func func (params* XParams)) ITimerList {
|
timer_func func (params* XParams)) *TimerList {
|
||||||
return this.AddRepeatTimerEx(milli_seconds, init_func, timer_func, nil)
|
return this.AddRepeatTimerEx(milli_seconds, init_func, timer_func, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,23 +166,88 @@ func (this *Timer) AddRepeatTimerEx(
|
|||||||
milli_seconds int32,
|
milli_seconds int32,
|
||||||
init_func func (params* XParams),
|
init_func func (params* XParams),
|
||||||
timer_func func (params* XParams),
|
timer_func func (params* XParams),
|
||||||
install_func func (timer_list ITimerList, params* XParams)) ITimerList {
|
install_func func (timer_list *TimerList, params* XParams)) *TimerList {
|
||||||
|
timerList := this.newTimerList()
|
||||||
if init_func != nil {
|
if init_func != nil {
|
||||||
init_func(new(XParams))
|
init_func(&timerList.params)
|
||||||
}
|
}
|
||||||
|
timerList.InitTimerList(this, REPEAT_TIMER, milli_seconds, timer_func)
|
||||||
|
if install_func != nil {
|
||||||
|
install_func(timerList, &timerList.params)
|
||||||
|
}
|
||||||
|
return timerList
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) AddFixedTimer(
|
||||||
|
milli_seconds int32,
|
||||||
|
init_func func (params* XParams),
|
||||||
|
timer_func func (params* XParams)) *TimerList {
|
||||||
|
return this.AddFixedTimerEx(milli_seconds, init_func, timer_func, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) AddFixedTimerEx(
|
||||||
|
milli_seconds int32,
|
||||||
|
init_func func (params* XParams),
|
||||||
|
timer_func func (params* XParams),
|
||||||
|
install_func func (timer_list *TimerList, params* XParams)) *TimerList {
|
||||||
|
timerList := this.newTimerList()
|
||||||
|
if init_func != nil {
|
||||||
|
init_func(&timerList.params)
|
||||||
|
}
|
||||||
|
timerList.InitTimerList(this, FIXED_TIMER, milli_seconds, timer_func)
|
||||||
|
if install_func != nil {
|
||||||
|
install_func(timerList, &timerList.params)
|
||||||
|
}
|
||||||
|
return timerList
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) {
|
||||||
|
this.detachTimer(timerList)
|
||||||
|
timerList.milliSeconds = milliSeconds
|
||||||
|
if timerList.timerType == FIXED_TIMER {
|
||||||
|
tick := this.getTickCount()
|
||||||
|
nowTime := time.Now().Unix()
|
||||||
|
todayPassedSeconds := nowTime - GetDaySeconds(nowTime)
|
||||||
|
timerList.expires = (tick - todayPassedSeconds * 1000) + int64(milliSeconds)
|
||||||
|
if timerList.fixedTimierExecuteTimes > 0 {
|
||||||
|
if timerList.expires <= tick {
|
||||||
|
timerList.expires += 1000 * 3600 * 24
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
timerList.expires = this.getTickCount() + int64(milliSeconds)
|
||||||
|
}
|
||||||
|
this.internalAddTimer(timerList)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) DeleteTimer(timerList *TimerList) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) GetTimerByAtttach(attachEntry *ListHead) *TimerList {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Timer) ModifyTimer(timerList *TimerList, milli_seconds int32) {
|
func (this *Timer) MutableParams(timerList *TimerList) *XParams {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) GetRemainTime(timerList *TimerList) int64 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Timer) GetRunningTimer() *TimerList {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Timer) detachTimer(timerList *TimerList) {
|
func (this *Timer) detachTimer(timerList *TimerList) {
|
||||||
|
if !timerList.entry.Empty() {
|
||||||
|
timerList.entry.DelInit()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Timer) addToFreeList(timerList *TimerList) {
|
func (this *Timer) addToFreeList(timerList *TimerList) {
|
||||||
|
this.freeTimer.AddTail(&timerList.entry)
|
||||||
|
this.freeTimerNum += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
|
func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
|
||||||
@ -225,6 +298,17 @@ func (this *Timer) getTimerIndex(index uint32) uint32 {
|
|||||||
return (uint32)((this.timerTick >> (TVR_BITS + index * TVN_BITS)) & TVN_MASK);
|
return (uint32)((this.timerTick >> (TVR_BITS + index * TVN_BITS)) & TVN_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Timer) newTimerList() *TimerList {
|
||||||
|
if !this.freeTimer.Empty() {
|
||||||
|
timerList := this.freeTimer.FirstEntry().(*TimerList)
|
||||||
|
timerList.entry.DelInit()
|
||||||
|
this.freeTimerNum -= 1
|
||||||
|
return timerList
|
||||||
|
} else {
|
||||||
|
return new(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.freeTimer.Empty() && this.freeTimerNum > this.cacheTimerNum && i < 1000; i += 1 {
|
||||||
timerList := this.freeTimer.FirstEntry().(*TimerList)
|
timerList := this.freeTimer.FirstEntry().(*TimerList)
|
||||||
|
@ -21,13 +21,9 @@ func (this *TimerList) InitTimerList(
|
|||||||
timer *Timer,
|
timer *Timer,
|
||||||
timerType int8,
|
timerType int8,
|
||||||
millSeconds int32,
|
millSeconds int32,
|
||||||
params XParams,
|
timerFunc func (params *XParams)) {
|
||||||
timerFunc func (params *XParams),
|
|
||||||
timerAfterFunc func (params *XParams)) {
|
|
||||||
this.timerType = timerType
|
this.timerType = timerType
|
||||||
this.milliSeconds = millSeconds
|
this.milliSeconds = millSeconds
|
||||||
this.fixedTimierExecuteTimes = 0
|
this.fixedTimierExecuteTimes = 0
|
||||||
this.timerFunc = timerFunc
|
this.timerFunc = timerFunc
|
||||||
this.timerAfterFunc = timerAfterFunc
|
|
||||||
this.params = params
|
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,10 @@ type XParams struct {
|
|||||||
Param3 XValue
|
Param3 XValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *XParams) Reset() *XParams {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
func (this *XParams) SetInt32(val int32) *XParams {
|
func (this *XParams) SetInt32(val int32) *XParams {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user