Mercurial > hg > nginx
annotate src/event/modules/ngx_eventport_module.c @ 6018:466bd63b63d1
Thread pools implementation.
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Sat, 14 Mar 2015 17:37:07 +0300 |
parents | d84f0abd4a53 |
children | 117c77b22db1 |
rev | line source |
---|---|
719 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
719 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_event.h> | |
11 | |
12 | |
13 #if (NGX_TEST_BUILD_EVENTPORT) | |
14 | |
15 #define ushort_t u_short | |
16 #define uint_t u_int | |
17 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
18 #ifndef CLOCK_REALTIME |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
19 #define CLOCK_REALTIME 0 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
20 typedef int clockid_t; |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
21 typedef void * timer_t; |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
22 #endif |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
23 |
719 | 24 /* Solaris declarations */ |
25 | |
26 #define PORT_SOURCE_AIO 1 | |
27 #define PORT_SOURCE_TIMER 2 | |
28 #define PORT_SOURCE_USER 3 | |
29 #define PORT_SOURCE_FD 4 | |
30 #define PORT_SOURCE_ALERT 5 | |
31 #define PORT_SOURCE_MQ 6 | |
32 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
33 #ifndef ETIME |
719 | 34 #define ETIME 64 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
35 #endif |
719 | 36 |
37 #define SIGEV_PORT 4 | |
38 | |
39 typedef struct { | |
40 int portev_events; /* event data is source specific */ | |
41 ushort_t portev_source; /* event source */ | |
42 ushort_t portev_pad; /* port internal use */ | |
43 uintptr_t portev_object; /* source specific object */ | |
44 void *portev_user; /* user cookie */ | |
45 } port_event_t; | |
46 | |
47 typedef struct port_notify { | |
48 int portnfy_port; /* bind request(s) to port */ | |
49 void *portnfy_user; /* user defined */ | |
50 } port_notify_t; | |
51 | |
1939
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
52 #if (__FreeBSD_version < 700005) |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
53 |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
54 typedef struct itimerspec { /* definition per POSIX.4 */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
55 struct timespec it_interval;/* timer period */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
56 struct timespec it_value; /* timer expiration */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
57 } itimerspec_t; |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
58 |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
59 #endif |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
60 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
61 int port_create(void); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
62 |
719 | 63 int port_create(void) |
64 { | |
65 return -1; | |
66 } | |
67 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
68 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
69 int port_associate(int port, int source, uintptr_t object, int events, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
70 void *user); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
71 |
719 | 72 int port_associate(int port, int source, uintptr_t object, int events, |
73 void *user) | |
74 { | |
75 return -1; | |
76 } | |
77 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
78 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
79 int port_dissociate(int port, int source, uintptr_t object); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
80 |
719 | 81 int port_dissociate(int port, int source, uintptr_t object) |
82 { | |
83 return -1; | |
84 } | |
85 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
86 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
87 int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
88 struct timespec *timeout); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
89 |
719 | 90 int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, |
91 struct timespec *timeout) | |
92 { | |
93 return -1; | |
94 } | |
95 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
96 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
97 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
98 |
719 | 99 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid) |
100 { | |
101 return -1; | |
102 } | |
103 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
104 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
105 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
106 struct itimerspec *ovalue); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
107 |
719 | 108 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
109 struct itimerspec *ovalue) | |
110 { | |
111 return -1; | |
112 } | |
113 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
114 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
115 int timer_delete(timer_t timerid); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
116 |
719 | 117 int timer_delete(timer_t timerid) |
118 { | |
119 return -1; | |
120 } | |
121 | |
122 #endif | |
123 | |
124 | |
125 typedef struct { | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
126 ngx_uint_t events; |
719 | 127 } ngx_eventport_conf_t; |
128 | |
129 | |
130 static ngx_int_t ngx_eventport_init(ngx_cycle_t *cycle, ngx_msec_t timer); | |
131 static void ngx_eventport_done(ngx_cycle_t *cycle); | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
132 static ngx_int_t ngx_eventport_add_event(ngx_event_t *ev, ngx_int_t event, |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
133 ngx_uint_t flags); |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
134 static ngx_int_t ngx_eventport_del_event(ngx_event_t *ev, ngx_int_t event, |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
135 ngx_uint_t flags); |
719 | 136 static ngx_int_t ngx_eventport_process_events(ngx_cycle_t *cycle, |
137 ngx_msec_t timer, ngx_uint_t flags); | |
138 | |
139 static void *ngx_eventport_create_conf(ngx_cycle_t *cycle); | |
140 static char *ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf); | |
141 | |
142 static int ep = -1; | |
143 static port_event_t *event_list; | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
144 static ngx_uint_t nevents; |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
145 static timer_t event_timer = (timer_t) -1; |
719 | 146 |
147 static ngx_str_t eventport_name = ngx_string("eventport"); | |
148 | |
149 | |
150 static ngx_command_t ngx_eventport_commands[] = { | |
151 | |
152 { ngx_string("eventport_events"), | |
153 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
154 ngx_conf_set_num_slot, | |
155 0, | |
156 offsetof(ngx_eventport_conf_t, events), | |
157 NULL }, | |
158 | |
159 ngx_null_command | |
160 }; | |
161 | |
162 | |
163 ngx_event_module_t ngx_eventport_module_ctx = { | |
164 &eventport_name, | |
165 ngx_eventport_create_conf, /* create configuration */ | |
166 ngx_eventport_init_conf, /* init configuration */ | |
167 | |
168 { | |
169 ngx_eventport_add_event, /* add an event */ | |
170 ngx_eventport_del_event, /* delete an event */ | |
171 ngx_eventport_add_event, /* enable an event */ | |
172 ngx_eventport_del_event, /* disable an event */ | |
173 NULL, /* add an connection */ | |
174 NULL, /* delete an connection */ | |
6018
466bd63b63d1
Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
6005
diff
changeset
|
175 NULL, /* trigger a notify */ |
719 | 176 NULL, /* process the changes */ |
177 ngx_eventport_process_events, /* process the events */ | |
178 ngx_eventport_init, /* init the events */ | |
179 ngx_eventport_done, /* done the events */ | |
180 } | |
181 | |
182 }; | |
183 | |
184 ngx_module_t ngx_eventport_module = { | |
185 NGX_MODULE_V1, | |
186 &ngx_eventport_module_ctx, /* module context */ | |
187 ngx_eventport_commands, /* module directives */ | |
188 NGX_EVENT_MODULE, /* module type */ | |
189 NULL, /* init master */ | |
190 NULL, /* init module */ | |
191 NULL, /* init process */ | |
192 NULL, /* init thread */ | |
193 NULL, /* exit thread */ | |
194 NULL, /* exit process */ | |
195 NULL, /* exit master */ | |
196 NGX_MODULE_V1_PADDING | |
197 }; | |
198 | |
199 | |
200 static ngx_int_t | |
201 ngx_eventport_init(ngx_cycle_t *cycle, ngx_msec_t timer) | |
202 { | |
203 port_notify_t pn; | |
204 struct itimerspec its; | |
205 struct sigevent sev; | |
206 ngx_eventport_conf_t *epcf; | |
207 | |
208 epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_eventport_module); | |
209 | |
210 if (ep == -1) { | |
211 ep = port_create(); | |
212 | |
213 if (ep == -1) { | |
214 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
215 "port_create() failed"); | |
216 return NGX_ERROR; | |
217 } | |
218 } | |
219 | |
220 if (nevents < epcf->events) { | |
221 if (event_list) { | |
222 ngx_free(event_list); | |
223 } | |
224 | |
225 event_list = ngx_alloc(sizeof(port_event_t) * epcf->events, | |
226 cycle->log); | |
227 if (event_list == NULL) { | |
228 return NGX_ERROR; | |
229 } | |
230 } | |
231 | |
232 ngx_event_flags = NGX_USE_EVENTPORT_EVENT; | |
233 | |
234 if (timer) { | |
235 ngx_memzero(&pn, sizeof(port_notify_t)); | |
236 pn.portnfy_port = ep; | |
237 | |
238 ngx_memzero(&sev, sizeof(struct sigevent)); | |
239 sev.sigev_notify = SIGEV_PORT; | |
240 #if !(NGX_TEST_BUILD_EVENTPORT) | |
241 sev.sigev_value.sival_ptr = &pn; | |
242 #endif | |
243 | |
244 if (timer_create(CLOCK_REALTIME, &sev, &event_timer) == -1) { | |
245 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
246 "timer_create() failed"); | |
247 return NGX_ERROR; | |
248 } | |
249 | |
250 its.it_interval.tv_sec = timer / 1000; | |
251 its.it_interval.tv_nsec = (timer % 1000) * 1000000; | |
252 its.it_value.tv_sec = timer / 1000; | |
253 its.it_value.tv_nsec = (timer % 1000) * 1000000; | |
254 | |
255 if (timer_settime(event_timer, 0, &its, NULL) == -1) { | |
256 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
257 "timer_settime() failed"); | |
258 return NGX_ERROR; | |
259 } | |
260 | |
261 ngx_event_flags |= NGX_USE_TIMER_EVENT; | |
262 } | |
263 | |
264 nevents = epcf->events; | |
265 | |
266 ngx_io = ngx_os_io; | |
267 | |
268 ngx_event_actions = ngx_eventport_module_ctx.actions; | |
269 | |
270 return NGX_OK; | |
271 } | |
272 | |
273 | |
274 static void | |
275 ngx_eventport_done(ngx_cycle_t *cycle) | |
276 { | |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
277 if (event_timer != (timer_t) -1) { |
719 | 278 if (timer_delete(event_timer) == -1) { |
279 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
280 "timer_delete() failed"); | |
281 } | |
282 | |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
283 event_timer = (timer_t) -1; |
719 | 284 } |
285 | |
286 if (close(ep) == -1) { | |
287 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
288 "close() event port failed"); | |
289 } | |
290 | |
291 ep = -1; | |
292 | |
293 ngx_free(event_list); | |
294 | |
295 event_list = NULL; | |
296 nevents = 0; | |
297 } | |
298 | |
299 | |
300 static ngx_int_t | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
301 ngx_eventport_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
719 | 302 { |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
303 ngx_int_t events, prev; |
719 | 304 ngx_event_t *e; |
305 ngx_connection_t *c; | |
306 | |
307 c = ev->data; | |
308 | |
309 events = event; | |
310 | |
311 if (event == NGX_READ_EVENT) { | |
312 e = c->write; | |
313 prev = POLLOUT; | |
314 #if (NGX_READ_EVENT != POLLIN) | |
315 events = POLLIN; | |
316 #endif | |
317 | |
318 } else { | |
319 e = c->read; | |
320 prev = POLLIN; | |
321 #if (NGX_WRITE_EVENT != POLLOUT) | |
322 events = POLLOUT; | |
323 #endif | |
324 } | |
325 | |
326 if (e->oneshot) { | |
327 events |= prev; | |
328 } | |
329 | |
330 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
331 "eventport add event: fd:%d ev:%04Xi", c->fd, events); |
719 | 332 |
333 if (port_associate(ep, PORT_SOURCE_FD, c->fd, events, | |
334 (void *) ((uintptr_t) ev | ev->instance)) | |
335 == -1) | |
336 { | |
337 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
338 "port_associate() failed"); | |
339 return NGX_ERROR; | |
340 } | |
341 | |
342 ev->active = 1; | |
343 ev->oneshot = 1; | |
344 | |
345 return NGX_OK; | |
346 } | |
347 | |
348 | |
349 static ngx_int_t | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
350 ngx_eventport_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
719 | 351 { |
352 ngx_event_t *e; | |
353 ngx_connection_t *c; | |
354 | |
355 /* | |
356 * when the file descriptor is closed, the event port automatically | |
4572
67653855682e
Fixed spelling in multiline C comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
357 * dissociates it from the port, so we do not need to dissociate explicitly |
719 | 358 * the event before the closing the file descriptor |
359 */ | |
360 | |
361 if (flags & NGX_CLOSE_EVENT) { | |
362 ev->active = 0; | |
363 ev->oneshot = 0; | |
364 return NGX_OK; | |
365 } | |
366 | |
367 c = ev->data; | |
368 | |
369 if (event == NGX_READ_EVENT) { | |
370 e = c->write; | |
371 event = POLLOUT; | |
372 | |
373 } else { | |
374 e = c->read; | |
375 event = POLLIN; | |
376 } | |
377 | |
378 if (e->oneshot) { | |
379 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
380 "eventport change event: fd:%d ev:%04Xi", c->fd, event); |
719 | 381 |
382 if (port_associate(ep, PORT_SOURCE_FD, c->fd, event, | |
383 (void *) ((uintptr_t) ev | ev->instance)) | |
384 == -1) | |
385 { | |
386 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
387 "port_associate() failed"); | |
388 return NGX_ERROR; | |
389 } | |
390 | |
391 } else { | |
392 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
393 "eventport del event: fd:%d", c->fd); | |
394 | |
395 if (port_dissociate(ep, PORT_SOURCE_FD, c->fd) == -1) { | |
396 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
397 "port_dissociate() failed"); | |
398 return NGX_ERROR; | |
399 } | |
400 } | |
401 | |
402 ev->active = 0; | |
403 ev->oneshot = 0; | |
404 | |
405 return NGX_OK; | |
406 } | |
407 | |
408 | |
409 ngx_int_t | |
410 ngx_eventport_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, | |
411 ngx_uint_t flags) | |
412 { | |
413 int n, revents; | |
414 u_int events; | |
415 ngx_err_t err; | |
416 ngx_int_t instance; | |
417 ngx_uint_t i, level; | |
5821
3f5f0ab59b35
Events: processing of posted events changed from LIFO to FIFO.
Valentin Bartenev <vbart@nginx.com>
parents:
5820
diff
changeset
|
418 ngx_event_t *ev, *rev, *wev; |
3f5f0ab59b35
Events: processing of posted events changed from LIFO to FIFO.
Valentin Bartenev <vbart@nginx.com>
parents:
5820
diff
changeset
|
419 ngx_queue_t *queue; |
719 | 420 ngx_connection_t *c; |
421 struct timespec ts, *tp; | |
422 | |
423 if (timer == NGX_TIMER_INFINITE) { | |
424 tp = NULL; | |
425 | |
426 } else { | |
427 ts.tv_sec = timer / 1000; | |
428 ts.tv_nsec = (timer % 1000) * 1000000; | |
429 tp = &ts; | |
430 } | |
431 | |
432 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
433 "eventport timer: %M", timer); | |
434 | |
435 events = 1; | |
436 | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
437 n = port_getn(ep, event_list, (u_int) nevents, &events, tp); |
719 | 438 |
439 err = ngx_errno; | |
440 | |
441 if (flags & NGX_UPDATE_TIME) { | |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
442 ngx_time_update(); |
719 | 443 } |
444 | |
445 if (n == -1) { | |
446 if (err == ETIME) { | |
447 if (timer != NGX_TIMER_INFINITE) { | |
448 return NGX_OK; | |
449 } | |
450 | |
451 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
452 "port_getn() returned no events without timeout"); | |
453 return NGX_ERROR; | |
454 } | |
455 | |
456 level = (err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT; | |
457 ngx_log_error(level, cycle->log, err, "port_getn() failed"); | |
458 return NGX_ERROR; | |
459 } | |
460 | |
461 if (events == 0) { | |
462 if (timer != NGX_TIMER_INFINITE) { | |
463 return NGX_OK; | |
464 } | |
465 | |
466 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
467 "port_getn() returned no events without timeout"); | |
468 return NGX_ERROR; | |
469 } | |
470 | |
471 for (i = 0; i < events; i++) { | |
472 | |
473 if (event_list[i].portev_source == PORT_SOURCE_TIMER) { | |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
474 ngx_time_update(); |
719 | 475 continue; |
476 } | |
477 | |
478 ev = event_list[i].portev_user; | |
479 | |
480 switch (event_list[i].portev_source) { | |
481 | |
482 case PORT_SOURCE_FD: | |
483 | |
484 instance = (uintptr_t) ev & 1; | |
485 ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1); | |
486 | |
487 if (ev->closed || ev->instance != instance) { | |
488 | |
489 /* | |
490 * the stale event from a file descriptor | |
491 * that was just closed in this iteration | |
492 */ | |
493 | |
494 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
495 "eventport: stale event %p", ev); | |
496 continue; | |
497 } | |
498 | |
499 revents = event_list[i].portev_events; | |
500 | |
501 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
502 "eventport: fd:%d, ev:%04Xd", | |
503 event_list[i].portev_object, revents); | |
504 | |
505 if (revents & (POLLERR|POLLHUP|POLLNVAL)) { | |
1128
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
506 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
507 "port_getn() error fd:%d ev:%04Xd", |
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
508 event_list[i].portev_object, revents); |
719 | 509 } |
510 | |
511 if (revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL)) { | |
512 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
513 "strange port_getn() events fd:%d ev:%04Xd", | |
514 event_list[i].portev_object, revents); | |
515 } | |
516 | |
517 if ((revents & (POLLERR|POLLHUP|POLLNVAL)) | |
518 && (revents & (POLLIN|POLLOUT)) == 0) | |
519 { | |
520 /* | |
521 * if the error events were returned without POLLIN or POLLOUT, | |
522 * then add these flags to handle the events at least in one | |
523 * active handler | |
524 */ | |
525 | |
526 revents |= POLLIN|POLLOUT; | |
527 } | |
528 | |
529 c = ev->data; | |
530 rev = c->read; | |
531 wev = c->write; | |
532 | |
533 rev->active = 0; | |
534 wev->active = 0; | |
535 | |
536 if (revents & POLLIN) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
537 rev->ready = 1; |
719 | 538 |
539 if (flags & NGX_POST_EVENTS) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
540 queue = rev->accept ? &ngx_posted_accept_events |
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
541 : &ngx_posted_events; |
719 | 542 |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
543 ngx_post_event(rev, queue); |
719 | 544 |
545 } else { | |
546 rev->handler(rev); | |
1287
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
547 |
5170
6362bd26e4b0
Events: protection from stale events in eventport and devpoll.
Valentin Bartenev <vbart@nginx.com>
parents:
4759
diff
changeset
|
548 if (ev->closed || ev->instance != instance) { |
1287
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
549 continue; |
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
550 } |
719 | 551 } |
552 | |
553 if (rev->accept) { | |
554 if (ngx_use_accept_mutex) { | |
555 ngx_accept_events = 1; | |
556 continue; | |
557 } | |
558 | |
559 if (port_associate(ep, PORT_SOURCE_FD, c->fd, POLLIN, | |
560 (void *) ((uintptr_t) ev | ev->instance)) | |
561 == -1) | |
562 { | |
563 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
564 "port_associate() failed"); | |
565 return NGX_ERROR; | |
566 } | |
567 } | |
568 } | |
569 | |
570 if (revents & POLLOUT) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
571 wev->ready = 1; |
719 | 572 |
573 if (flags & NGX_POST_EVENTS) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
574 ngx_post_event(wev, &ngx_posted_events); |
719 | 575 |
576 } else { | |
577 wev->handler(wev); | |
578 } | |
579 } | |
580 | |
581 continue; | |
582 | |
583 default: | |
584 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
6005
d84f0abd4a53
Events: fixed typo in the error message.
Ruslan Ermilov <ru@nginx.com>
parents:
5821
diff
changeset
|
585 "unexpected eventport object %d", |
719 | 586 event_list[i].portev_object); |
587 continue; | |
588 } | |
589 } | |
590 | |
591 return NGX_OK; | |
592 } | |
593 | |
594 | |
595 static void * | |
596 ngx_eventport_create_conf(ngx_cycle_t *cycle) | |
597 { | |
598 ngx_eventport_conf_t *epcf; | |
599 | |
600 epcf = ngx_palloc(cycle->pool, sizeof(ngx_eventport_conf_t)); | |
601 if (epcf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
1939
diff
changeset
|
602 return NULL; |
719 | 603 } |
604 | |
605 epcf->events = NGX_CONF_UNSET; | |
606 | |
607 return epcf; | |
608 } | |
609 | |
610 | |
611 static char * | |
612 ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf) | |
613 { | |
614 ngx_eventport_conf_t *epcf = conf; | |
615 | |
616 ngx_conf_init_uint_value(epcf->events, 32); | |
617 | |
618 return NGX_CONF_OK; | |
619 } |