libevent - note 之 时间管理 - 时间缓存与校正
上一文章中,知道在libevent中,用了小根堆对Timer事件进行管理,其key值对应事件
的超时时间,从小根堆中可以取出最小的超时时间
对于Timer,libevnet有一个时间管理机制,即下面要说到的等待时间管理,时间缓存校正等
1.等待时间处理
- timeout_next()函数 – 计算等待时间
对于Timer事件,什么时候执行需要根据对应事件的超时时间来定,所以要知道相应的最短的等待时间可以取出小根堆里最小的超时时间进行计算
static int
timeout_next(struct event_base *base, struct timeval **tv_p)
{
struct timeval now;
struct event *ev;
struct timeval *tv = *tv_p;
int res = 0;
//堆的首元素具有最小的超时值
ev = min_heap_top(&base->timeheap);
if (ev == NULL) {
/* if no time-based events are active wait for I/O */
*tv_p = NULL;//如果没有定时事件,将等待时间设置为NULL,表示一直阻塞直到有I/O事件发生
...
}
if (gettime(base, &now) == -1) {
res = -1;
}
//如果超时时间<=当前值,不能等待,需要立即返回
if (evutil_t