文章目录
  1. event_base结构体
  2. 事件循环

event_base结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
引用方式: #include <event.h>
evsel: event_base对象选择的I/O复用机制
evbase: event_base对象的存储数据地址(通过evsel成员的init函数初始化)
changelist: 事件变化队列(考虑一个文件描述符上注册的事件被多次修改, 那么可以使用缓存来避免重复的系统调用, 仅用于事件检测算法的复杂度为O(1)的I/O复用技术)
evsigsel: 信号的后端处理机制(目前在signal.c中定义了一种处理机制)
sig: 被信号事件处理器使用,其中封装了一个由socketpair创建的管道; 管道用于信号处理函数与事件多路分发器之间的通信, 即统一事件源
virtual_event_count: 添加到event_base对象中的虚拟事件数
event_count: 添加到event_base对象上所有事件的数量
event_count_active: event_base对象上的激活事件数
event_gotterm: 是否执行完活动队列上的剩余任务后就退出事件循环
event_break: 是否立即退出事件循环,而不论是否还要任务需要处理
event_continue: 是否应该启动一个新的事件循环
event_running_priority: 目前正在处理的活动事件队列的优先级
running_loop: 事件循环是否已经启动
activequeues: 活动事件队列数组(索引值越小的队列, 优先级越高, 高优先级中的)
common_timeout_queues: 通用定时器队列
io: 文件描述符与I/O事件之间的映射关系表
sigmap: 信号值与信号事件的映射关系表
th_owner_id: 当前运行该event_base的事件循环的线程
th_base_lock: 对该event_base的独占锁
current_event_cond: 条件变量(用于唤醒正在等待某个事件处理完毕的线程)
current_event_waiters: 等待条件变量current_event_cond的线程数
flags: event_base对象的配置参数
is_notify_pending: 工作线程是否要唤醒管道
th_notify_fd: 工作线程与主线程建立的管道
*/
struct event_base {
const struct eventop *evsel;
void *evbase;
struct event_changelist changelist;
const struct eventop *evsigsel;
struct evsig_info sig;
int virtual_event_count;
int virtual_event_count_max;
int event_count;
int event_count_max;
int event_count_active;
int event_count_active_max;
int event_gotterm;
int event_break;
int event_continue;
int event_running_priority;
int running_loop;
int n_deferreds_queued;
struct evcallback_list *activequeues;
int nactivequeues;
struct evcallback_list active_later_queue;
struct common_timeout_list **common_timeout_queues;
int n_common_timeouts;
int n_common_timeouts_allocated;
struct event_io_map io;
struct event_signal_map sigmap;
struct min_heap timeheap;
struct timeval tv_cache;
struct evutil_monotonic_timer monotonic_timer;
struct timeval tv_clock_diff;
time_t last_updated_clock_diff;
#ifndef EVENT__DISABLE_THREAD_SUPPORT
unsigned long th_owner_id;
void *th_base_lock;
void *current_event_cond;
int current_event_waiters;
#endif
struct event_callback *current_event;

#ifdef _WIN32
struct event_iocp_port *iocp;
#endif
enum event_base_config_flag flags;
struct timeval max_dispatch_time;
int max_dispatch_callbacks;
int limit_callbacks_after_prio;
int is_notify_pending;
evutil_socket_t th_notify_fd[2];
struct event th_notify;
int (*th_notify_fn)(struct event_base *base);
struct evutil_weakrand_state weakrand_seed;
LIST_HEAD(once_event_list, event_once) once_events;
};

事件循环

事件循环由event_base_loop函数实现。关于event_base_loop函数的分析详见代码清单12-10。