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