Mercurial > hg > nginx
annotate src/event/ngx_event_timer.c @ 4522:14411ee4d89f
Whitespace fixes.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 05 Mar 2012 18:09:06 +0000 |
parents | d620f497c50f |
children | 876e6b0814a5 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
4412 | 4 * Copyright (C) Nginx, Inc. |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
5 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
6 |
50
b288069a8696
nginx-0.0.1-2003-01-23-21:47:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
49
diff
changeset
|
7 |
b288069a8696
nginx-0.0.1-2003-01-23-21:47:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
49
diff
changeset
|
8 #include <ngx_config.h> |
b288069a8696
nginx-0.0.1-2003-01-23-21:47:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
49
diff
changeset
|
9 #include <ngx_core.h> |
b288069a8696
nginx-0.0.1-2003-01-23-21:47:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
49
diff
changeset
|
10 #include <ngx_event.h> |
b288069a8696
nginx-0.0.1-2003-01-23-21:47:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
49
diff
changeset
|
11 |
b288069a8696
nginx-0.0.1-2003-01-23-21:47:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
49
diff
changeset
|
12 |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
230
diff
changeset
|
13 #if (NGX_THREADS) |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
14 ngx_mutex_t *ngx_event_timer_mutex; |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
230
diff
changeset
|
15 #endif |
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
230
diff
changeset
|
16 |
210
00cafae0bdf1
nginx-0.0.1-2003-12-14-23:10:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
209
diff
changeset
|
17 |
559 | 18 ngx_thread_volatile ngx_rbtree_t ngx_event_timer_rbtree; |
19 static ngx_rbtree_node_t ngx_event_timer_sentinel; | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
20 |
1022 | 21 /* |
22 * the event timer rbtree may contain the duplicate keys, however, | |
23 * it should not be a problem, because we use the rbtree to find | |
24 * a minimum timer value only | |
25 */ | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
26 |
493 | 27 ngx_int_t |
28 ngx_event_timer_init(ngx_log_t *log) | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
29 { |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1022
diff
changeset
|
30 ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel, |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1022
diff
changeset
|
31 ngx_rbtree_insert_timer_value); |
559 | 32 |
272
d4e65d74db9f
nginx-0.0.2-2004-03-01-00:03:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
270
diff
changeset
|
33 #if (NGX_THREADS) |
559 | 34 |
35 if (ngx_event_timer_mutex) { | |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
36 ngx_event_timer_mutex->log = log; |
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
37 return NGX_OK; |
209
e1c815be05ae
nginx-0.0.1-2003-12-09-18:08:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
207
diff
changeset
|
38 } |
e1c815be05ae
nginx-0.0.1-2003-12-09-18:08:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
207
diff
changeset
|
39 |
501 | 40 ngx_event_timer_mutex = ngx_mutex_init(log, 0); |
41 if (ngx_event_timer_mutex == NULL) { | |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
42 return NGX_ERROR; |
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
43 } |
559 | 44 |
272
d4e65d74db9f
nginx-0.0.2-2004-03-01-00:03:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
270
diff
changeset
|
45 #endif |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
46 |
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
47 return NGX_OK; |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
48 } |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
49 |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
50 |
493 | 51 ngx_msec_t |
52 ngx_event_find_timer(void) | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
53 { |
561 | 54 ngx_msec_int_t timer; |
55 ngx_rbtree_node_t *node, *root, *sentinel; | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
56 |
559 | 57 if (ngx_event_timer_rbtree.root == &ngx_event_timer_sentinel) { |
316
a0beefedaf94
nginx-0.0.3-2004-04-15-00:34:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
315
diff
changeset
|
58 return NGX_TIMER_INFINITE; |
213
f536f91e8e99
nginx-0.0.1-2003-12-19-15:45:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
212
diff
changeset
|
59 } |
f536f91e8e99
nginx-0.0.1-2003-12-19-15:45:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
212
diff
changeset
|
60 |
563 | 61 ngx_mutex_lock(ngx_event_timer_mutex); |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
62 |
559 | 63 root = ngx_event_timer_rbtree.root; |
64 sentinel = ngx_event_timer_rbtree.sentinel; | |
65 | |
66 node = ngx_rbtree_min(root, sentinel); | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
67 |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
68 ngx_mutex_unlock(ngx_event_timer_mutex); |
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
69 |
563 | 70 timer = (ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_msec; |
315
39b6f2df45c0
nginx-0.0.3-2004-04-14-21:44:28 import
Igor Sysoev <igor@sysoev.ru>
parents:
306
diff
changeset
|
71 |
559 | 72 return (ngx_msec_t) (timer > 0 ? timer : 0); |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
73 } |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
74 |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
75 |
493 | 76 void |
557 | 77 ngx_event_expire_timers(void) |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
78 { |
559 | 79 ngx_event_t *ev; |
80 ngx_rbtree_node_t *node, *root, *sentinel; | |
81 | |
82 sentinel = ngx_event_timer_rbtree.sentinel; | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
83 |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
84 for ( ;; ) { |
213
f536f91e8e99
nginx-0.0.1-2003-12-19-15:45:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
212
diff
changeset
|
85 |
563 | 86 ngx_mutex_lock(ngx_event_timer_mutex); |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
87 |
559 | 88 root = ngx_event_timer_rbtree.root; |
89 | |
90 if (root == sentinel) { | |
91 return; | |
92 } | |
93 | |
94 node = ngx_rbtree_min(root, sentinel); | |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
95 |
557 | 96 /* node->key <= ngx_current_time */ |
97 | |
563 | 98 if ((ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_msec <= 0) |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
99 { |
559 | 100 ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer)); |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
101 |
372
c9fdfccebc49
nginx-0.0.7-2004-06-29-01:03:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
371
diff
changeset
|
102 #if (NGX_THREADS) |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
103 |
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
104 if (ngx_threaded && ngx_trylock(ev->lock) == 0) { |
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
105 |
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
106 /* |
4133
59b99f217c6d
Replaced "can not" with "cannot" and "could not" in a bunch of places.
Ruslan Ermilov <ru@nginx.com>
parents:
1743
diff
changeset
|
107 * We cannot change the timer of the event that is being |
59b99f217c6d
Replaced "can not" with "cannot" and "could not" in a bunch of places.
Ruslan Ermilov <ru@nginx.com>
parents:
1743
diff
changeset
|
108 * handled by another thread. And we cannot easy walk |
59b99f217c6d
Replaced "can not" with "cannot" and "could not" in a bunch of places.
Ruslan Ermilov <ru@nginx.com>
parents:
1743
diff
changeset
|
109 * the rbtree to find next expired timer so we exit the loop. |
59b99f217c6d
Replaced "can not" with "cannot" and "could not" in a bunch of places.
Ruslan Ermilov <ru@nginx.com>
parents:
1743
diff
changeset
|
110 * However, it should be a rare case when the event that is |
59b99f217c6d
Replaced "can not" with "cannot" and "could not" in a bunch of places.
Ruslan Ermilov <ru@nginx.com>
parents:
1743
diff
changeset
|
111 * being handled has an expired timer. |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
112 */ |
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
113 |
379
73688d5d7fc3
nginx-0.0.7-2004-07-06-20:12:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
375
diff
changeset
|
114 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
461 | 115 "event %p is busy in expire timers", ev); |
371
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
116 break; |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
117 } |
372
c9fdfccebc49
nginx-0.0.7-2004-06-29-01:03:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
371
diff
changeset
|
118 #endif |
371
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
119 |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
120 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
557 | 121 "event timer del: %d: %M", |
559 | 122 ngx_event_ident(ev->data), ev->timer.key); |
371
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
123 |
559 | 124 ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer); |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
125 |
371
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
126 ngx_mutex_unlock(ngx_event_timer_mutex); |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
127 |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
128 #if (NGX_DEBUG) |
559 | 129 ev->timer.left = NULL; |
130 ev->timer.right = NULL; | |
131 ev->timer.parent = NULL; | |
371
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
132 #endif |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
133 |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
134 ev->timer_set = 0; |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
135 |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
136 #if (NGX_THREADS) |
306
6b91bfbc4123
nginx-0.0.3-2004-04-05-00:32:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
275
diff
changeset
|
137 if (ngx_threaded) { |
563 | 138 ev->posted_timedout = 1; |
306
6b91bfbc4123
nginx-0.0.3-2004-04-05-00:32:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
275
diff
changeset
|
139 |
563 | 140 ngx_post_event(ev, &ngx_posted_events); |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
141 |
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
142 ngx_unlock(ev->lock); |
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
143 |
306
6b91bfbc4123
nginx-0.0.3-2004-04-05-00:32:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
275
diff
changeset
|
144 continue; |
6b91bfbc4123
nginx-0.0.3-2004-04-05-00:32:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
275
diff
changeset
|
145 } |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
374
diff
changeset
|
146 #endif |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
147 |
374
213f17e9f776
nginx-0.0.7-2004-07-02-09:47:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
372
diff
changeset
|
148 ev->timedout = 1; |
213f17e9f776
nginx-0.0.7-2004-07-02-09:47:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
372
diff
changeset
|
149 |
509 | 150 ev->handler(ev); |
374
213f17e9f776
nginx-0.0.7-2004-07-02-09:47:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
372
diff
changeset
|
151 |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
152 continue; |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
153 } |
374
213f17e9f776
nginx-0.0.7-2004-07-02-09:47:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
372
diff
changeset
|
154 |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
155 break; |
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
156 } |
371
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
157 |
780e93985b93
nginx-0.0.7-2004-06-28-20:05:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
361
diff
changeset
|
158 ngx_mutex_unlock(ngx_event_timer_mutex); |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
159 } |