加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Libevent源码分析-----跨平台Reactor接口的实现

发布时间:2020-12-15 05:24:28 所属栏目:百科 来源:网络整理
导读:之前的博文讲了怎么实现线程、锁、内存分配、日志等功能的跨平台。Libevent最重要的跨平台功能还是实现了多路IO接口的跨平台(即Reactor模式)。这使得用户可以在不同的平台使用统一的接口。这篇博文就是来讲解Libevent是怎么实现这一点的。 Libevent在实现线



之前的博文讲了怎么实现线程、锁、内存分配、日志等功能的跨平台。Libevent最重要的跨平台功能还是实现了多路IO接口的跨平台(即Reactor模式)。这使得用户可以在不同的平台使用统一的接口。这篇博文就是来讲解Libevent是怎么实现这一点的。

Libevent在实现线程、内存分配、日志时,都是使用了函数指针和全局变量。在实现多路IO接口上时,Libevent也采用了这种方式,不过还是有点差别的。


相关结构体:

现在来看一下event_base结构体,下面代码只列出了本文要讲的内容:

[cpp] view plain copy
  1. //event-internal.h文件
  2. structevent_base{
  3. conststructeventop*evsel;
  4. void*evbase;
  5. };
  6. structeventop{
  7. constchar*name;//多路IO复用函数的名字
  8. void*(*init)(structevent_base*);
  9. int(*add)(structevent_base*,evutil_socket_tfd,shortold,87); background-color:inherit; font-weight:bold">shortevents,void*fdinfo);
  10. int(*del)(void*fdinfo);
  11. int(*dispatch)(structtimeval*);
  12. void(*dealloc)(structevent_base*);
  13. intneed_reinit;//是否要重新初始化
  14. //多路IO复用的特征。参考http://blog.csdn.net/luotuo44/article/details/38443569
  15. enumevent_method_featurefeatures;
  16. size_tfdinfo_len;//额外信息的长度。有些多路IO复用函数需要额外的信息
  17. };

可以看到event_base结构体中有一个struct eventop类型指针。而这个struct eventop结构体的成员就是一些函数指针。名称也像一个多路IO复用函数应该有的操作:add可以添加fd,del可以删除一个fd,dispatch可以进入监听。明显只要给event_base的evsel成员赋值就能使用对应的多路IO复用函数了。

选择后端:


可供选择的后端:

现在来看一下有哪些可以用的多路IO复用函数。其实在Libevent的源码目录中,已经为每一个多路IO复用函数专门创建了一个文件,如select.c、poll.c、epoll.c、kqueue.c等。

打开这些文件就可以发现在文件的前面都会声明一些多路IO复用的操作函数,而且还会定义一个struct eventop类型的全局变量。如下面代码所示:

    //select.c文件
  1. staticvoid*select_init(staticintselect_add(int,153); background-color:inherit; font-weight:bold">void*);
  2. intselect_del(void*);
  3. intselect_dispatch(voidselect_dealloc(structeventopselectops={
  4. "select",
  5. select_init,
  6. select_add,
  7. select_del,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> select_dispatch,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> select_dealloc,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> 0,/*doesn'tneedreinit.*/
  8. EV_FEATURE_FDS,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> };
  9. //poll.c文件
  10. void*poll_init(intpoll_add(void*_idx);
  11. intpoll_del(void*_idx);
  12. intpoll_dispatch(structtimeval*);
  13. voidpoll_dealloc(structeventoppollops={
  14. "poll",
  15. poll_init,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> poll_add,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> poll_del,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> poll_dispatch,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> poll_dealloc,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> 0,0); background-color:inherit">/*doesn'tneed_reinit*/
  16. EV_FEATURE_FDS,153); background-color:inherit; font-weight:bold">sizeof(structpollidx),108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> };

如何选定后端:

看到这里,读者想必已经知道,只需将对应平台的多路IO复用函数的全局变量赋值给event_base的evsel变量即可。可是怎么让Libevent根据不同的平台选择不同的多路IO复用函数呢?另外像大部分OS都会实现select、poll和一个自己的高效多路IO复用函数。怎么从多个中选择一个呢?下面看一下Libevent的解决方案吧:

    //event.c文件
  1. #ifdef_EVENT_HAVE_EVENT_PORTS
  2. externstructeventopevportops;
  3. #endif
  4. #ifdef_EVENT_HAVE_SELECT
  5. structeventopselectops;
  6. #endif
  7. #ifdef_EVENT_HAVE_POLL
  8. structeventoppollops;
  9. #ifdef_EVENT_HAVE_EPOLL
  10. structeventopepollops;
  11. #ifdef_EVENT_HAVE_WORKING_KQUEUE
  12. structeventopkqops;
  13. #ifdef_EVENT_HAVE_DEVPOLL
  14. structeventopdevpollops;
  15. #ifdefWIN32
  16. structeventopwin32ops;
  17. /*Arrayofbackendsinorderofpreference.*/
  18. structeventop*eventops[]={
  19. &evportops,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #ifdef_EVENT_HAVE_WORKING_KQUEUE
  20. &kqops,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #ifdef_EVENT_HAVE_EPOLL
  21. &epollops,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> &devpollops,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> &pollops,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> &selectops,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> &win32ops,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> NULL
  22. };

它根据宏定义判断当前的OS环境是否有某个多路IO复用函数。如果有,那么就把与之对应的struct eventop结构体指针放到一个全局数组中。有了这个数组,现在只需将数组的某个元素赋值给evsel变量即可。因为是条件宏,在编译器编译代码之前完成宏的替换,所以是可以这样定义一个数组的。关于这些检测当前OS环境的宏,可以参考《event-config.h指明所在系统的环境》。

从数组的元素可以看到,低下标存的是高效多路IO复用函数。如果从低到高下标选取一个多路IO复用函数,那么将优先选择高效的。


具体实现:

现在看一下Libevent是怎么选取一个多路IO复用函数的:
    structevent_base*
  1. event_base_new_with_config(structevent_config*cfg)
  2. {
  3. inti;
  4. structevent_base*base;
  5. intshould_check_environment;
  6. //分配并清零event_base内存.event_base里的所有成员都会为0
  7. if((base=mm_calloc(1,153); background-color:inherit; font-weight:bold">structevent_base)))==NULL){
  8. event_warn("%s:calloc",__func__);
  9. returnNULL;
  10. }
  11. ...
  12. should_check_environment=
  13. !(cfg&&(cfg->flags&EVENT_BASE_FLAG_IGNORE_ENV));
  14. //遍历数组的元素
  15. for(i=0;eventops[i]&&!base->evbase;i++){
  16. if(cfg!=NULL){
  17. /*determineifthisbackendshouldbeavoided*/
  18. if(event_config_is_avoided_method(cfg,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> eventops[i]->name))
  19. continue;
  20. if((eventops[i]->features&cfg->require_features)
  21. !=cfg->require_features)
  22. continue;
  23. }
  24. /*alsoobeytheenvironmentvariables*/
  25. if(should_check_environment&&
  26. event_is_method_disabled(eventops[i]->name))
  27. //找到了一个满足条件的多路IO复用函数
  28. base->evsel=eventops[i];
  29. //初始化evbase,后面会说到
  30. base->evbase=base->evsel->init(base);
  31. if(base->evbase==NULL){
  32. event_warnx("%s:noeventmechanismavailable",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> __func__);
  33. base->evsel=NULL;
  34. event_base_free(base);
  35. returnNULL;
  36. ....
  37. return(base);
  38. }

可以看到,首先从eventops数组中选出一个元素。如果设置了event_config,那么就对这个元素(即多路IO复用函数)特征进行检测,看其是否满足event_config所描述的特征。关于event_config,可以查看《多路IO复用函数的选择配置》。


后端数据存储结构体:

在本文最前面列出的event_base结构体中,除了evsel变量外,还有一个evbase变量。这也是一个很重要的变量,而且也是用于跨平台的。

像select、poll、epoll之类多路IO复用函数在调用时要传入一些数据,比如监听的文件描述符fd,监听的事件有哪些。在Libevent中,这些数据都不是保存在event_base这个结构体中的,而是存放在evbase这个指针指向的一个结构体中。


IO复用结构体:

由于不同的多路IO复用函数需要使用不同格式的数据,所以Libevent为每一个多路IO复用函数都定义了专门的结构体(即结构体是不同的),本文姑且称之为IO复用结构体。evbase指向的就是这些结构体。由于这些结构体是不同的,所以要用一个void类型指针。

在select.c、poll.c这类文件中都定义了属于自己的IO复用结构体,如下面代码所示:
    structselectop{
  1. intevent_fds;/*Highestfdinfdset*/
  2. intevent_fdsz;
  3. intresize_out_sets;
  4. fd_set*event_readset_in;
  5. fd_set*event_writeset_in;
  6. fd_set*event_readset_out;
  7. fd_set*event_writeset_out;
  8. structpollop{
  9. intevent_count;/*Highestnumberalloc*/
  10. intnfds;/*Highestnumberused*/
  11. intrealloc_copy;/*Trueiffwemustrealloc
  12. *event_set_copy*/
  13. structpollfd*event_set;
  14. structpollfd*event_set_copy;
  15. };

前面event_base_new_with_config的代码中,有下面一行代码:

    base->evbase=base->evsel->init(base);
明显这行代码就是用来赋值evbase的。下面是poll对应的init函数:

    //poll.c文件
  1. void*
  2. poll_init(structevent_base*base)
  3. structpollop*pollop;
  4. if(!(pollop=mm_calloc(1,153); background-color:inherit; font-weight:bold">structpollop))))
  5. return(NULL);
  6. evsig_init(base);//其他的一些初始化
  7. return(pollop);
  8. }

经过上面的一些处理后,Libevent在特定的OS下能使用到特定的多路IO复用函数。在之前博文中说到的evmap_io_add和evmap_signal_add函数中都会调用evsel->add。由于在新建event_base时就选定了对应的多路IO复用函数,给evsel、evbase变量赋值了,所以evsel->add能把对应的fd和监听事件加到对应的IO复用结构体保存。比如poll的add函数在一开始就有下面一行代码:

    structpollop*pop=base->evbase;

当然,poll的其他函数在一开始时也是会有这行代码的,因为要使用到fd和对应的监听事件等数据,就必须要获取那个IO复用结构体。

由于有evsel和evbase这个两个指针变量,当初始化完成之后,再也不用担心具体使用的多路IO复用函数是哪个了。evsel结构体的函数指针提供了统一的接口,上层的代码要使用到多路IO复用函数的一些操作函数时,直接调用evsel结构体提供的函数指针即可。也正是如此,Libevent实现了统一的跨平台Reactor接口。
之前的博文讲了怎么实现线程、锁、内存分配、日志等功能的跨平台。Libevent最重要的跨平台功能还是实现了多路IO接口的跨平台(即Reactor模式)。这使得用户可以在不同的平台使用统一的接口。这篇博文就是来讲解Libevent是怎么实现这一点的。

Libevent在实现线程、内存分配、日志时,都是使用了函数指针和全局变量。在实现多路IO接口上时,Libevent也采用了这种方式,不过还是有点差别的。


structpollop*pop=base->evbase;

当然,poll的其他函数在一开始时也是会有这行代码的,因为要使用到fd和对应的监听事件等数据,就必须要获取那个IO复用结构体。

由于有evsel和evbase这个两个指针变量,当初始化完成之后,再也不用担心具体使用的多路IO复用函数是哪个了。evsel结构体的函数指针提供了统一的接口,上层的代码要使用到多路IO复用函数的一些操作函数时,直接调用evsel结构体提供的函数指针即可。也正是如此,Libevent实现了统一的跨平台Reactor接口。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读