一.定时器的数据结构.
skynet定时器采用一个timer的数据结构来保存数据, 如下:
struct timer {
struct link_list near[TIME_NEAR]; //离当前时间最近的事件队列,每次tick的时候从这个队列里面
struct link_list t[4][TIME_LEVEL]; //离当前时间较远的事件队列。
struct spinlock lock;
uint32_t time; //表示每次tick的数量,tick一次 time+1
uint32_t starttime; //进程启动开始的系统时间
uint64_t current; //进程启动开始经过的了多少时间,单位
uint64_t current_point; //进程当前系统时间:单位 1/100秒。
};
二.看一下如何增加一个定时器事件。
static void add_node(struct timer *T,struct timer_node *node) {
uint32_t time=node->expire;
uint32_t current_time=T->time;
if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) {
link(&T->near[time&TIME_NEAR_MASK],node);
} else {
int i;
uint32_t mask=TIME_NEAR << TIME_LEVEL_SHIFT;
for (i=0;i