The analysis here is libevent-1.4.9.
PS: The front also looked at the source code of the Libev, the mother, the code is too obscene, compared to libevent code written much better.
First, take a look at some of the most important data structures:
The EVENTOP structure is the base class for all event-driven models. All IO multiplexing types will implement various methods in this structure.
struct eventop {
const char *name; ///<事件驱动名称
void *(*init)(struct event_base *); //<初始化
int (*add)(void *, struct event *); ///<加入新的事件监测
int (*del)(void *, struct event *); ///<从事件监测中删除某一事件
int (*dispatch)(struct event_base *, void *, struct timeval *);///<启动此事件监测
void (*dealloc)(struct event_base *, void *); ///<释放此事件驱动的资源
/* set if we need to reinitialize the event base */
int need_reinit; ///<标志位
};
Event_base manages all event objects, including all variables, such as event-driven engine Evsel. All event objects will contain this structure.
struct event_base {
const struct eventop *evsel; ///<事件驱动引擎
void *evbase; ///<事件驱动引擎的全局数据,在每一个事件引擎文件中定义,下面会介绍.
int event_count; /* counts number of total events */
int event_count_active; /* counts number of active events */
int event_gotterm; /* Set to terminate loop */
int event_break; /* Set to terminate loop immediately */
/* active event management */
struct event_list **activequeues; ///<激活队列
int nactivequeues; ///<激活队列数目
/* signal handling info */
struct evsignal_info sig; ///<信号
struct event_list eventqueue; ///<全部事件队列
struct timeval event_tv;
struct min_heap timeheap; ///<这里libevent将定时器队列实现为一个最小堆,也就是为了每次都把时间最晚的定时器能取出来,然后实现超时。更其实算法很简单,想要详细的了解可以去看下算法导论的第六章的Priority queues.
struct timeval tv_cache;
};
The event structure represents each incident, including some event-private data, such as a callback function. Here the event list it uses the tail queue.
struct event {
TAILQ_ENTRY (event) ev_next; ///<下一个事件
TAILQ_ENTRY (event) ev_active_next; ///<下一个激活事件
TAILQ_ENTRY (event) ev_signal_next; ///<下一个信号事件列表
unsigned int min_heap_idx; /* for managing timeouts */
struct event_base *ev_base; ///<全局的base
int ev_fd; ///<所需要监测的事件句柄
short ev_events;
short ev_ncalls;
short *ev_pncalls; /* Allows deletes in callback */
struct timeval ev_timeout; ///<超时时间
int ev_pri; /* smaller numbers are higher priority */
void (*ev_callback)(int, short, void *arg); ///<回调函数
void *ev_arg; ///<传递给回调函数的参数
int ev_res; /* result passed to event callback */
int ev_flags;
};