這裡分析的是libevent-1.4.9。
PS:前面還看了libev的源代碼,媽的,那代碼寫的太猥亵了,相比較libevent代碼寫的好多了。。
首先來看一下最主要的幾個數據結構:
eventop結構體是所有事件驅動模型的基類。所有的io復用類型都會實現此結構體裡各種方法。
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管理所有的event對象,它包含了一些全部變量,比如事件驅動引擎evsel等。所有的event對象都會包含這個結構體。
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;
};
event結構表示每個事件,包含了一些事件私有的數據,比如回調函數等。。這裡事件鏈表它使用了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;
};
下面來介紹一下事件引擎中的兩個結構,這裡主要介紹下select,其他的幾本類似.
selectop是全局的select數據結構,也就是上面event_base數據結構中evbase的值,我們通過avbase就可以操作select的數據結構
struct selectop {
int event_fds; /* Highest fd in fd set */
int event_fdsz;
fd_set *event_readset_in;
fd_set *event_writeset_in;
fd_set *event_readset_out;
fd_set *event_writeset_out;
struct event **event_r_by_fd;
struct event **event_w_by_fd;
};
selectops也就是實現了eventop。它導出了select的接口。
const struct eventop selectops = {
"select",
select_init,
select_add,
select_del,
select_dispatch,
select_dealloc,
0
};
我們再來看下幾個重要的函數(其中省略了一些語句,只介紹重要的一些語句):
struct event_base *
event_base_new(void)
{
......................................
//前面就是一些初始化,最重要的部分是下面的這個for循環。在這裡初始化事件驅動引擎。這裡eventops是一個eventop數組,裡面包含所有事件驅動引擎的接口(就像上面介紹的selectops結構)
for (i = 0; eventops[i] && !base->evbase; i++) {
base->evsel = eventops[i];
///下面調用初始化函數,返回每個事件引擎的全局數據結構
base->evbase = base->evsel->init(base);
}
..................................
/* allocate a single active event queue */
event_base_priority_init(base, 1);
return (base);
}
event_init將event_base_new返回的值付給一個全局的變量current_base
Java代碼
struct event_base *
event_init(void)
{
struct event_base *base = event_base_new();
if (base != NULL)
current_base = base;
return (base);
}
event_add加一新的事件到當前事件引擎
int
event_add(struct event *ev, const struct timeval *tv)
{
///取得當前事件的一些有用的數據結構。
struct event_base *base = ev->ev_base;
const struct eventop *evsel = base->evsel;
void *evbase = base->evbase;
int res = 0;
/*
* prepare for timeout insertion further below, if we get a
* failure on any step, we should not change any state.
*/
if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
if (min_heap_reserve(&base->timeheap,
1 + min_heap_size(&base->timeheap)) == -1)
return (-1); /* ENOMEM == errno */
}
///這裡調用evsel->add來加一事件到當前的事件引擎。
if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) &&
!(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) {
res = evsel->add(evbase, ev);
if (res != -1)
event_queue_insert(base, ev, EVLIST_INSERTED);
}
/*
* we should change the timout state only if the previous event
* addition succeeded.
*/
if (res != -1 && tv != NULL) {
struct timeval now;
/*
* we already reserved memory above for the case where we
* are not replacing an exisiting timeout.
*/
//如果超時就先刪除此事件。
if (ev->ev_flags & EVLIST_TIMEOUT)
event_queue_remove(base, ev, EVLIST_TIMEOUT);
/* Check if it is active due to a timeout. Rescheduling
* this timeout before the callback can be executed
* removes it from the active list. */
if ((ev->ev_flags & EVLIST_ACTIVE) &&
(ev->ev_res & EV_TIMEOUT)) {
/* See if we are just active executing this
* event in a loop
*/
if (ev->ev_ncalls && ev->ev_pncalls) {
/* Abort loop */
*ev->ev_pncalls = 0;
}
event_queue_remove(base, ev, EVLIST_ACTIVE);
}
gettime(base, &now);
//計算超時時間並賦值給ev_timeout
evutil_timeradd(&now, tv, &ev->ev_timeout);
event_debug((
"event_add: timeout in %ld seconds, call %p",
tv->tv_sec, ev->ev_callback));
//將此定時器加入定時器最小堆。
event_queue_insert(base, ev, EVLIST_TIMEOUT);
}
return (res);
}
loop函數將進入事件監測循環。
int
event_base_loop(struct event_base *base, int flags)
{
............................................
.......................................
///校准時間,非Monotonic時鐘這個函數將會立即返回。
timeout_correct(base, &tv);
tv_p = &tv;
if (!base->event_count_active && !(flags & EVLOOP_NONBLOCK)) {
timeout_next(base, &tv_p);
} else {
/*
* if we have active events, we just poll new events
* without waiting.
*/
evutil_timerclear(&tv);
}
/* If we have no events, we just exit */
if (!event_haveevents(base)) {
event_debug(("%s: no events registered.", __func__));
return (1);
}
/* update last old time */
gettime(base, &base->event_tv);
/* clear time cache */
base->tv_cache.tv_sec = 0;
///調用相應的事件引擎處理函數
res = evsel->dispatch(base, evbase, tv_p);
if (res == -1)
return (-1);
gettime(base, &base->tv_cache);
///超時處理函數(下面緊接著會介紹)
timeout_process(base);
if (base->event_count_active) {
event_process_active(base);
if (!base->event_count_active && (flags & EVLOOP_ONCE))
done = 1;
} else if (flags & EVLOOP_NONBLOCK)
done = 1;
}
..................................................
}
超時處理函數
void
timeout_process(struct event_base *base)
{
struct timeval now;
struct event *ev;
if (min_heap_empty(&base->timeheap))
return;
gettime(base, &now);
///遍歷定時器
while ((ev = min_heap_top(&base->timeheap))) {
if (evutil_timercmp(&ev->ev_timeout, &now, >))
break;
/* delete this event from the I/O queues */
event_del(ev);
event_debug(("timeout_process: call %p",
ev->ev_callback));
///激活此事件
event_active(ev, EV_TIMEOUT, 1);
}
}