码迷,mamicode.com
首页 > 其他好文 > 详细

libevent(三)event_base

时间:2017-10-17 12:18:42      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:class   复用   hat   efi   ack   end   color   pointer   null   

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
};

 

libevent(三)event_base

标签:class   复用   hat   efi   ack   end   color   pointer   null   

原文地址:http://www.cnblogs.com/gattaca/p/7680389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!