前言
上一小节我们介绍了事件是如何注册/注销的,在本小节中,我们将进一步探讨事件从未注册到处理的整个过程,即事件的主循环。
事件主循环
事件的主循环主要是通过event_base_loop
完成的。我们下面先来看看这个函数,再进行总结。
event_base_loop
int
event_base_loop(struct event_base *base, int flags)
{
const struct eventop *evsel = base->evsel;
void *evbase = base->evbase;
struct timeval tv;
struct timeval *tv_p;
int res, done;
/* clear time cache */
base->tv_cache.tv_sec = 0;
//如果有信号事件,则将base赋给专门处理signal的evsignal_base(global var)
if (base->sig.ev_signal_added)
evsignal_base = base;
done = 0; //标识位
while (!done) {
/* Terminate the loop if we have been asked to */
//如果设置了退出标识位,则退出循环
if (base->event_gotterm) {
base->event_gotterm = 0;
break;
}
if (base->event_break) {
base->event_break = 0;
break;
}
/* You cannot use this interface for multi-threaded apps */
while (event_gotsig) {
event_gotsig = 0;
if (event_sigcb) {
res = (*event_sigcb)();
if (res == -1) {
errno = EINTR;
return (-1);
}
}