libeventevent_base
Posted Sawyer Ford
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libeventevent_base相关的知识,希望对你有一定的参考价值。
libevent能够处理三种事件: I/O、定时器、信号。
event_base统一管理所有事件。
struct event_base { const struct eventop *evsel; // backend void *evbase; /** Pointer to backend-specific data. */ const struct eventop *evsigsel; // signal backend struct evsig_info sig; /** Data to implement the common signal handelr code. */ int event_count; // 事件总数 int event_count_active; // 激活事件总数 struct event_list eventqueue; // 存储所有事件 struct event_io_map io; // 存储I/O事件 struct event_signal_map sigmap; // 存储信号事件 struct min_heap timeheap; // 存储定时器事件 /* Active event management. */ /** An array of nactivequeues queues for active events (ones that * have triggered, and whose callbacks need to be called). Low * priority numbers are more important, and stall higher ones. */ struct event_list *activequeues; /** The length of the activequeues array */ int nactivequeues; ... };
eventop
用于描述event_base的底层实现机制
/** Structure to define the backend of a given event_base. */ struct eventop { const char *name; void *(*init)(struct event_base *); int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo); int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo); int (*dispatch)(struct event_base *, struct timeval *); void (*dealloc)(struct event_base *); int need_reinit; enum event_method_feature features; size_t fdinfo_len; };
libevent支持多种平台,因此定义了一个全局数组来存放多个eventop。
/* Array of backends in order of preference. */ static const struct eventop *eventops[] = { #ifdef _EVENT_HAVE_EVENT_PORTS &evportops, #endif #ifdef _EVENT_HAVE_WORKING_KQUEUE &kqops, #endif #ifdef _EVENT_HAVE_EPOLL &epollops, #endif #ifdef _EVENT_HAVE_DEVPOLL &devpollops, #endif #ifdef _EVENT_HAVE_POLL &pollops, #endif #ifdef _EVENT_HAVE_SELECT &selectops, #endif #ifdef WIN32 &win32ops, #endif NULL };
Linux平台的I/O多路复用机制是epoll,对应epollops。
const struct eventop epollops = { "epoll", epoll_init, epoll_nochangelist_add, epoll_nochangelist_del, epoll_dispatch, epoll_dealloc, 1, /* need reinit */ EV_FEATURE_ET|EV_FEATURE_O1, 0 };
以上是关于libeventevent_base的主要内容,如果未能解决你的问题,请参考以下文章