解析libevent 1.4.14 版本如何实现不同操作系统下配置使用I/O demultiplex机制:
一、定义
libevent在编译阶段选择系统的I/O demultiplex机制,而不支持在运行阶段根据配置选择。
Libevent支持多种I/O多路复用技术的关键就在于结构体eventop,它的成员是一系列的函数指针, 定义在event-internal.h文件中:
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; }; |
在libevent中,每种I/O demultiplex机制的实现都必须提供这五个函数接口,来完成自身的初始化、销毁释放;对事件的注册、注销和分发。
通过宏定义在编译的时候选择相应的I/O多路复用对象,同时用extern申明要取的对象是在别处定义的。 event.c 文件中:
#ifdef HAVE_EVENT_PORTS extern const struct eventop evportops; #endif #ifdef HAVE_SELECT extern const struct eventop selectops; #endif #ifdef HAVE_POLL extern const struct eventop pollops; #endif #ifdef HAVE_EPOLL extern const struct eventop epollops; #endif #ifdef HAVE_WORKING_KQUEUE extern const struct eventop kqops; #endif #ifdef HAVE_DEVPOLL extern const struct eventop devpollops; #endif #ifdef WIN32 extern const struct eventop win32ops; #endif |