61 lines
1.9 KiB
C++
61 lines
1.9 KiB
C++
/*
|
|
* 仿照linux2.6内核定时器实现
|
|
*/
|
|
#ifndef A8_XTIMER_H
|
|
#define A8_XTIMER_H
|
|
|
|
struct xtimer_list;
|
|
struct xtvec_base;
|
|
|
|
namespace a8
|
|
{
|
|
typedef void (*XTimerFunc)(const a8::XParams& param);
|
|
typedef void (*XTimerAfterFunc)(const a8::XParams& param);
|
|
typedef long long (*XGetTickCountFunc)(void*);
|
|
|
|
class XTimer
|
|
{
|
|
private:
|
|
XTimer();
|
|
~XTimer();
|
|
|
|
public:
|
|
void Init(XGetTickCountFunc func, void* context, int gc_time, int cache_timer_num);
|
|
|
|
void Update();
|
|
|
|
//添加截止时间定时器(一次性定时器)
|
|
xtimer_list* AddDeadLineTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
|
|
a8::XTimerAfterFunc timer_after_func = nullptr);
|
|
xtimer_list* AddDeadLineTimerAndAttach(int expire_time, a8::XParams,
|
|
a8::XTimerFunc timer_func,
|
|
list_head* attach_list,
|
|
a8::XTimerAfterFunc timer_after_func = nullptr);
|
|
//添加重复执行定时器(周期性定时器)
|
|
xtimer_list* AddRepeatTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func);
|
|
//修改定时器参数
|
|
void ModifyTimer(xtimer_list* timer, int expire_time);
|
|
//删除定时器
|
|
void DeleteTimer(xtimer_list* timer);
|
|
//通过关联的list_head获取定时器对象
|
|
xtimer_list* GetTimerByAttach(list_head* attach_entry);
|
|
|
|
private:
|
|
void UpdateTimer();
|
|
xtimer_list* NewTimerList();
|
|
void AddToFreeList(xtimer_list* timer);
|
|
void Clear();
|
|
static void GC_XTimerFunc(const a8::XParams& param);
|
|
|
|
private:
|
|
xtvec_base* base_ = nullptr;
|
|
XGetTickCountFunc get_tick_count_func_ = nullptr;
|
|
void* context_ = nullptr;
|
|
int gc_time_ = 10;
|
|
int cache_timer_num_ = 100;
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|