在以前博文libevent之Reactor模式中,咱們知道Reactor模式中一個重要的組件就是事件多路分發機制(event demultiplexer)。而在libevent中,對事件多路分發機制的支持依賴於操做系統支持的多路複用機制(select、poll、epoll等)。html
libevent定義了一個頂層的結構體eventop(event option),來抽象不一樣操做系統支持的多路複用機制:數組
// <event_internal.h>
1 /** Structure to define the backend of a given event_base. */ 2 struct eventop { 3 /** The name of this backend. */ 4 const char *name; 5 /** Function to set up an event_base to use this backend. It should 6 * create a new structure holding whatever information is needed to 7 * run the backend, and return it. The returned pointer will get 8 * stored by event_init into the event_base.evbase field. On failure, 9 * this function should return NULL. */ 10 void *(*init)(struct event_base *); 11 /** Enable reading/writing on a given fd or signal. 'events' will be 12 * the events that we're trying to enable: one or more of EV_READ, 13 * EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that 14 * were enabled on this fd previously. 'fdinfo' will be a structure 15 * associated with the fd by the evmap; its size is defined by the 16 * fdinfo field below. It will be set to 0 the first time the fd is 17 * added. The function should return 0 on success and -1 on error. 18 */ 19 int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo); 20 /** As "add", except 'events' contains the events we mean to disable. */ 21 int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo); 22 /** Function to implement the core of an event loop. It must see which 23 added events are ready, and cause event_active to be called for each 24 active event (usually via event_io_active or such). It should 25 return 0 on success and -1 on error. 26 */ 27 int (*dispatch)(struct event_base *, struct timeval *); 28 /** Function to clean up and free our data from the event_base. */ 29 void (*dealloc)(struct event_base *); 30 /** Flag: set if we need to reinitialize the event base after we fork. 31 */ 32 int need_reinit; 33 /** Bit-array of supported event_method_features that this backend can 34 * provide. */ 35 enum event_method_feature features; 36 /** Length of the extra information we should record for each fd that 37 has one or more active events. This information is recorded 38 as part of the evmap entry for each fd, and passed as an argument 39 to the add and del functions above. 40 */ 41 size_t fdinfo_len; 42 };
能夠看到,該結構體已經聲明(非定義)了多路分發機制常備的Reactor初始化、事件添加、事件移除、事件分發及Reactor清理函數,並且均以函數指針的方式定義,便於複用。socket
如epoll對該結構體的一個複用:ide
1 static void *epoll_init(struct event_base *); 2 static int epoll_dispatch(struct event_base *, struct timeval *); 3 static void epoll_dealloc(struct event_base *); 4 5 static const struct eventop epollops_changelist = { 6 "epoll (with changelist)", 7 epoll_init, 8 event_changelist_add, 9 event_changelist_del, 10 epoll_dispatch, 11 epoll_dealloc, 12 1, /* need reinit */ 13 EV_FEATURE_ET|EV_FEATURE_O1, 14 EVENT_CHANGELIST_FDINFO_SIZE 15 };
libevent用了一個數組來存儲其所支持的多路複用機制:函數
1 // <event.c> 2 /* Array of backends in order of preference. */ 3 static const struct eventop *eventops[] = { 4 #ifdef _EVENT_HAVE_EVENT_PORTS 5 &evportops, 6 #endif 7 #ifdef _EVENT_HAVE_WORKING_KQUEUE 8 &kqops, 9 #endif 10 #ifdef _EVENT_HAVE_EPOLL 11 &epollops, 12 #endif 13 #ifdef _EVENT_HAVE_DEVPOLL 14 &devpollops, 15 #endif 16 #ifdef _EVENT_HAVE_POLL 17 &pollops, 18 #endif 19 #ifdef _EVENT_HAVE_SELECT 20 &selectops, 21 #endif 22 #ifdef WIN32 23 &win32ops, 24 #endif 25 NULL 26 };
經過這個程序,咱們能夠知道libevent是經過宏定義來肯定當前操做系統是否支持某中多路複用機制,而且按順序選擇系統支持的機制。oop
另外,咱們若是想知道程序當前所支持的多路複用機制,咱們能夠調用函數event_get_supported_methods:this
1 // <event.c> 2 const char ** 3 event_get_supported_methods(void) 4 { 5 static const char **methods = NULL; 6 const struct eventop **method; 7 const char **tmp; 8 int i = 0, k; 9 10 /* count all methods */ 11 for (method = &eventops[0]; *method != NULL; ++method) { 12 ++i; 13 } 14 15 /* allocate one more than we need for the NULL pointer */ 16 tmp = mm_calloc((i + 1), sizeof(char *)); 17 if (tmp == NULL) 18 return (NULL); 19 20 /* populate the array with the supported methods */ 21 for (k = 0, i = 0; eventops[k] != NULL; ++k) { 22 tmp[i++] = eventops[k]->name; 23 } 24 tmp[i] = NULL; 25 26 if (methods != NULL) 27 mm_free((char**)methods); 28 29 methods = tmp; 30 31 return (methods); 32 }
而要想知道程序實際調用的是哪種多路複用機制,咱們能夠調用函數event_get_method獲得:spa
1 // <event.c> 2 const char * 3 event_get_method(void) 4 { 5 return (current_base->evsel->name); 6 }