blob: e20839d9edee6dc7e37f4d9129044c1e50c70185 (
plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_log.h>
#include <ngx_alloc.h>
#include <ngx_connection.h>
#include <ngx_event.h>
#include <ngx_event_timer.h>
/* STUB */
#define NGX_TIMER_QUEUE_NUM 5
/* should be per-thread */
static ngx_event_t *ngx_timer_queue;
static int ngx_timer_cur_queue;
/* */
static int ngx_timer_queue_num;
ngx_event_t *ngx_event_init_timer(ngx_log_t *log)
{
int i;
ngx_timer_queue_num = NGX_TIMER_QUEUE_NUM;
ngx_timer_cur_queue = 0;
ngx_test_null(ngx_timer_queue,
ngx_alloc(ngx_timer_queue_num * sizeof(ngx_event_t), log),
NULL);
for (i = 0; i < ngx_timer_queue_num; i++) {
ngx_timer_queue[i].timer_prev = &ngx_timer_queue[i];
ngx_timer_queue[i].timer_next = &ngx_timer_queue[i];
}
return ngx_timer_queue;
}
void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
{
ngx_event_t *e;
#if (NGX_DEBUG_EVENT)
ngx_connection_t *c = (ngx_connection_t *) ev->data;
ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer);
#endif
if (ev->timer_next || ev->timer_prev) {
ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set");
return;
}
#if (NGX_DEBUG_EVENT)
ngx_log_debug(ev->log, "timer slot: %d" _ ngx_timer_cur_queue);
#endif
for (e = ngx_timer_queue[ngx_timer_cur_queue].timer_next;
e != &ngx_timer_queue[ngx_timer_cur_queue] && timer > e->timer_delta;
e = e->timer_next)
{
timer -= e->timer_delta;
}
ngx_timer_cur_queue++;
if (ngx_timer_cur_queue >= ngx_timer_queue_num) {
ngx_timer_cur_queue = 0;
}
ev->timer_delta = timer;
ev->timer_next = e;
ev->timer_prev = e->timer_prev;
e->timer_prev->timer_next = ev;
e->timer_prev = ev;
}
int ngx_event_find_timer(void)
{
int i;
ngx_msec_t timer;
timer = NGX_MAX_MSEC;
for (i = 0; i < ngx_timer_queue_num; i++) {
if (ngx_timer_queue[i].timer_next == &ngx_timer_queue[i]) {
continue;
}
if (timer > ngx_timer_queue[i].timer_next->timer_delta) {
timer = ngx_timer_queue[i].timer_next->timer_delta;
}
}
if (timer == NGX_MAX_MSEC) {
return 0;
} else {
return timer;
}
}
void ngx_event_expire_timers(ngx_msec_t timer)
{
int i;
ngx_msec_t delta;
ngx_event_t *ev;
for (i = 0; i < ngx_timer_queue_num; i++) {
delta = timer;
for ( ;; ) {
ev = ngx_timer_queue[i].timer_next;
if (ev == &ngx_timer_queue[i]) {
break;
}
if (ev->timer_delta > delta) {
ev->timer_delta -= delta;
break;
}
delta -= ev->timer_delta;
ngx_del_timer(ev);
ev->timedout = 1;
if (ev->event_handler(ev) == NGX_ERROR) {
ev->close_handler(ev);
}
}
}
}
|