loop.", __func__));
return (0);
}
à′?′timeout_next·?·¨
static??int
timeout_next(struct??event_base??*base,??struct??timeval??**tv_p)
{
??struct??timeval??now;
??struct??event??*ev;
??struct??timeval??*tv??=??*tv_p;
///è?3?×?D?μ?????ê±???£
??if??((ev??=??min_heap_top(&base->timeheap))??==??NULL)??{
??/*??if??no??time-based??events??are??active??wait??for??I/O??*/
??*tv_p??=??NULL;
??return??(0);
??}
///μ?μ?μ±?°μ?ê±???£
??if??(gettime(base,??&now)??==??-1)
??return??(-1);
///ò?3?ê±?ò?±?óí?3?
??if??(evutil_timercmp(&ev->ev_timeout,??&now,??<=))??{
??evutil_timerclear(tv);
??return??(0);
??}
///???¨ê±?÷ê??t??è¥μ±?°ê±??£?ò2?íê?3?ê±ê±??????tv?£
??evutil_timersub(&ev->ev_timeout,??&now,??tv);
??assert(tv->tv_sec??>=??0);
??assert(tv->tv_usec??>=??0);
??event_debug(("timeout_next:??in??%ld??seconds",??tv->tv_sec));
??return??(0);
}
void
timeout_process(struct??event_base??*base)
{
..............................................
??gettime(base,??&now);
///?aê?±éàú′?ó??è???óáD
??while??((ev??=??min_heap_top(&base->timeheap)))??{
///è?1?±èμ±?°ê±??′ó£??ò?μ?÷?1??μ?3?ê±ê±??òò′??±?óí?3??£
??if??(evutil_timercmp(&ev->ev_timeout,??&now,??>))
????break;
///é?3y′?3?ê±ê??t£?òò′??ò???úê1ó??¨ê±?÷ê±£?Dèòa?ò????′???è??¨ê±?÷oó£??ù′?add′?ê??t?£
??/*??delete??this??event??from??the??I/O??queues??*/
??event_del(ev);
??event_debug(("timeout_process:??call??%p",
????ev->ev_callback));
///?óè??¤???óáD?£
??event_active(ev,??EV_TIMEOUT,??1);
??}
}
其中event_active是通过event_queue_insert来插入到激活队列的,因此我们来看这个函数:
void
event_queue_insert(struct event_base *base, struct event *ev, int queue)
{
..............................................
ev->ev_flags |= queue;
switch (queue) {
///这个主要用来保存所有的激活以及非激活队列,也就是eventqueue.
case EVLIST_INSERTED:
TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
break;
case EVLIST_ACTIVE:
///激活队列数加一,并将此事件插入到相应的优先级的激活队列中。
base->event_count_active++;
TAILQ_INSERT_TAIL(base->activequeues[ev->ev_pri],
ev,ev_active_next);
break;
case EVLIST_TIMEOUT: {
///处理超时事件。
min_heap_push(&base->timeheap, ev);
break;
}
default:
event_errx(1, "%s: unknown queue %x", __func__, queue);
}
}
最后来看执行激活队列
static void
event_process_active(struct event_base *base)
{
struct event *ev;
struct event_list *activeq = NULL;
int i;
short ncalls;
///取出相应的激活队列
for (i = 0; i < base->nactivequeues; ++i) {
if (TAILQ_FIRST(base->activequeues[i]) != NULL) {
activeq = base->activequeues[i];
break;
}
}
assert(activeq != NULL);
///开始遍历上面取出的队列
for (ev = TAILQ_FIRST(activeq); ev; ev =
|