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)中的事件结构和管理的更多相关文章

  1. libevent中的事件机制

    libevent是事件驱动的网络库,事件驱动是他的核心,所以理解事件驱动对于理解整个网络库有很重要的意义.       本着从简入繁,今天分析下单线程最简单的事件触发.通过sample下的event- ...

  2. libevent源码分析二--timeout事件响应

    libevent不仅支持io事件,同时还支持timeout事件与signal事件,这篇文件将分析libevent是如何组织timeout事件以及如何响应timeout事件. 1.  min_heap ...

  3. libevent源码分析一--io事件响应

    这篇文章将分析libevent如何组织io事件,如何捕捉事件的发生并进行相应的响应.这里不会详细分析event与event_base的细节,仅描述io事件如何存储与如何响应. 1.  select l ...

  4. 怎么理解js中的事件委托

    怎么理解js中的事件委托 时间 2015-01-15 00:59:59  SegmentFault 原文  http://segmentfault.com/blog/sunchengli/119000 ...

  5. jQuery:详解jQuery中的事件(二)

    上一篇讲到jQuery中的事件,深入学习了加载DOM和事件绑定的相关知识,这篇主要深入讨论jQuery事件中的合成事件.事件冒泡和事件移除等内容. 接上篇jQuery:详解jQuery中的事件(一) ...

  6. jQuery学习笔记(三)jQuery中的事件

    目录 加载DOM 事件绑定 合成事件 事件冒泡 移除事件 一.加载DOM Javascript 与HTML之间的交互是通过用户操作浏览器页面引发的事件来处理的.jQuery提供了丰富的事件处理机制.从 ...

  7. JS 中的事件绑定、事件监听、事件委托

    事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...

  8. javascript 中的事件机制

    1.javascript中的事件. 事件流 javascript中的事件是以一种流的形式存在的. 一个事件会也有多个元素同时响应. 有时候这不是我们想要的效果, 我们只是需要某个特定的元素相应我们的绑 ...

  9. jQuery中的事件与动画 (你的明天Via Via)

    众所周知,页面在加载时,会触发load事件:当用户单击某个按钮时,会触发该按钮的click事件. 这些事件就像日常生活中,人们按下开关,灯就亮了(或者灭了),往游戏机里投入游戏币就可以启动游戏一样, ...

随机推荐

  1. Java Swing学习

    在Java学习的过程中,我们时常会因为控制台程序的枯燥而失去了学习Java的乐趣,那么今天我们就开始学习Java的Swing.也就是GUI(Graphical user interface),在应用到 ...

  2. SQL&SQLite

    注册博客园有一年多了,每次都是来找点资料,从来没有写过点什么,促使我开始写博客的原因主要有两点 一是在查找资料的过程中,经常需要重复的查找某个知识点,一个知识点时间长了之后总是忘记,这样重复的过程却是 ...

  3. Muduo阅读笔记---入门(一)

    第一步:下载源码和文档 下载muduo项目的源码.<muduo-manual.pdf>文档,以及<Linux多线程服务端编程:使用muduo C++网络库.pdf>,这些是前期 ...

  4. Leetcode题解(28)

    90. Subsets II 题目 分析:代码如下 class Solution { public: vector<vector<int> > subsetsWithDup(v ...

  5. 在找一份相对完整的Webpack项目配置指南么?这里有

    Webpack已经出来很久了,相关的文章也有很多,然而比较完整的例子却不是很多,让很多新手不知如何下脚,下脚了又遍地坑 说实话,官方文档是蛮乱的,而且有些还是错的错的..很多配置问题只有爬过坑才知道 ...

  6. Can you answer these queries?

    Can you answer these queries? Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & ...

  7. 网络基础二 tcp/ip协议簇 端口 三次握手 四次挥手 11种状态集

    第1章 概念介绍 1.1 VLAN 1.1.1 什么是VLAN VLAN(Virtual LAN),翻译成中文是“虚拟局域网”.LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成 ...

  8. HQL语法

    HQL:Hibernate Query Language HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征. $下面介绍HQL语句的语法 1.from子句 from Person 表明从P ...

  9. display: run-in

    If a sibling block box (that does not float and is not absolutely positioned) follows the run-in box ...

  10. jQuery选择器(基本过滤选择器)第三节

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...