64,682
社区成员
发帖
与我相关
我的任务
分享
void tick()
{
tw_timer* tmp = slots[cur_slot];//取得时间轮上当前槽的头结点
printf("current slot is %d\n", cur_slot);
while (tmp)
{
printf("tick the timer once\n");
/*如果定时器的rotation值大于0,则他的这一轮不起作用*/
if (tmp->rotation > 0)
{
tmp->rotation--;
tmp = tmp->next;
}
/*否则,说明定时器已经到期,于是执行定时任务,然后删除该定时器*/
else
{
tmp->cb_func(tmp->user_data);
if (tmp == slots[cur_slot])
{
printf("delete header in cur_slot\n");
slots[cur_slot] = tmp->next;
delete tmp;
if (slots[cur_slot])
{
slots[cur_slot]->prev = NULL;
}
tmp = slots[cur_slot];
}
else
{
tmp->prev->next = tmp->next;
if (tmp->next)
{
tmp->next->prev = tmp->prev;
}
tw_timer* tmp2 = tmp->next;
delete tmp;
tmp = tmp2;
}
}
}
cur_slot = ++cur_slot % N;/*更新时间轮的当前槽,以反映时间轮的转动*/
}
else
{
tmp->prev->next = tmp->next;
if (tmp->next)
{
tmp->next->prev = tmp->prev;
}
tw_timer* tmp2 = tmp->next;
delete tmp;
tmp = tmp2;
}
else
{
//假如链表现在是1、2、3、4,tmp现在指向的2,
tmp->prev->next = tmp->next;//tmp的上一个的下一个不就是tmp本身么?也就是tmp现在指向了3
if (tmp->next)//这一句是判断tmp的下一个也就是4是否为真
{
tmp->next->prev = tmp->prev;//如果为真把tmp又指向了上一个也就是2,
}
//尤其是上面这一段我很是费解,到底啥意思
//下面这一段tmp指向下一个,然后删了原本的tmp,但是下一个的并没设为头啊,也没设置上一个节点是啥,就把tmp删除立了
tw_timer* tmp2 = tmp->next;
delete tmp;
tmp = tmp2;
}