66 lines
2.2 KiB
C++
66 lines
2.2 KiB
C++
/*
|
|
* 仿照linux2.6内核定时器实现
|
|
*/
|
|
#ifndef A8_TIMER_H
|
|
#define A8_TIMER_H
|
|
|
|
struct timer_list;
|
|
struct tvec_base;
|
|
|
|
namespace a8
|
|
{
|
|
typedef void (*TimerFunc)(const a8::XParams& param);
|
|
typedef void (*TimerAfterFunc)(const a8::XParams& param);
|
|
|
|
class Timer : public a8::Singleton<Timer>
|
|
{
|
|
private:
|
|
Timer() {};
|
|
friend class a8::Singleton<Timer>;
|
|
|
|
public:
|
|
void Init();
|
|
void UnInit();
|
|
void Update();
|
|
|
|
//添加截止时间定时器(一次性定时器)
|
|
timer_list* AddDeadLineTimer(int milli_seconds, a8::XParams param, a8::TimerFunc timer_func,
|
|
a8::TimerAfterFunc timer_after_func = nullptr);
|
|
timer_list* AddDeadLineTimerAndAttach(int milli_seconds, a8::XParams,
|
|
a8::TimerFunc timer_func,
|
|
list_head* attach_list,
|
|
a8::TimerAfterFunc timer_after_func = nullptr);
|
|
//添加重复执行定时器(周期性定时器)
|
|
timer_list* AddRepeatTimer(int milli_seconds, a8::XParams param, a8::TimerFunc timer_func);
|
|
//添加固定时间定时器(每天,每次重启一天里可能会多次调用)
|
|
timer_list* AddFixedTimer(int milli_seconds, a8::XParams param, a8::TimerFunc timer_func);
|
|
//修改定时器参数
|
|
void ModifyTimer(timer_list* timer, int milli_seconds);
|
|
//删除定时器
|
|
void DeleteTimer(timer_list* timer);
|
|
//通过关联的list_head获取定时器对象
|
|
timer_list* GetTimerByAttach(list_head* attach_entry);
|
|
//获取定时器关联参数
|
|
a8::XParams* MutableParams(timer_list* timer);
|
|
//获取定时器剩余时间
|
|
long long GetRemainTime(timer_list* timer);
|
|
//获取当前正在运行的定时器
|
|
timer_list* GetRunningTimer();
|
|
//获取定时器可怠速时间mill seconds
|
|
int GetIdleableMillSeconds();
|
|
|
|
private:
|
|
void UpdateTimer();
|
|
timer_list* NewTimerList();
|
|
void AddToFreeList(timer_list* timer);
|
|
void Clear();
|
|
static void GC_TimerFunc(const a8::XParams& param);
|
|
|
|
private:
|
|
tvec_base* base_ = nullptr;
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|