libev是一个开源库,实现了一个reactor模式事件驱动任务调度库。代码非常精简,包含所有实现的.c文件只有不到5000行。
支持的事件类型:
ev_io
ev_timer
ev_periodic
ev_signal
ev_child
ev_stat
ev_idle
ev_prepare  and ev_check
ev_embed
ev_fork
ev_cleanup
ev_async
常用的事件类型:
ev_io,io就绪事件
ev_timer,定时器事件
ev_signal,信号事件
官方使用例子
ev_io
   static void    stdin_readable_cb (struct ev_loop *loop, ev_io *w, int revents)    {       ev_io_stop (loop, w);      .. read from stdin here (or from w->fd) and handle any I/O errors    }     ...    struct ev_loop *loop = ev_default_init (0);    ev_io stdin_readable;    ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ);    ev_io_start (loop, &stdin_readable);    ev_run (loop, 0);

ev_timer

   static void    one_minute_cb (struct ev_loop *loop, ev_timer *w, int revents)    {      .. one minute over, w is actually stopped right here    }     ev_timer mytimer;    ev_timer_init (&mytimer, one_minute_cb, 60., 0.);    ev_timer_start (loop, &mytimer);
   static void    timeout_cb (struct ev_loop *loop, ev_timer *w, int revents)    {      .. ten seconds without any activity    }     ev_timer mytimer;    ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */    ev_timer_again (&mytimer); /* start timer */    ev_run (loop, 0);     // and in some piece of code that gets executed on any "activity":    // reset the timeout to start ticking again at 10 seconds    ev_timer_again (&mytimer);

ev_signal

   static void    sigint_cb (struct ev_loop *loop, ev_signal *w, int revents)    {      ev_break (loop, EVBREAK_ALL);    }     ev_signal signal_watcher;    ev_signal_init (&signal_watcher, sigint_cb, SIGINT);    ev_signal_start (loop, &signal_watcher);

关键代码分析


ev.h
核心h文件
typedef struct ev_watcher
{
  EV_WATCHER (ev_watcher)
} ev_watcher;
/* shared by all watchers */
#define EV_WATCHER(type)            \
  int active; /* private */            \
  int pending; /* private */            \
  EV_DECL_PRIORITY /* private */        \
  EV_COMMON /* rw */                \
  EV_CB_DECLARE (type) /* private */
libev中的基础数据类型是ev_watcher,所有的事件都可以通过(W)watcher转换成ev_watcher,ev_watcher提供通用的抽象接口ev_start、ev_stop
ev_watcher是一种c方式实现的继承、多态、封装,ev_start和ev_stop就是基类ev_watcher提供的抽象接口
ev_io/ev_timer/ev_async都继承自ev_watcher
ev.c
核心c代码
struct ev_loop
{
    ev_tstamp ev_rt_now;
    #define ev_rt_now ((loop)->ev_rt_now)
    #define VAR(name,decl) decl;
      #include "ev_vars.h"
    #undef VAR
};
reactor的核心结构体,因为成员变量众多,把详细的定义包含在ev_vars.h中。
ev_vars.h
io事件处理器列表,文件描述符fd作为数组下标
fd关注的事件发生后,查找anfds[fd]的事件处理器,检查触发事件并调用fd所有关联的处理函数。
ANFD * anfds;
ANFD的定义在ev.c中
/*保存fd事件信息的结构*/
//loop初始化的时候会初始化一个ANFD数组,每个ANFD表示一个fd对应的这个fd的所有事件信息
typedef struct
{
  //每个fd可以有多个事件
  WL head;
  unsigned char events; /* 事件的类型 */
  unsigned char reify;  /*  (EV_ANFD_REIFY, EV__IOFDSET) */
  unsigned char emask;  /* epoll后端的实际内核mask */
  unsigned char unused;
  //不同多路复用API的专用变量
  //epoll
#if EV_USE_EPOLL
  unsigned int egen;    /* generation counter to counter epoll bugs */
#endif
//windows平台
#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  SOCKET handle;
#endif
#if EV_USE_IOCP
  OVERLAPPED or, ow;
#endif
} ANFD;
ev_io的观察列表,ev_io_start把事件处理器注册到ev_loop的fdchanges列表。
int* fdchanges;
ev_wrap.h
包装器头文件,简化了代码量,也增加了代码的阅读难度
抽几个关键的宏贴出来
#define fdchanges ((loop)->fdchanges)
#define anfds ((loop)->anfds)
这些宏是有益还是有害,仁者见仁智者见智
事件主循环
int ev_run(ev_loop *loop, int flags)
{
    //检查关注fd列表fdchanges,新增加或者修改的fd都保存在这个列表中
    //通过fd查找注册的事件处理器ANFD,检查事件处理器的关注事件
    fd_reify
    调用跨平台的多路复用api,封装过的,epoll的封装在ev_epoll.c
    backend_poll    
    把事件加入待处理列表
    ev_feed_event    
    //调用所有的待处理事件处理器
    ev_invoke_pending
}

libev笔记的更多相关文章

  1. libev事件库学习笔记

    一.libev库的安装 因为个人的学习环境是在ubuntu 12.04上进行的,所以本节仅介绍该OS下的安装步骤. 使用系统工具自动化安装: sudo apt-get install libev-de ...

  2. Libev学习笔记4

    这一节首先分析Libev的定时器部分,然后分析signal部分. 对定时器的使用主要有两个函数: ev_timer_init (&timeout_watcher, timeout_cb, .) ...

  3. Libev学习笔记3

    设置完需要监听的事件之后,就开始event loop了.在Libev中,该工作由ev_run函数完成.它的大致流程如下: int ev_run (EV_P_ int flags) { do { /* ...

  4. Libev学习笔记2

    这一节根据官方文档给出的简单示例,深入代码内部,了解其实现机制.示例代码如下: int main (void) { struct ev_loop *loop = EV_DEFAULT; ev_io_i ...

  5. Libev学习笔记1

    和Libevent相似,Libev是一个高性事件驱动框架,据说性能比Libevent要高,bug比Libevent要少.Libev只是一个事件驱动框架,不是网络库,因为它的内部并没有任何socket编 ...

  6. libev事件库使用笔记

    源码下载地址:http://dist.schmorp.de/libev/ libev是一个高性能的事件循环库,比libevent库的性能要好. 安装: tar -zxf libev-4.15.tar. ...

  7. libev学习笔记

    转 libev的使用--结合Socket编程 作者:cxy450019566 之前自己学过一些libev编程的基础,这次写压测刚好用上了,才算真正动手写了些东西,在这里做一些总结.写这篇文章是为了用浅 ...

  8. blfs(systemv版本)学习笔记-编译安装i3-wm平铺式窗口管理器

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! i3-wm项目的官网:https://i3wm.org/ 首先需要lfs基础上编译安装完整的xorg服务 我的xorg服务编译安 ...

  9. libev 学习使用

    libev 简单的I/O库.  a high performance full featured event loop written in c libev 的大小也比 libevent 小得多并且自 ...

随机推荐

  1. 【英语】Bingo口语笔记(1) - Hold

    how are you holding up? 你还撑得住吧?你还好吧?撑住太不容易了吧!

  2. ios ble 参考

    About Core Bluetooth https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Con ...

  3. mysql 添加索引后 在查询的时候是mysql就自动从索引里面查询了。还是查询的时候有单 独的参数查询索引?

    MYSQL在创建索引后对索引的使用方式分为两种:1 由数据库的查询优化器自动判断是否使用索引:2 用户可在写SQL语句时强制使用索引 下面就两种索引使用方式进行说明第一种,自动使用索引.数据库在收到查 ...

  4. Arduino开发常见错误

    使用Ethernet时需要指定访问服务器的ip,我用的是本机做服务器.但是有一天重启了路由器,ip地址就变了!程序得跟着改! Arduino突然烧写不了程序:可能是正在运行的程序让arduino死机了 ...

  5. 纠结的ARC

    xcode不断进步,在xcode4中引入了ARC的概念.您用或者不用它就在那里,于是有了本文:如何在未使用arc的工程中引入一个使用了arc特性的文件,如何在arc工程中引用未使用arc的文件.其实说 ...

  6. 解决 winform 界面对不齐 z

    一个winform的程序,本机上界面对得很齐,到一到客户的机器上就惨不忍睹,一番研究后搞定: 1. AutoScaleMode = None 2. BackgroundImageLayout = No ...

  7. python+selenium环境搭建

    这里主要基于windows平台. 下载python.http://python.org/getit/ 下载setuptools [python的基础包工具].http://pypi.python.or ...

  8. When not to automate 什么时候不进行自动化

    The cornerstone of test automation is the premise that the expected application behavior is known. W ...

  9. uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)

    [题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...

  10. IOS 异步加载图片

    #import <Foundation/Foundation.h> #import "StringUtils.h" @interface ImageManager : ...