This commit is contained in:
aozhiwei 2023-08-12 18:51:47 +08:00
parent 2418793e3d
commit efae915742
5 changed files with 217 additions and 221 deletions

View File

@ -79,8 +79,23 @@ func StrContains(s string, substr string) bool {
func ConvertUpperCamelCase(name string) string { func ConvertUpperCamelCase(name string) string {
newName := "" newName := ""
preIs_ := false
for i := 0; i < len(name); i++ { for i := 0; i < len(name); i++ {
newName += name[i:i] if i == 0 {
newName += strings.ToUpper(name[i:i +1])
preIs_ = name[i] == '_'
} else {
if preIs_ {
if name[i] != '_' {
newName += strings.ToUpper(name[i:i +1])
}
} else {
if name[i] != '_' {
newName += name[i:i +1]
}
}
preIs_ = name[i] == '_'
}
} }
return newName return newName
} }

300
timer.go
View File

@ -9,18 +9,21 @@ const TVN_MASK = TVN_SIZE - 1
const TVR_MASK = TVR_SIZE - 1 const TVR_MASK = TVR_SIZE - 1
const ( const (
DEADLINE_TIMER = 0 TIMEOUT_TIMER = 0
REPEAT_TIMER = iota INTERVAL_TIMER = iota
FIXED_TIMER = iota
) )
type Timer struct { type XTimerDestoryHandleNode struct {
entry ListHead
cb func()
}
type XTimer struct {
freeTimerNum int32 freeTimerNum int32
freeTimerList ListHead freeTimerList ListHead
runningTimer *TimerList runningTimer *XTimerList
timerTick int64 timerTick int64
getTickCount func (interface{}) int64 getTickCount func (interface{}) int64
getFixedTimerExpires func (interface{}, bool, int32, int64) int64
context interface{} context interface{}
cacheTimerNum int32 cacheTimerNum int32
tv1 [TVR_SIZE]ListHead tv1 [TVR_SIZE]ListHead
@ -30,9 +33,8 @@ type Timer struct {
tv5 [TVN_SIZE]ListHead tv5 [TVN_SIZE]ListHead
} }
func (this *Timer) Init( func (this *XTimer) Init(
getTickCount func (interface{}) int64, getTickCount func (interface{}) int64,
getFixedTimerExpires func (interface{}, bool, int32, int64) int64,
context interface{}, context interface{},
gcTime int32, gcTime int32,
cacheTimerNum int32) { cacheTimerNum int32) {
@ -49,25 +51,19 @@ func (this *Timer) Init(
this.timerTick = getTickCount(context) this.timerTick = getTickCount(context)
this.context = context this.context = context
this.getTickCount = getTickCount this.getTickCount = getTickCount
this.getFixedTimerExpires = getFixedTimerExpires
this.cacheTimerNum = cacheTimerNum this.cacheTimerNum = cacheTimerNum
this.AddRepeatTimer( this.SetInterval(gcTime, this.gcTimerFunc)
gcTime,
func (params *XParams) {
},
this.gcTimerFunc)
} }
func (this *Timer) UnInit() { func (this *XTimer) UnInit() {
this.clear() this.clear()
} }
func (this *Timer) clear() { func (this *XTimer) clear() {
freeTimerFunc := func (data interface{}) { freeTimerFunc := func (data interface{}) {
head := data.(*ListHead) head := data.(*ListHead)
for !head.Empty() { for !head.Empty() {
timerList := head.FirstEntry().(*TimerList) timerList := head.FirstEntry().(*XTimerList)
this.detachTimer(timerList) this.detachTimer(timerList)
if !timerList.attachEntry.Empty() { if !timerList.attachEntry.Empty() {
timerList.attachEntry.DelInit() timerList.attachEntry.DelInit()
@ -82,7 +78,7 @@ func (this *Timer) clear() {
TraverseArray(&this.tv5, freeTimerFunc) TraverseArray(&this.tv5, freeTimerFunc)
} }
func (this *Timer) Update() { func (this *XTimer) Update() {
for this.getTickCount(this.context) >= this.timerTick { for this.getTickCount(this.context) >= this.timerTick {
index := uint32(this.timerTick & TVR_MASK) index := uint32(this.timerTick & TVR_MASK)
@ -97,21 +93,15 @@ func (this *Timer) Update() {
var workList ListHead var workList ListHead
this.tv1[index].ReplaceInit(&workList) this.tv1[index].ReplaceInit(&workList)
for !workList.Empty() { for !workList.Empty() {
timerList := workList.FirstEntry().(*TimerList) timer := workList.FirstEntry().(*XTimerList)
this.runningTimer = timerList this.runningTimer = timer
if timerList.timerFunc != nil { timer.cb(TIMER_EXEC_EVENT, nil)
timerList.timerFunc(&timerList.params)
}
if timerList.timerAfterFunc != nil {
timerList.timerAfterFunc(&timerList.params)
}
if this.runningTimer != nil { if this.runningTimer != nil {
switch this.runningTimer.timerType { switch this.runningTimer.timerType {
case REPEAT_TIMER, FIXED_TIMER: case TIMEOUT_TIMER:
this.modifyTimerEx(timerList, timerList.milliSeconds, false) this.internalDelete(timer, false, true)
case DEADLINE_TIMER: case INTERVAL_TIMER:
this.detachTimer(timerList) this.internalModifyTime(timer, timer.expireTime)
this.addToFreeList(timerList)
} }
} }
} }
@ -119,143 +109,106 @@ func (this *Timer) Update() {
this.runningTimer = nil this.runningTimer = nil
} }
func (this *Timer) NewTimerAttacher() *TimerAttacher { func (this *XTimer) NewTimerAttacher() *XTimerAttacher {
attacher := new(TimerAttacher) attacher := new(XTimerAttacher)
attacher.timer = this attacher.timer = this
attacher.timers.Init(nil) attacher.timers.Init(nil)
return attacher return attacher
} }
func (this *Timer) AddDeadLineTimer( func (this *XTimer) SetTimeout(expireTime int32, cb TimerCb) {
milli_seconds int32, this.internalSetTimeout(expireTime, cb, nil, nil)
init_func func (params* XParams),
timer_func func (params* XParams)) *TimerList {
return this.AddDeadLineTimerEx(milli_seconds, init_func, timer_func, nil)
} }
func (this *Timer) AddSimpleDeadLineTimer( func (this *XTimer) SetTimeoutEx(expireTime int32, cb TimerCb, attacher *XTimerAttacher) {
milli_seconds int32, this.internalSetTimeout(expireTime, cb, attacher, nil)
timer_func func (params* XParams)) *TimerList {
return this.AddDeadLineTimer(milli_seconds, nil, timer_func);
} }
func (this *Timer) AddDeadLineTimerEx( func (this *XTimer) SetTimeoutWp(expireTime int32, cb TimerCb) *XTimerWp {
milli_seconds int32, var wp *XTimerWp
init_func func (params* XParams), this.internalSetTimeout(expireTime, cb, nil, &wp)
timer_func func (params* XParams), return wp
install_func func (timerList *TimerList, params* XParams)) *TimerList {
timerList := this.newTimerList()
timerList.params.Reset()
if init_func != nil {
init_func(&timerList.params)
}
timerList.initTimerList(this, DEADLINE_TIMER, milli_seconds, timer_func)
if install_func != nil {
install_func(timerList, &timerList.params)
}
this.ModifyTimer(timerList, milli_seconds)
return timerList
} }
func (this *Timer) AddRepeatTimer( func (this *XTimer) SetTimeoutExWp(expireTime int32, cb TimerCb, attacher *XTimerAttacher) *XTimerWp {
milli_seconds int32, var wp *XTimerWp
init_func func (params* XParams), this.internalSetTimeout(expireTime, cb, attacher, &wp)
timer_func func (params* XParams)) *TimerList { return wp
return this.AddRepeatTimerEx(milli_seconds, init_func, timer_func, nil)
} }
func (this *Timer) AddSimpleRepeatTimer( func (this *XTimer) SetInterval(expireTime int32, cb TimerCb) {
milli_seconds int32, this.internalSetInterval(expireTime, cb, nil, nil)
timer_func func (params* XParams)) *TimerList {
return this.AddRepeatTimer(milli_seconds, nil, timer_func)
} }
func (this *Timer) AddRepeatTimerEx( func (this *XTimer) SetIntervalEx(expireTime int32, cb TimerCb, attacher *XTimerAttacher) {
milli_seconds int32, this.internalSetInterval(expireTime, cb, attacher, nil)
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, REPEAT_TIMER, milli_seconds, timer_func)
if install_func != nil {
install_func(timerList, &timerList.params)
}
this.ModifyTimer(timerList, milli_seconds)
return timerList
} }
func (this *Timer) AddFixedTimer( func (this *XTimer) SetIntervalWp(expireTime int32, cb TimerCb) *XTimerWp {
milli_seconds int32, var wp *XTimerWp
init_func func (params* XParams), this.internalSetInterval(expireTime, cb, nil, &wp)
timer_func func (params* XParams)) *TimerList { return wp
return this.AddFixedTimerEx(milli_seconds, init_func, timer_func, nil)
} }
func (this *Timer) AddSimpleFixedTimer( func (this *XTimer) SetIntervalExWp(expireTime int32, cb TimerCb, attacher *XTimerAttacher) *XTimerWp {
milli_seconds int32, var wp *XTimerWp
timer_func func (params* XParams)) *TimerList { this.internalSetInterval(expireTime, cb, attacher, &wp)
return this.AddFixedTimer(milli_seconds, nil, timer_func) return wp
} }
func (this *Timer) AddFixedTimerEx( func (this *XTimer) internalSetTimeout(expireTime int32, cb TimerCb, attacher *XTimerAttacher, wpPp**XTimerWp) {
milli_seconds int32, timer := this.newTimerList()
init_func func (params* XParams), timer.initTimerList(this, TIMEOUT_TIMER, expireTime, cb)
timer_func func (params* XParams), if attacher != nil {
install_func func (timer_list *TimerList, params* XParams)) *TimerList { attacher.timers.AddTail(&timer.attachEntry)
timerList := this.newTimerList()
if init_func != nil {
init_func(&timerList.params)
} }
timerList.initTimerList(this, FIXED_TIMER, milli_seconds, timer_func) this.internalModifyTime(timer, expireTime)
if install_func != nil { if wpPp != nil {
install_func(timerList, &timerList.params) timer.wp = &XTimerWp{
timer: timer,
}
*wpPp = timer.wp
} }
this.ModifyTimer(timerList, milli_seconds)
return timerList
} }
func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) { func (this *XTimer) internalSetInterval(expireTime int32, cb TimerCb, attacher *XTimerAttacher, wpPp**XTimerWp) {
this.modifyTimerEx(timerList, milliSeconds, true) timer := this.newTimerList()
timer.initTimerList(this, INTERVAL_TIMER, expireTime, cb)
if attacher != nil {
attacher.timers.AddTail(&timer.attachEntry)
}
this.internalModifyTime(timer, expireTime)
if wpPp != nil {
timer.wp = &XTimerWp{
timer: timer,
}
*wpPp = timer.wp
}
} }
func (this *Timer) modifyTimerEx(timerList *TimerList, milliSeconds int32, isFirstAdd bool) { func (this *XTimer) ModifyTimer(timerWp *XTimerWp, expireTime int32) {
this.detachTimer(timerList) if timerWp.Expired() {
timerList.milliSeconds = milliSeconds panic("Xtimer ModifyTimer expired timer")
if timerList.timerType == FIXED_TIMER {
tick := this.getTickCount(this.context)
timerList.expires = this.getFixedTimerExpires(
this.context,
isFirstAdd,
milliSeconds,
tick)
} else {
timerList.expires = this.getTickCount(this.context) + int64(milliSeconds)
} }
this.internalAddTimer(timerList) this.internalModifyTime(timerWp.timer, expireTime)
} }
func (this *Timer) DeleteTimer(timerList *TimerList) { func (this *XTimer) Delete(timerWp *XTimerWp) {
if timerList == nil { if timerWp.Expired() {
return panic("Xtimer Delete expired timer")
} }
if this.runningTimer == timerList { this.internalDelete(timerWp.timer, false, true)
this.runningTimer = nil
}
if timerList.timerAfterFunc != nil {
timerList.timerAfterFunc(&timerList.params)
}
this.detachTimer(timerList)
this.addToFreeList(timerList)
} }
func (this *Timer) GetRemainTime(timerList *TimerList) int64 { func (this *XTimer) GetRemainTime(timerWp *XTimerWp) int64 {
if timerList == nil { if timerWp.Expired() {
return 0 panic("Xtimer GetRemainTime expired timer")
} }
remainTime := timerList.expires - this.getTickCount(this.context) return this.internalGetRemainTime(timerWp.timer)
}
func (this *XTimer) internalGetRemainTime(timer *XTimerList) int64 {
remainTime := timer.expires - this.getTickCount(this.context)
if remainTime < 0 { if remainTime < 0 {
return 0 return 0
} else { } else {
@ -263,11 +216,7 @@ func (this *Timer) GetRemainTime(timerList *TimerList) int64 {
} }
} }
func (this *Timer) GetRunningTimer() *TimerList { func (this *XTimer) GetIdleTime() int64 {
return this.runningTimer
}
func (this *Timer) GetIdleTime() int64 {
var idleTime int64 = 1 var idleTime int64 = 1
for i := (this.timerTick & TVR_MASK); i < TVR_SIZE; i++ { for i := (this.timerTick & TVR_MASK); i < TVR_SIZE; i++ {
if !this.tv1[i].Empty() { if !this.tv1[i].Empty() {
@ -278,19 +227,19 @@ func (this *Timer) GetIdleTime() int64 {
return idleTime return idleTime
} }
func (this *Timer) detachTimer(timerList *TimerList) { func (this *XTimer) detachTimer(timerList *XTimerList) {
if !timerList.entry.Empty() { if !timerList.entry.Empty() {
timerList.entry.DelInit() timerList.entry.DelInit()
} }
} }
func (this *Timer) addToFreeList(timerList *TimerList) { func (this *XTimer) addToFreeList(timerList *XTimerList) {
timerList.reset() timerList.reset()
this.freeTimerList.AddTail(&timerList.entry) this.freeTimerList.AddTail(&timerList.entry)
this.freeTimerNum++ this.freeTimerNum++
} }
func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 { func (this *XTimer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
var cascadeList ListHead var cascadeList ListHead
tv[index].ReplaceInit(&cascadeList) tv[index].ReplaceInit(&cascadeList)
@ -298,7 +247,7 @@ func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
pos := cascadeList.next pos := cascadeList.next
next := pos.next next := pos.next
for pos != &cascadeList { for pos != &cascadeList {
this.internalAddTimer(pos.data.(*TimerList)) this.internalAddTimer(pos.data.(*XTimerList))
pos = next pos = next
next = pos.next next = pos.next
} }
@ -306,7 +255,7 @@ func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 {
return index return index
} }
func (this *Timer) internalAddTimer(timerList *TimerList) { func (this *XTimer) internalAddTimer(timerList *XTimerList) {
timerList.entry.data = timerList timerList.entry.data = timerList
expires := timerList.expires expires := timerList.expires
idx := expires - this.timerTick idx := expires - this.timerTick
@ -335,26 +284,27 @@ func (this *Timer) internalAddTimer(timerList *TimerList) {
vec.AddTail(&timerList.entry) vec.AddTail(&timerList.entry)
} }
func (this *Timer) getTimerIndex(index uint32) uint32 { func (this *XTimer) 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 { func (this *XTimer) newTimerList() *XTimerList {
if !this.freeTimerList.Empty() { if !this.freeTimerList.Empty() {
timerList := this.freeTimerList.FirstEntry().(*TimerList) timerList := this.freeTimerList.FirstEntry().(*XTimerList)
this.freeTimerNum-- this.freeTimerNum--
return timerList return timerList
} else { } else {
timerList := new(TimerList) timerList := new(XTimerList)
timerList.init() timerList.init()
return timerList return timerList
} }
} }
func (this *Timer) gcTimerFunc(params *XParams) { func (this *XTimer) gcTimerFunc(ev int32, args *Args) {
if ev == TIMER_EXEC_EVENT {
count := 0 count := 0
for ; !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum; { for ; !this.freeTimerList.Empty() && this.freeTimerNum > this.cacheTimerNum; {
timerList := this.freeTimerList.FirstEntry().(*TimerList) timerList := this.freeTimerList.FirstEntry().(*XTimerList)
timerList.entry.DelInit() timerList.entry.DelInit()
this.freeTimerNum-- this.freeTimerNum--
count++ count++
@ -363,3 +313,51 @@ func (this *Timer) gcTimerFunc(params *XParams) {
} }
} }
} }
}
func (this *XTimer) internalDelete(timer *XTimerList, isDestory bool, toFreeList bool) {
if timer == nil {
panic("Xtimer.internalDelete error")
}
if this.runningTimer == timer {
this.runningTimer = nil
}
this.detachTimer(timer)
if !timer.attachEntry.Empty() {
timer.attachEntry.DelInit()
}
if isDestory {
timer.cb(TIMER_DESTORY_EVENT, nil)
} else {
timer.cb(TIMER_DELETE_EVENT, nil)
}
timer.cb = nil
if timer.wp != nil {
timer.wp.timer = nil
timer.wp = nil
}
for !timer.destoryHandleList.Empty() {
handle := timer.destoryHandleList.FirstEntry().(XTimerDestoryHandleNode)
handle.entry.DelInit()
handle.cb()
}
if toFreeList {
this.addToFreeList(timer)
}
}
func (this *XTimer) internalModifyTime(timer *XTimerList, expireTime int32) {
this.detachTimer(timer)
timer.expireTime = expireTime
timer.expires = this.getTickCount(this.context) + int64(expireTime)
this.internalAddTimer(timer)
}
func (this *XTimer) clearAttacher(attacher* XTimerAttacher) {
var workList ListHead
attacher.timers.ReplaceInit(&workList)
for !workList.Empty() {
timer := workList.FirstEntry().(*XTimerList)
this.internalDelete(timer, false, true)
}
}

View File

@ -1,62 +1,59 @@
package q5 package q5
type TimerAttacher struct { type XTimerWp struct {
timer *Timer timer *XTimerList
}
type XTimerAttacher struct {
timer *XTimer
timers ListHead timers ListHead
} }
func (this *TimerAttacher) ClearTimers() { func (this *XTimerAttacher) ClearTimers() {
var workList ListHead this.timer.clearAttacher(this)
this.timers.ReplaceInit(&workList)
for !workList.Empty() {
timerList := workList.FirstEntry().(*TimerList)
this.timer.DeleteTimer(timerList)
}
} }
type TimerList struct { type XTimerList struct {
destoryHandleList ListHead
entry ListHead entry ListHead
attachEntry ListHead attachEntry ListHead
timerType int8 timerType int8
milliSeconds int32 expireTime int32
expires int64 expires int64
timerFunc func (params *XParams) cb TimerCb
timerAfterFunc func (params *XParams) wp *XTimerWp
params XParams
} }
func (this *TimerList) initTimerList( func (this *XTimerWp) Expired() bool {
return this.timer == nil
}
func (this *XTimerList) initTimerList(
timer interface{}, timer interface{},
timerType int8, timerType int8,
millSeconds int32, expireTime int32,
timerFunc func (params *XParams)) { cb TimerCb) {
this.timerType = timerType this.timerType = timerType
this.milliSeconds = millSeconds this.expireTime = expireTime
this.timerFunc = timerFunc this.cb = cb
} }
func (this *TimerList) reset() { func (this *XTimerList) reset() {
if !this.entry.Empty() { if !this.entry.Empty() {
this.entry.DelInit() this.entry.DelInit()
} }
if !this.attachEntry.Empty() { if !this.attachEntry.Empty() {
this.attachEntry.DelInit() this.attachEntry.DelInit()
} }
this.timerFunc = nil this.cb = nil
this.timerAfterFunc = nil
this.params.Reset()
} }
func (this *TimerList) init() { func (this *XTimerList) init() {
this.entry.Init(this) this.entry.Init(this)
this.attachEntry.Init(this) this.attachEntry.Init(this)
} }
func (this *TimerList) SetTimerAfterFunc(timerAfterFunc func(*XParams)) { func (this *XTimerList) Attach(timerAttacher *XTimerAttacher) {
this.timerAfterFunc = timerAfterFunc
}
func (this *TimerList) Attach(timerAttacher *TimerAttacher) {
timerAttacher.timers.AddTail(&this.attachEntry) timerAttacher.timers.AddTail(&this.attachEntry)
} }

View File

@ -1 +1,8 @@
package q5 package q5
type TimerCb func (int32, *Args)
type Args []interface{}
const TIMER_EXEC_EVENT = 1
const TIMER_DELETE_EVENT = 2
const TIMER_DESTORY_EVENT = 3

View File

@ -1,21 +0,0 @@
package q5
type XParams struct {
Sender XValue
Param1 XValue
Param2 XValue
Param3 XValue
}
func (this *XParams) Reset() *XParams {
this.Sender.Reset()
this.Param1.Reset()
this.Param2.Reset()
this.Param3.Reset()
return this
}
func (this *XParams) Init(initFunc func (*XParams)) *XParams {
initFunc(this)
return this
}