libevent源码分析:eventop

eventop:定义了event_base使用的后端IO复用的一个统一接口

 1 /** Structure to define the backend of a given event_base. */
 2 struct eventop {
 3     /** The name of this backend. */
 4     const char *name;
 5     /** Function to set up an event_base to use this backend.  It should
 6      * create a new structure holding whatever information is needed to
 7      * run the backend, and return it.  The returned pointer will get
 8      * stored by event_init into the event_base.evbase field.  On failure,
 9      * this function should return NULL. */
10     void *(*init)(struct event_base *);
11     /** Enable reading/writing on a given fd or signal.  'events' will be
12      * the events that we're trying to enable: one or more of EV_READ,
13      * EV_WRITE, EV_SIGNAL, and EV_ET.  'old' will be those events that
14      * were enabled on this fd previously.  'fdinfo' will be a structure
15      * associated with the fd by the evmap; its size is defined by the
16      * fdinfo field below.  It will be set to 0 the first time the fd is
17      * added.  The function should return 0 on success and -1 on error.
18      */
19     int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
20     /** As "add", except 'events' contains the events we mean to disable. */
21     int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
22     /** Function to implement the core of an event loop.  It must see which
23         added events are ready, and cause event_active to be called for each
24         active event (usually via event_io_active or such).  It should
25         return 0 on success and -1 on error.
26      */
27     int (*dispatch)(struct event_base *, struct timeval *);
28     /** Function to clean up and free our data from the event_base. */
29     void (*dealloc)(struct event_base *);
30     /** Flag: set if we need to reinitialize the event base after we fork.
31      */
32     int need_reinit;
33     /** Bit-array of supported event_method_features that this backend can
34      * provide. */
35     enum event_method_feature features;
36     /** Length of the extra information we should record for each fd that
37         has one or more active events.  This information is recorded
38         as part of the evmap entry for each fd, and passed as an argument
39         to the add and del functions above.
40      */
41     size_t fdinfo_len;
42 };

定义的成员包括:

1、name:后端的名字,例如:select、poll、epoll

2、init:用来初始化一个event_base来使用这个后端的函数。

3、add:激活一个给定文件描述符或者信号上的读或写。

4、del:和add的操作相反。

5、dispatch:完成核心事件循环的函数。

6、dealloc:清除和释放event_base数据。

7、need_reinit:标志在fork后是否需要重新初始化event_base。

原文地址:https://www.cnblogs.com/lit10050528/p/5872106.html