forked from chronolaw/annotated_nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_event_posted.c
80 lines (57 loc) · 1.84 KB
/
ngx_event_posted.c
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
// annotated by chrono since 2016
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
// 两个双端队列,保存epoll获取的事件
//
// 如果使用了reuseport,或者不使用负载均衡
// 那么这两个队列就完全不会用到
// 保存accept事件,即客户端发起的连接请求
ngx_queue_t ngx_posted_accept_events;
// 读写事件和通知事件
// 1.17.5新增,处理ngx_posted_next_events
ngx_queue_t ngx_posted_next_events;
ngx_queue_t ngx_posted_events;
// 遍历队列,取出队列里的事件,调用对应的handler
// cycle参数只用于记录日志
void
ngx_event_process_posted(ngx_cycle_t *cycle, ngx_queue_t *posted)
{
ngx_queue_t *q;
ngx_event_t *ev;
while (!ngx_queue_empty(posted)) {
// 取队列头节点
q = ngx_queue_head(posted);
// 获取头节点元素,即事件
ev = ngx_queue_data(q, ngx_event_t, queue);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"posted event %p", ev);
// in ngx_event_posted.h
// 函数宏,从队列里移除
ngx_delete_posted_event(ev);
// 执行事件的回调函数
ev->handler(ev);
}
}
void
ngx_event_move_posted_next(ngx_cycle_t *cycle)
{
ngx_queue_t *q;
ngx_event_t *ev;
for (q = ngx_queue_head(&ngx_posted_next_events);
q != ngx_queue_sentinel(&ngx_posted_next_events);
q = ngx_queue_next(q))
{
ev = ngx_queue_data(q, ngx_event_t, queue);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"posted next event %p", ev);
ev->ready = 1;
ev->available = -1;
}
ngx_queue_add(&ngx_posted_events, &ngx_posted_next_events);
ngx_queue_init(&ngx_posted_next_events);
}