1、 libevent库初始化,使用以下两个接口初始化event库,并且初始化一个event
struct event_base * event_base_new(void);
int event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg);
2、添加定时器事件到event
int event_add(struct event *ev, const struct timeval *tv);
对于定时器事件,该接口所做主要工作:
1)、初始化小根堆堆空间
2)、将该event插入到小根堆中
注意小根堆是由base维护的
3、 进入事件主循环
int event_base_dispatch(struct event_base *event_base);
在事件主循环中调用接口
static int timeout_next(struct event_base *base, struct timeval **tv_p)
取出小根堆堆顶的timeout,注意这个timeout是相对于绝对时间的相对时间使用该timeout令接口 base->evsel->dispatch(base, tv_p)阻塞等待;
该接口返回后调用接口timeout_process(base)将超时事件插入到激活链表,并更新变量event_count_active;
然后调用接口event_process_active(base) 处理激活链表任务对应的回调函数即超时事件。