Libevent(2.1.8)中的事件结构和管理
Libevent(2.1.8)中的事件结构体
这里的libevent版本为 2.1.8 。 libevent中事件的结构体struct event,定义在event_struct.h 中, 这里我们简单看一下:
struct event {
    struct event_callback ev_evcallback; //事件的回调函数
    /* for managing timeouts */
    union {
        TAILQ_ENTRY(event) ev_next_with_common_timeout;// 链表形式管理定时 之前只用最小堆管理超时
        int min_heap_idx; //最小堆 管理超时事件
    } ev_timeout_pos;// 二选一
    evutil_socket_t ev_fd; // io事件时为关心的文件描述符,信号事件时为信号值
    struct event_base *ev_base;// 所属事件堆的实例
    union {
        /* used for io events */
        struct {
            LIST_ENTRY (event) ev_io_next;
            struct timeval ev_timeout;
        } ev_io;  //这部分指io事件时 所关心的同一描述符下,所有IO事件的链表
        /* used by signal events */
        struct {
            LIST_ENTRY (event) ev_signal_next;
            short ev_ncalls;
            /* Allows deletes in callback */
            short *ev_pncalls;
        } ev_signal;//同一信号下信号事件链表
    } ev_;
    short ev_events;     //事件的类型  可读可写 超时等
    short ev_res;        /* result passed to event callback */
    struct timeval ev_timeout; //事件超时时间
};
这里可以看到这个版本下的event 结构体比之前的好像少了不少东西, 如之前的结构:
//event提供了函数接口,供Reactor在事件发生时调用,以执行相应的事件处理,
//通常它会绑定一个有效的句柄(ev_fd)做为回调函数的参数.
struct event {
//已注册事件队列入口
TAILQ_ENTRY (event) ev_next;
//已激活事件队列入口
TAILQ_ENTRY (event) ev_active_next;
//信号事件队列入口 不过在以前的分析中可以看到,将来这个队列中的内容会被添加到ev_active_next队列中。
TAILQ_ENTRY (event) ev_signal_next;
//表示该event在定时器事件最小堆min_heap的索引
unsigned int min_heap_idx; /* for managing timeouts */
//该事件所属的反应堆实例
struct event_base *ev_base;
//对于I/O事件,是绑定的文件描述符; 对于signal事件,是绑定的信号.
int ev_fd; //表示事件类型: I/O,定时器或者信号
short ev_events; //事件就绪执行时,将要调用ev_callback 的次数,通常为1
short ev_ncalls; //该事件的超时时间,在定时器最小堆min_heap操作中作为节点值进行比较.
struct timeval ev_timeout; //该事件的优先级,越小越优先.
int ev_pri; /* smaller numbers are higher priority */ //该事件被激活时的回调函数
void (*ev_callback)(int, short, void *arg); //该事件的标记信息,表示其当前的状态,即它在哪个链表中
int ev_flags; ... //其他成员.
};
这里可以看到在老版本的libevent 中event结构体中有回调函数, 参数,优先级参数, 新版本中都被定义在了struct event_callback ev_evcallback;中 如下:
struct event_callback {
    TAILQ_ENTRY(event_callback) evcb_active_next;
    short evcb_flags;     //选择回调类型 由以下四种
    ev_uint8_t evcb_pri;    /* smaller numbers are higher priority */
    ev_uint8_t evcb_closure; 
    /* allows us to adopt for different types of events */
        union {
        void (*evcb_callback)(evutil_socket_t, short, void *);
        void (*evcb_selfcb)(struct event_callback *, void *);
        void (*evcb_evfinalize)(struct event *, void *);
        void (*evcb_cbfinalize)(struct event_callback *, void *);
    } evcb_cb_union;
    void *evcb_arg;
};
比起老版本 新版本又丢弃了TAILQ_ENTRY(event) ev_next; 已注册事件队列入口 ev_active_next 已激活队列入口 ev_signal_next 信号事件队列入口等结构。
Libevent 事件堆实例结构体:
新版本的struct base_event 如下:
struct event_base {
    /** Function pointers and other data to describe this event_base's
     * backend. */
    const struct eventop *evsel;//具体事件处理模型结构(select /poll/epoll)指针
    /** Pointer to backend-specific data. */
    void *evbase;//指向IO复用的真正操作 通过evsel来初始化
    /** List of changes to tell backend about at next dispatch.  Only used
     * by the O(1) backends. */
    struct event_changelist changelist; //???????后续
    /** Function pointers used to describe the backend that this event_base
     * uses for signals */
    const struct eventop *evsigsel;//信号操作
    /** Data to implement the common signal handelr code. */
    struct evsig_info sig;
    /** Number of virtual events */
    int virtual_event_count; //??????后续
    /** Maximum number of virtual events active */
    int virtual_event_count_max; //?????
    /** Number of total events added to this event_base */
    int event_count;   //事件总数
    /** Maximum number of total events added to this event_base */
    int event_count_max;//事件堆最多的事件数量
    /** Number of total events active in this event_base */
    int event_count_active;
    /** Maximum number of total events active in this event_base */
    int event_count_active_max;
    /** Set if we should terminate the loop once we're done processing
     * events. */
    int event_gotterm; //处理完事件后是否终止
    /** Set if we should terminate the loop immediately */
    int event_break; //是否马上终止事件循环
    /** Set if we should start a new instance of the loop immediately. */
    int event_continue; //是否马上开始实例
    /** The currently running priority of events */
    int event_running_priority;  //当前running 事件的优先级
    /** Set if we're running the event_base_loop function, to prevent
     * reentrant invocation. */
    int running_loop; //是否正在loop event
    /** Set to the number of deferred_cbs we've made 'active' in the
     * loop.  This is a hack to prevent starvation; it would be smarter
     * to just use event_config_set_max_dispatch_interval's max_callbacks
     * feature */
    int n_deferreds_queued;
    /* Active event management. */
    /** An array of nactivequeues queues for active event_callbacks (ones
     * that have triggered, and whose callbacks need to be called).  Low
     * priority numbers are more important, and stall higher ones.
     */
    //已激活的回调事件队列  即event_callback 结构的队列
    struct evcallback_list *activequeues;
    /** The length of the activequeues array */
    int nactivequeues; //激活队列长度
    /** A list of event_callbacks that should become active the next time
     * we process events, but not this time. */
    struct evcallback_list active_later_queue;// 下次激活的事件队列?
    /* common timeout logic */
    //超时事件链表与小根堆配合使用
    /** An array of common_timeout_list* for all of the common timeout
     * values we know. */
    struct common_timeout_list **common_timeout_queues;
    /** The number of entries used in common_timeout_queues */
    int n_common_timeouts;
    /** The total size of common_timeout_queues. */
    int n_common_timeouts_allocated;
    /** Mapping from file descriptors to enabled (added) events */
    struct event_io_map io;//存储io 事件
    /** Mapping from signal numbers to enabled (added) events. */
    struct event_signal_map sigmap;//存储信号事件
    /** Priority queue of events with timeouts. */
    struct min_heap timeheap;//小根堆
    /** Stored timeval: used to avoid calling gettimeofday/clock_gettime
     * too often. */
    struct timeval tv_cache;
    struct evutil_monotonic_timer monotonic_timer;
    /** Difference between internal time (maybe from clock_gettime) and
     * gettimeofday. */
    struct timeval tv_clock_diff;
    /** Second in which we last updated tv_clock_diff, in monotonic time. */
    time_t last_updated_clock_diff;
#ifndef EVENT__DISABLE_THREAD_SUPPORT   //多线程支持
    /* threading support */
    /** The thread currently running the event_loop for this base */
    unsigned long th_owner_id;
    /** A lock to prevent conflicting accesses to this event_base */
    void *th_base_lock;
    /** A condition that gets signalled when we're done processing an
     * event with waiters on it. */
    void *current_event_cond;
    /** Number of threads blocking on current_event_cond. */
    int current_event_waiters;
#endif
    /** The event whose callback is executing right now */
    struct event_callback *current_event;
    /** Flags that this base was configured with */
//这里后续的event config 会讲到
    enum event_base_config_flag flags;
    struct timeval max_dispatch_time;
    int max_dispatch_callbacks;
    int limit_callbacks_after_prio;
    /* Notify main thread to wake up break, etc. */
  //多线程   主线程loop过程中一般会阻塞  
  //若这时添加或者删除事件的话, 就需要唤醒阻塞的主线程, 加入了一个未决状态
    /** True if the base already has a pending notify, and we don't need
     * to add any more. */
    int is_notify_pending;
    /** A socketpair used by some th_notify functions to wake up the main
     * thread. */
    evutil_socket_t th_notify_fd[];
    /** An event used by some th_notify functions to wake up the main
     * thread. */
    struct event th_notify;
    /** A function used to wake up the main thread from another thread. */
    int (*th_notify_fn)(struct event_base *base);//唤醒主线程的函数指针
    /** Saved seed for weak random number generator. Some backends use
     * this to produce fairness among sockets. Protected by th_base_lock. */
    struct evutil_weakrand_state weakrand_seed;
    /** List of event_onces that have not yet fired. */
    LIST_HEAD(once_event_list, event_once) once_events;
};
对于事件的存储 老版本中用队列来存储, 在新版本中使用哈希表map 如event_io_map , event_signal_map结构。还有一些多线程支持的参数后续会看到它们的用途。后续还会详细解释其中的各个字段含义。
Libevent(2.1.8)中的事件结构和管理的更多相关文章
- libevent中的事件机制
		
libevent是事件驱动的网络库,事件驱动是他的核心,所以理解事件驱动对于理解整个网络库有很重要的意义. 本着从简入繁,今天分析下单线程最简单的事件触发.通过sample下的event- ...
 - libevent源码分析二--timeout事件响应
		
libevent不仅支持io事件,同时还支持timeout事件与signal事件,这篇文件将分析libevent是如何组织timeout事件以及如何响应timeout事件. 1. min_heap ...
 - libevent源码分析一--io事件响应
		
这篇文章将分析libevent如何组织io事件,如何捕捉事件的发生并进行相应的响应.这里不会详细分析event与event_base的细节,仅描述io事件如何存储与如何响应. 1. select l ...
 - 怎么理解js中的事件委托
		
怎么理解js中的事件委托 时间 2015-01-15 00:59:59 SegmentFault 原文 http://segmentfault.com/blog/sunchengli/119000 ...
 - jQuery:详解jQuery中的事件(二)
		
上一篇讲到jQuery中的事件,深入学习了加载DOM和事件绑定的相关知识,这篇主要深入讨论jQuery事件中的合成事件.事件冒泡和事件移除等内容. 接上篇jQuery:详解jQuery中的事件(一) ...
 - jQuery学习笔记(三)jQuery中的事件
		
目录 加载DOM 事件绑定 合成事件 事件冒泡 移除事件 一.加载DOM Javascript 与HTML之间的交互是通过用户操作浏览器页面引发的事件来处理的.jQuery提供了丰富的事件处理机制.从 ...
 - JS 中的事件绑定、事件监听、事件委托
		
事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...
 - javascript 中的事件机制
		
1.javascript中的事件. 事件流 javascript中的事件是以一种流的形式存在的. 一个事件会也有多个元素同时响应. 有时候这不是我们想要的效果, 我们只是需要某个特定的元素相应我们的绑 ...
 - jQuery中的事件与动画                (你的明天Via Via)
		
众所周知,页面在加载时,会触发load事件:当用户单击某个按钮时,会触发该按钮的click事件. 这些事件就像日常生活中,人们按下开关,灯就亮了(或者灭了),往游戏机里投入游戏币就可以启动游戏一样, ...
 
随机推荐
- 217. Contains Duplicate (leetcode)
			
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
 - JavaScript面向对象深入理解原型
			
原型模式 function Person(){ } Person.prototype.name="Ewarm"; Person.prototype.age="29&quo ...
 - svn: Working copy 'D:\workspace\web\..\..\images' is too old (format 10, created by Subversion 1.6
			
问题:SVN同步或者提交的时候出现类似错误信息: 解决:找到对应目录,删除隐藏文件.SVN 重新提交或更新
 - Yii2之ListView小部件
			
ListView是yii框架中类似GridView,也是用于展示多条数据的小部件,相比GridView,ListView可以更加灵活地设置数据展示的格式. 下面以我自己做的一个使用ListView来展 ...
 - .12-Vue源码之patch(2)
			
快完事咯! 简单看了下patch函数,虽然不长,但是实际上很长很长,慢慢来吧, 首先来个总览: // line-5250 // oldVnode => 原生DOM节点 // vnode => ...
 - JNI 对象处理 (转)
			
JNI 的基本问题就是解决 Java 和 C++ 代码互相调用的通信问题,在 C++ 代码编写过程中最大的问题莫过于适应其中的代码编写规则,C++调用或是返回的内容必须遵守 JVM 和 C++ 代码的 ...
 - windows中通过bat批处理打开exe文件
			
1.想要运行的程序: C:\Program Files\Windows Media Player\wmplayer.exe C:\Program Files\Haihaisoft Universal ...
 - 2301: [HAOI2011]Problem b ( 分块+莫比乌斯反演+容斥)
			
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 6015 Solved: 2741[Submit] ...
 - A - Wrestling Match HDU - 5971
			
Nowadays, at least one wrestling match is held every year in our country. There are a lot of people ...
 - Photoshop颜色出现比较大的偏差,偏色严重,显示器配置文件2351似乎有问题
			
其实出现这个问题是因为 显示器的配置问题.并不是PS版本或者电脑系统问题. 一般在你首次启动PS的时候会出现提示:显示器配置文件2351似乎有问题. 如果你点击了继续运行那以后你使用PS打开任何文件都 ...