fdbus有自己的定时器,基类为CSysLoopTimer
1、CSysLoopTimer
其中,**enable(bool enb, uint64_t time_start, int32_t init_value)**的实现方式如下
enb表示是否使能定时器
time_start表示定时器开始时间,如果小于等于0,则取当前系统时间
init_time表示定时器的初始超时时间,如果值大于0,则以此超时时间计算定时器截至时间,否则使用mInterval值计算定时器截止时间。mInterval一般为定时器使用者传入的超时间隔时间
line69 ~ 77实现了mTimerWorkingList 是按 expiration() 值从小到大排列的。因此,最先到期的定时器会排在最前面,确保它们在事件循环中最先被触发。
当连续调用2次enable(true)方法时,会先将timer从mTimerWorkingList删除,然后重新添加新的timer。
**config(EEnable enb, EEnable rpt, int32_t interval, int32_t init_value)**方法配置相关标志位
2、CBaseLoopTimer
CBaseLoopTimer继承CSysLoopTimer
enable(int32_t interval = -1) 方法为使能定时器,其中interval表示超时间隔时间,如果不设置,则使用mInterval值。
实现方法如下
最终走向CSysLoopTimer::config(EEnable enb, EEnable rpt, int32_t interval, int32_t init_value)
同理,**disable()**也一样。
最终在这里会删除mTimerWorkingList中的timer。
**enableRepeat()和enableOneShot()**的差异点如下,主要是mRepeat标志位的设置
mRepeat标志位会在CBaseEventLoop::processTimers()中使用。
line226表示如果mRepeat为true,则会新加一个timer到mTimerWorkingList中,否则就会从mTimerWorkingList中删除此timer
line237表示执行timer中的run()方法
3、CMethodLoopTimer
CMethodLoopTimer继承CBaseLoopTimer
只需要在构造函数中传入超时间隔时间interval,重复标志位repeat,类示例instance,类示例中的超时执行函数method