// 默认情况下new eventbase
struct event_base *
event_base_new(void)
{
struct event_base *base = NULL;
//初始化系统配置
struct event_config *cfg = event_config_new(); // 使用默认的配置方法
if (cfg) {
//再利用事件的系统配置去初始化base event
base = event_base_new_with_config(cfg);
event_config_free(cfg);
}
return base;
} event_config 结构如下 struct event_config {
  //我们知道libevent 支持很多io模型 select poll epoll devpoll win32等
//这里的entries 是一个队列里面存放着要避免的方法 event_config_entry{ const char *avoid_method, 双向表指针}
TAILQ_HEAD(event_configq, event_config_entry) entries;
//初始化占用cpu个数
int n_cpus_hint;
//反应堆检查事件的间隔
struct timeval max_dispatch_interval;
//指两次事件间隔最多调用事件个数
int max_dispatch_callbacks;
//若优先级别低于它就不会执行上两项
int limit_callbacks_after_prio;
//支持后台方法的特性 如epoll 模型想要采用ET 即EV_FEATURE_ET 还用延迟关闭等选项
enum event_method_feature require_features; enum event_base_config_flag flags;
}; /*
对于event_config的初始化都有相关接口
例如:
  entries 不希望采用的后台模型 event_config_avoid_method() 它会将所有要避免的模型加入队列 event_get_supported_method 得到所有避免的模型
  event_config_set_num_cpus_hint 设置cpu个数 event_config_set_max_dispatch_interval(struct event_config *cfg,
const struct timeval *max_interval, int max_callbacks,
int min_priority);
详细说明一下:
Record an interval and/or a number of callbacks after which the event base
should check for new events. By default, the event base will run as many
events are as activated at the higest activated priority before checking
for new events. If you configure it by setting max_interval, it will check
the time after each callback, and not allow more than max_interval to
elapse before checking for new events. If you configure it by setting
max_callbacks to a value >= 0, it will run no more than max_callbacks
callbacks before checking for new events.
记录反应堆应该监测新事件时间间隔或者回调函数的个数。 一般来说 反应堆会在检查新事件之前会运行所有的激活的事件。
如果你配置了max_interval, 它会在每次callback之后检查时间, 保证反应堆每次去check新事件的时间间隔不允许超过最大间隔。
设置max_callbacks ,意思就是说每次监测新事件的间隔 最多可以有max_callbacks个回调被执行。 This option can decrease the latency of high-priority events, and
avoid priority inversions where multiple low-priority events keep us from
polling for high-priority events, but at the expense of slightly decreasing
the throughput. Use it with caution!
这个选项能够减少潜在的高优先级事件, 避免低优先级事件阻碍我们及时处理高优先级事件,但是由于处理激活事件受到了限制
它的吞吐量将减小, 小心使用。
@param cfg The event_base configuration object.
@param max_interval An interval after which Libevent should stop running
callbacks and check for more events, or NULL if there should be
no such interval.
@param max_callbacks A number of callbacks after which Libevent should
stop running callbacks and check for more events, or -1 if there
should be no such limit.
min_priority 事件优先级低于它 就不会执行上面俩个参数的设置
0 表示任何优先级都会执行 若是1 就是表示优先级是1或在在1之上就会执行
@param min_priority A priority below which max_interval and max_callbacks
should not be enforced. If this is set to 0, they are enforced
for events of every priority; if it's set to 1, they're enforced
for events of priority 1 and above, and so on.
@return 0 on success, -1 on failure. int event_config_require_features(struct event_config *cfg, int feature);//设置feature
int event_config_set_flag(struct event_config *cfg, int flag);
event_base_config_flag:
这个字段:
enum event_base_config_flag {
/** Do not allocate a lock for the event base, even if we have
locking set up.
Setting this option will make it unsafe and nonfunctional to call
functions on the base concurrently from multiple threads.
*/
//不分配锁 用于是否选择多线程
EVENT_BASE_FLAG_NOLOCK = 0x01,
/** Do not check the EVENT_* environment variables when configuring
an event_base */
//是否监测EVENT_*环境变量
EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
//windows 选项
EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
/** Instead of checking the current time every time the event loop is
ready to run timeout callbacks, check after each timeout callback.
*/
//在超时callback之后check 当前时间 而不是在准备运行timeout callbacks时
EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08, /** If we are using the epoll backend, this flag says that it is
safe to use Libevent's internal change-list code to batch up
adds and deletes in order to try to do as few syscalls as
possible. Setting this flag can make your code run faster, but
it may trigger a Linux bug: it is not safe to use this flag
if you have any fds cloned by dup() or its variants. Doing so
will produce strange and hard-to-diagnose bugs. This flag can also be activated by setting the
EVENT_EPOLL_USE_CHANGELIST environment variable. This flag has no effect if you wind up using a backend other than
epoll.
*/
//使用changelist 减少系统调用 但是可能是不安全的
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10, /** Ordinarily, Libevent implements its time and timeout code using
the fastest monotonic timer that we have. If this flag is set,
however, we use less efficient more precise timer, assuming one is
present.
*/
//选则计时器
//通常情况下,Libevent将使用最快计时器。 但是如果这个标志被设置,我们使用更低效的更精确的计时器。
EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
};
*/ //默认情况下 产生的event_config
struct event_config *
event_config_new(void)
{
struct event_config *cfg = mm_calloc(, sizeof(*cfg)); if (cfg == NULL)
return (NULL); TAILQ_INIT(&cfg->entries);
cfg->max_dispatch_interval.tv_sec = -;
cfg->max_dispatch_callbacks = INT_MAX;
//优先级低于1的话就不会有上述设置 这里的优先级别越小优先级就越大
//那么这里默认就不使用上述设置
cfg->limit_callbacks_after_prio = ;
return (cfg);
} //根据event config 初始化反应堆
struct event_base *
event_base_new_with_config(const struct event_config *cfg)
{
int i;
struct event_base *base;
int should_check_environment;
。。。。。
根据cfg填充base的各个字段
return (base);
}

Libevent 反应堆的初始化的更多相关文章

  1. libevent源码深度剖析

    原文地址: http://blog.csdn.net/sparkliang/article/details/4957667 第一章 1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多 ...

  2. libevent(了解)

    1 前言 Libevent是一个轻量级的开源高性能网络库,使用者众多,研究者更甚,相关文章也不少.写这一系列文章的用意在于,一则分享心得:二则对libevent代码和设计思想做系统的.更深层次的分析, ...

  3. libevent 源码分析

    1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多,研究者更甚,相关文章也不少.写这一系列文章的用意在于,一则分享心得:二则对libevent代码和设计思想做系统的.更深层次的分析, ...

  4. libevent功能使用简介

    http://blog.csdn.net/happyanger6/article/details/7272324 1. 介绍 libevent是一个用来开发可扩展的网络服务器的事件通知函数库.当一个文 ...

  5. libevent的作用或者说是有哪些功能

    1. 介绍 libevent是一个用来开发可扩展的网络服务器的事件通知函数库.当一个文件描述符上的特定事件发生或是一个超时时间到达后,libevent API提供一种执行回调函数的机制.而且,libe ...

  6. libevent源码深度剖析八

    libevent源码深度剖析八 ——集成信号处理 张亮 现在我们已经了解了libevent的基本框架:事件管理框架和事件主循环.上节提到了libevent中I/O事件和Signal以及Timer事件的 ...

  7. 示例的libevent的程序

    著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:auxten 链接:http://zhuanlan.zhihu.com/auxten/20315482 来源:知乎 /* ...

  8. Windows下libevent C++封装类实现(为什么要使用封装好的网络库?)

    题记 windows平台下对于服务器高并发的网络模型选型中,使用libevent是个不错的选择. 本文的背景基于:国内博客对于libevent大多介绍linux实现,大多是c语言的实现,Windows ...

  9. Windows下libevent C++封装类实现

    题记 windows平台下对于服务器高并发的网络模型选型中,使用libevent是个不错的选择. 本文的背景基于:国内博客对于libevent大多介绍linux实现,大多是c语言的实现,Windows ...

随机推荐

  1. Android 自定义View实现QQ运动积分抽奖转盘

    因为偶尔关注QQ运动, 看到QQ运动的积分抽奖界面比较有意思,所以就尝试用自定义View实现了下,原本想通过开发者选项查看下界面的一些信息,后来发现积分抽奖界面是在WebView中展示的,应该是在H5 ...

  2. 运用El表达式截取字符串/获取list的长度

    ${fn:substring(wjcd.lrsj, 0, 16)} 使用functions函数来获取list的长度 ${fn:length(list)} 引入 <%@ taglib prefix ...

  3. struts2系列(四):struts2国际化的多种方式

    一.struts2国际化原理 根据不同的Locale读取不同的文本. 例如有两个资源文件: 第一个:message_zh_CN.properties 第二个:message_en_US.propert ...

  4. Quart.Net分布式任务管理平台

           无关主题:一段时间没有更新文章了,与自己心里的坚持还是背驰,虽然这期间在公司做了统计分析,由于资源分配问题,自己或多或少的原因,确实拖得有点久了,自己这段时间也有点松懈,借口就不说那么多 ...

  5. fiddler学习资源

    小坦克   fiddler教程:http://www.cnblogs.com/TankXiao/archive/2012/04/25/2349049.htmlps:另外博主其他测试文章也值得一看 涂根 ...

  6. C++获取Windows7 32位系统中所有进程名(类似于任务管理器中的进程)

    代码是网上查找资料,然后自己调试,修改之后可以运行. 系统:win7 32位,VS2008 ------------------------------------------------------ ...

  7. 2016-2017 ACM-ICPC, Asia Tsukuba Regional Contest D Hidden Anagrams

    题目链接:http://codeforces.com/gym/101158/attachments /* * @Author: lyucheng * @Date: 2017-10-21 12:20:0 ...

  8. 【JDK1.8】JDK1.8集合源码阅读——LinkedHashMap

    一.前言 在上一篇随笔中,我们分析了HashMap的源码,里面涉及到了3个钩子函数,用来预设给子类--LinkedHashMap的调用,所以趁热打铁,今天我们来一起看一下它的源码吧. 二.Linked ...

  9. RPC 调用简述

    首先了解什么叫RPC,为什么要RPC,RPC是指远程过程调用,也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网 ...

  10. 一款非常推荐的用户界面插件----EasyUI

      前  言    easyui是一种基于jQuery的用户界面插件集合. easyui为创建现代化,互动,JavaScript应用程序,提供必要的功能. 使用easyui你不需要写很多代码,你只需要 ...