nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev
18 years ago
13 | 13 | static ngx_event_t ngx_cleaner_event; |
14 | 14 | |
15 | 15 | ngx_uint_t ngx_test_config; |
16 | ||
17 | #if (NGX_THREADS) | |
18 | ngx_tls_key_t ngx_core_tls_key; | |
19 | #endif | |
16 | 20 | |
17 | 21 | |
18 | 22 | /* STUB NAME */ |
48 | 48 | } ngx_core_conf_t; |
49 | 49 | |
50 | 50 | |
51 | typedef struct { | |
52 | ngx_pool_t *pool; /* pcre's malloc() pool */ | |
53 | } ngx_core_tls_t; | |
54 | ||
55 | ||
51 | 56 | ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle); |
52 | 57 | ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle); |
53 | 58 | void ngx_delete_pidfile(ngx_cycle_t *cycle); |
59 | 64 | extern ngx_array_t ngx_old_cycles; |
60 | 65 | extern ngx_module_t ngx_core_module; |
61 | 66 | extern ngx_uint_t ngx_test_config; |
67 | #if (NGX_THREADS) | |
68 | extern ngx_tls_key_t ngx_core_tls_key; | |
69 | #endif | |
62 | 70 | |
63 | 71 | |
64 | 72 | #endif /* _NGX_CYCLE_H_INCLUDED_ */ |
6 | 6 | static void ngx_regex_free(void *p); |
7 | 7 | |
8 | 8 | |
9 | /* THREADS: this pool should be private for each thread */ | |
10 | 9 | static ngx_pool_t *ngx_pcre_pool; |
11 | 10 | |
12 | 11 | |
20 | 19 | ngx_regex_t *ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options, |
21 | 20 | ngx_pool_t *pool, ngx_str_t *err) |
22 | 21 | { |
23 | int erroff; | |
24 | const char *errstr; | |
25 | ngx_regex_t *re; | |
22 | int erroff; | |
23 | const char *errstr; | |
24 | ngx_regex_t *re; | |
25 | #if (NGX_THREADS) | |
26 | ngx_core_tls_t *tls; | |
27 | ||
28 | #if (NGX_SUPPRESS_WARN) | |
29 | tls = NULL; | |
30 | #endif | |
31 | ||
32 | if (ngx_threaded) { | |
33 | tls = ngx_thread_get_tls(ngx_core_tls_key); | |
34 | tls->pool = pool; | |
35 | } else { | |
36 | ngx_pcre_pool = pool; | |
37 | } | |
38 | ||
39 | #else | |
26 | 40 | |
27 | 41 | ngx_pcre_pool = pool; |
42 | ||
43 | #endif | |
28 | 44 | |
29 | 45 | re = pcre_compile((const char *) pattern->data, (int) options, |
30 | 46 | &errstr, &erroff, NULL); |
43 | 59 | |
44 | 60 | /* ensure that there is no current pool */ |
45 | 61 | |
62 | #if (NGX_THREADS) | |
63 | if (ngx_threaded) { | |
64 | tls->pool = NULL; | |
65 | } else { | |
66 | ngx_pcre_pool = NULL; | |
67 | } | |
68 | #else | |
46 | 69 | ngx_pcre_pool = NULL; |
70 | #endif | |
47 | 71 | |
48 | 72 | return re; |
49 | 73 | } |
67 | 91 | |
68 | 92 | static void *ngx_regex_malloc(size_t size) |
69 | 93 | { |
70 | if (ngx_pcre_pool) { | |
71 | return ngx_palloc(ngx_pcre_pool, size); | |
94 | ngx_pool_t *pool; | |
95 | #if (NGX_THREADS) | |
96 | ngx_core_tls_t *tls; | |
97 | ||
98 | if (ngx_threaded) { | |
99 | tls = ngx_thread_get_tls(ngx_core_tls_key); | |
100 | pool = tls->pool; | |
101 | } else { | |
102 | pool = ngx_pcre_pool; | |
103 | } | |
104 | #else | |
105 | pool = ngx_pcre_pool; | |
106 | #endif | |
107 | ||
108 | if (pool) { | |
109 | return ngx_palloc(pool, size); | |
72 | 110 | } |
73 | 111 | |
74 | 112 | return NULL; |
321 | 321 | ngx_msec_t timer; |
322 | 322 | ngx_err_t err; |
323 | 323 | ngx_cycle_t **old_cycle; |
324 | ngx_event_t *rev, *wev; | |
324 | 325 | ngx_connection_t *c; |
325 | 326 | ngx_epoch_msec_t delta; |
326 | 327 | struct dvpoll dvp; |
475 | 476 | event_list[i].events, event_list[i].revents); |
476 | 477 | } |
477 | 478 | |
478 | if ((event_list[i].events & (POLLOUT|POLLERR|POLLHUP)) | |
479 | && c->write->active) | |
480 | { | |
481 | c->write->ready = 1; | |
479 | wev = c->write; | |
480 | ||
481 | if ((event_list[i].events & (POLLOUT|POLLERR|POLLHUP)) && wev->active) { | |
482 | wev->ready = 1; | |
482 | 483 | |
483 | 484 | if (!ngx_threaded && !ngx_accept_mutex_held) { |
484 | c->write->event_handler(c->write); | |
485 | wev->event_handler(wev); | |
485 | 486 | |
486 | 487 | } else { |
487 | ngx_post_event(c->write); | |
488 | ngx_post_event(wev); | |
488 | 489 | } |
489 | 490 | } |
490 | 491 | |
494 | 495 | * if the accept event is the last one. |
495 | 496 | */ |
496 | 497 | |
497 | if ((event_list[i].events & (POLLIN|POLLERR|POLLHUP)) | |
498 | && c->read->active) | |
499 | { | |
500 | c->read->ready = 1; | |
498 | rev = c->read; | |
499 | ||
500 | if ((event_list[i].events & (POLLIN|POLLERR|POLLHUP)) && rev->active) { | |
501 | rev->ready = 1; | |
501 | 502 | |
502 | 503 | if (!ngx_threaded && !ngx_accept_mutex_held) { |
503 | c->read->event_handler(c->read); | |
504 | ||
505 | } else if (!c->read->accept) { | |
506 | ngx_post_event(c->read); | |
504 | rev->event_handler(rev); | |
505 | ||
506 | } else if (!rev->accept) { | |
507 | ngx_post_event(rev); | |
507 | 508 | |
508 | 509 | } else if (ngx_accept_disabled <= 0) { |
509 | 510 | ngx_mutex_unlock(ngx_posted_events_mutex); |
510 | 511 | |
511 | c->read->event_handler(c->read); | |
512 | c->read->event_handler(rev); | |
512 | 513 | |
513 | 514 | if (ngx_accept_disabled > 0) { |
514 | 515 | ngx_accept_mutex_unlock(); |
74 | 74 | static int ngx_epoll_add_event(ngx_event_t *ev, int event, u_int flags); |
75 | 75 | static int ngx_epoll_del_event(ngx_event_t *ev, int event, u_int flags); |
76 | 76 | static int ngx_epoll_add_connection(ngx_connection_t *c); |
77 | static int ngx_epoll_del_connection(ngx_connection_t *c); | |
77 | static int ngx_epoll_del_connection(ngx_connection_t *c, u_int flags); | |
78 | 78 | static int ngx_epoll_process_events(ngx_cycle_t *cycle); |
79 | 79 | |
80 | 80 | static void *ngx_epoll_create_conf(ngx_cycle_t *cycle); |
110 | 110 | ngx_epoll_del_event, /* delete an event */ |
111 | 111 | ngx_epoll_add_event, /* enable an event */ |
112 | 112 | ngx_epoll_del_event, /* disable an event */ |
113 | NULL, /* add an connection */ | |
114 | NULL, /* delete an connection */ | |
113 | ngx_epoll_add_connection, /* add an connection */ | |
114 | ngx_epoll_del_connection, /* delete an connection */ | |
115 | 115 | NULL, /* process the changes */ |
116 | 116 | ngx_epoll_process_events, /* process the events */ |
117 | 117 | ngx_epoll_init, /* init the events */ |
123 | 123 | NGX_MODULE, |
124 | 124 | &ngx_epoll_module_ctx, /* module context */ |
125 | 125 | ngx_epoll_commands, /* module directives */ |
126 | NGX_EVENT_MODULE, /* module type */ | |
127 | NULL, /* init module */ | |
128 | NULL /* init process */ | |
126 | NGX_EVENT_MODULE, /* module type */ | |
127 | NULL, /* init module */ | |
128 | NULL /* init process */ | |
129 | 129 | }; |
130 | 130 | |
131 | 131 | |
173 | 173 | ngx_event_flags = NGX_USE_LEVEL_EVENT |
174 | 174 | #endif |
175 | 175 | |NGX_HAVE_GREEDY_EVENT |
176 | |NGX_HAVE_INSTANCE_EVENT; | |
176 | |NGX_USE_EPOLL_EVENT; | |
177 | 177 | |
178 | 178 | return NGX_OK; |
179 | 179 | } |
305 | 305 | } |
306 | 306 | |
307 | 307 | |
308 | #if 0 | |
309 | 308 | static int ngx_epoll_add_connection(ngx_connection_t *c) |
310 | 309 | { |
311 | 310 | struct epoll_event ee; |
329 | 328 | } |
330 | 329 | |
331 | 330 | |
332 | static int ngx_epoll_del_connection(ngx_connection_t *c) | |
333 | { | |
331 | static int ngx_epoll_del_connection(ngx_connection_t *c, u_int flags) | |
332 | { | |
333 | int op; | |
334 | struct epoll_event ee; | |
335 | ||
336 | /* | |
337 | * when the file descriptor is closed the epoll automatically deletes | |
338 | * it from its queue so we do not need to delete explicity the event | |
339 | * before the closing the file descriptor | |
340 | */ | |
341 | ||
342 | if (flags & NGX_CLOSE_EVENT) { | |
343 | c->read->active = 0; | |
344 | c->write->active = 0; | |
345 | return NGX_OK; | |
346 | } | |
347 | ||
348 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
349 | "epoll del connection: fd:%d", c->fd); | |
350 | ||
351 | op = EPOLL_CTL_DEL; | |
352 | ee.events = 0; | |
353 | ee.data.ptr = NULL; | |
354 | ||
355 | if (epoll_ctl(ep, op, c->fd, &ee) == -1) { | |
356 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, | |
357 | "epoll_ctl(%d, %d) failed", op, c->fd); | |
358 | return NGX_ERROR; | |
359 | } | |
360 | ||
334 | 361 | c->read->active = 0; |
335 | 362 | c->write->active = 0; |
336 | 363 | |
337 | 364 | return NGX_OK; |
338 | 365 | } |
339 | #endif | |
340 | 366 | |
341 | 367 | |
342 | 368 | int ngx_epoll_process_events(ngx_cycle_t *cycle) |
348 | 374 | ngx_err_t err; |
349 | 375 | ngx_log_t *log; |
350 | 376 | ngx_msec_t timer; |
377 | ngx_event_t *rev, *wev; | |
351 | 378 | struct timeval tv; |
352 | 379 | ngx_connection_t *c; |
353 | 380 | ngx_epoch_msec_t delta; |
355 | 382 | for ( ;; ) { |
356 | 383 | timer = ngx_event_find_timer(); |
357 | 384 | |
385 | #if (NGX_THREADS) | |
386 | ||
387 | if (timer == NGX_TIMER_ERROR) { | |
388 | return NGX_ERROR; | |
389 | } | |
390 | ||
391 | if (timer == NGX_TIMER_INFINITE || timer > 500) { | |
392 | timer = 500; | |
393 | break; | |
394 | } | |
395 | ||
396 | #endif | |
397 | ||
358 | 398 | if (timer != 0) { |
359 | 399 | break; |
360 | 400 | } |
364 | 404 | |
365 | 405 | ngx_event_expire_timers((ngx_msec_t) |
366 | 406 | (ngx_elapsed_msec - ngx_old_elapsed_msec)); |
407 | ||
408 | if (ngx_posted_events && ngx_threaded) { | |
409 | ngx_wakeup_worker_thread(cycle); | |
410 | } | |
367 | 411 | } |
368 | 412 | |
369 | 413 | /* NGX_TIMER_INFINITE == INFTIM */ |
437 | 481 | return NGX_ERROR; |
438 | 482 | } |
439 | 483 | |
440 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { | |
441 | ngx_accept_mutex_unlock(); | |
442 | return NGX_ERROR; | |
443 | } | |
444 | ||
445 | lock = 1; | |
484 | if (events > 0) { | |
485 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { | |
486 | ngx_accept_mutex_unlock(); | |
487 | return NGX_ERROR; | |
488 | } | |
489 | ||
490 | lock = 1; | |
491 | ||
492 | } else { | |
493 | lock =0; | |
494 | } | |
495 | ||
446 | 496 | log = cycle->log; |
447 | 497 | |
448 | 498 | for (i = 0; i < events; i++) { |
451 | 501 | instance = (uintptr_t) c & 1; |
452 | 502 | c = (ngx_connection_t *) ((uintptr_t) c & (uintptr_t) ~1); |
453 | 503 | |
454 | if (event_list[i].events & EPOLLIN) { | |
455 | c->read->returned_instance = instance; | |
456 | } | |
457 | ||
458 | if (event_list[i].events & EPOLLOUT) { | |
459 | c->write->returned_instance = instance; | |
460 | } | |
461 | ||
462 | if (c->read->instance != instance) { | |
504 | rev = c->read; | |
505 | ||
506 | if (c->fd == -1 || rev->instance != instance) { | |
463 | 507 | |
464 | 508 | /* |
465 | 509 | * the stale event from a file descriptor |
491 | 535 | c->fd, event_list[i].events); |
492 | 536 | } |
493 | 537 | |
538 | wev = c->write; | |
539 | ||
494 | 540 | if ((event_list[i].events & (EPOLLOUT|EPOLLERR|EPOLLHUP)) |
495 | && c->write->active) | |
541 | && wev->active) | |
496 | 542 | { |
497 | c->write->ready = 1; | |
498 | ||
499 | if (!ngx_threaded && !ngx_accept_mutex_held) { | |
500 | c->write->event_handler(c->write); | |
543 | if (ngx_threaded) { | |
544 | wev->posted_ready = 1; | |
545 | ngx_post_event(wev); | |
501 | 546 | |
502 | 547 | } else { |
503 | ngx_post_event(c->write); | |
548 | wev->ready = 1; | |
549 | ||
550 | if (!ngx_accept_mutex_held) { | |
551 | wev->event_handler(wev); | |
552 | ||
553 | } else { | |
554 | ngx_post_event(wev); | |
555 | } | |
504 | 556 | } |
505 | 557 | } |
506 | 558 | |
511 | 563 | */ |
512 | 564 | |
513 | 565 | if ((event_list[i].events & (EPOLLIN|EPOLLERR|EPOLLHUP)) |
514 | && c->read->active) | |
566 | && rev->active) | |
515 | 567 | { |
516 | c->read->ready = 1; | |
568 | if (ngx_threaded && !rev->accept) { | |
569 | rev->posted_ready = 1; | |
570 | ||
571 | ngx_post_event(rev); | |
572 | ||
573 | continue; | |
574 | } | |
575 | ||
576 | rev->ready = 1; | |
517 | 577 | |
518 | 578 | if (!ngx_threaded && !ngx_accept_mutex_held) { |
519 | c->read->event_handler(c->read); | |
520 | ||
521 | } else if (!c->read->accept) { | |
522 | ngx_post_event(c->read); | |
579 | rev->event_handler(rev); | |
580 | ||
581 | } else if (!rev->accept) { | |
582 | ngx_post_event(rev); | |
523 | 583 | |
524 | 584 | } else if (ngx_accept_disabled <= 0) { |
525 | 585 | |
526 | 586 | ngx_mutex_unlock(ngx_posted_events_mutex); |
527 | 587 | |
528 | c->read->event_handler(c->read); | |
588 | rev->event_handler(rev); | |
529 | 589 | |
530 | 590 | if (ngx_accept_disabled > 0) { |
531 | 591 | ngx_accept_mutex_unlock(); |
559 | 619 | ngx_event_expire_timers((ngx_msec_t) delta); |
560 | 620 | } |
561 | 621 | |
562 | if (!ngx_threaded) { | |
563 | ngx_event_process_posted(cycle); | |
622 | if (ngx_posted_events) { | |
623 | if (ngx_threaded) { | |
624 | ngx_wakeup_worker_thread(cycle); | |
625 | ||
626 | } else { | |
627 | ngx_event_process_posted(cycle); | |
628 | } | |
564 | 629 | } |
565 | 630 | |
566 | 631 | return NGX_OK; |
59 | 59 | NULL, /* disable an event */ |
60 | 60 | NULL, /* add an connection */ |
61 | 61 | ngx_iocp_del_connection, /* delete an connection */ |
62 | NULL, /* process the changes */ | |
62 | 63 | ngx_iocp_process_events, /* process the events */ |
63 | 64 | ngx_iocp_init, /* init the events */ |
64 | 65 | ngx_iocp_done /* done the events */ |
72 | 73 | ngx_iocp_commands, /* module directives */ |
73 | 74 | NGX_EVENT_MODULE, /* module type */ |
74 | 75 | NULL, /* init module */ |
75 | NULL /* init child */ | |
76 | NULL /* init process */ | |
76 | 77 | }; |
77 | 78 | |
78 | 79 |
420 | 420 | timer = ngx_event_find_timer(); |
421 | 421 | |
422 | 422 | #if (NGX_THREADS) |
423 | ||
423 | 424 | if (timer == NGX_TIMER_ERROR) { |
424 | 425 | return NGX_ERROR; |
425 | 426 | } |
441 | 442 | ngx_event_expire_timers((ngx_msec_t) |
442 | 443 | (ngx_elapsed_msec - ngx_old_elapsed_msec)); |
443 | 444 | |
444 | /* TODO: if ngx_threaded then wake up the worker thread */ | |
445 | if (ngx_posted_events && ngx_threaded) { | |
446 | ngx_wakeup_worker_thread(cycle); | |
447 | } | |
445 | 448 | } |
446 | 449 | |
447 | 450 | ngx_old_elapsed_msec = ngx_elapsed_msec; |
13 | 13 | static ngx_int_t ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags); |
14 | 14 | static ngx_int_t ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags); |
15 | 15 | static ngx_int_t ngx_poll_process_events(ngx_cycle_t *cycle); |
16 | static char *ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf); | |
16 | 17 | |
17 | 18 | |
18 | 19 | static struct pollfd *event_list; |
30 | 31 | ngx_event_module_t ngx_poll_module_ctx = { |
31 | 32 | &poll_name, |
32 | 33 | NULL, /* create configuration */ |
33 | NULL, /* init configuration */ | |
34 | ngx_poll_init_conf, /* init configuration */ | |
34 | 35 | |
35 | 36 | { |
36 | 37 | ngx_poll_add_event, /* add an event */ |
576 | 577 | |
577 | 578 | return nready; |
578 | 579 | } |
580 | ||
581 | ||
582 | static char *ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf) | |
583 | { | |
584 | ngx_event_conf_t *ecf; | |
585 | ||
586 | ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); | |
587 | ||
588 | if (ecf->use != ngx_poll_module.ctx_index) { | |
589 | return NGX_CONF_OK; | |
590 | } | |
591 | ||
592 | #if (NGX_THREADS) | |
593 | ||
594 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
595 | "poll() is not supported in the threaded mode"); | |
596 | return NGX_CONF_ERROR; | |
597 | ||
598 | #else | |
599 | ||
600 | return NGX_CONF_OK; | |
601 | ||
602 | #endif | |
603 | } |
157 | 157 | |
158 | 158 | ngx_event_actions = ngx_rtsig_module_ctx.actions; |
159 | 159 | |
160 | ngx_event_flags = NGX_USE_RTSIG_EVENT | |
161 | |NGX_HAVE_GREEDY_EVENT | |
162 | |NGX_HAVE_INSTANCE_EVENT; | |
160 | ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_HAVE_GREEDY_EVENT; | |
163 | 161 | |
164 | 162 | return NGX_OK; |
165 | 163 | } |
274 | 272 | ngx_msec_t timer; |
275 | 273 | ngx_err_t err; |
276 | 274 | siginfo_t si; |
275 | ngx_event_t *rev, *wev; | |
277 | 276 | struct timeval tv; |
278 | 277 | struct timespec ts, *tp; |
279 | 278 | struct sigaction sa; |
289 | 288 | for ( ;; ) { |
290 | 289 | timer = ngx_event_find_timer(); |
291 | 290 | |
291 | #if (NGX_THREADS) | |
292 | ||
293 | if (timer == NGX_TIMER_ERROR) { | |
294 | return NGX_ERROR; | |
295 | } | |
296 | ||
297 | if (timer == NGX_TIMER_INFINITE || timer > 500) { | |
298 | timer = 500; | |
299 | break; | |
300 | } | |
301 | ||
302 | #endif | |
303 | ||
292 | 304 | if (timer != 0) { |
293 | 305 | break; |
294 | 306 | } |
298 | 310 | |
299 | 311 | ngx_event_expire_timers((ngx_msec_t) |
300 | 312 | (ngx_elapsed_msec - ngx_old_elapsed_msec)); |
313 | ||
314 | if (ngx_posted_events && ngx_threaded) { | |
315 | ngx_wakeup_worker_thread(cycle); | |
316 | } | |
301 | 317 | } |
302 | 318 | |
303 | 319 | expire = 1; |
339 | 355 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
340 | 356 | "rtsig timer: %d", timer); |
341 | 357 | |
342 | /* Linux sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */ | |
358 | /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */ | |
343 | 359 | |
344 | 360 | signo = sigtimedwait(&set, &si, tp); |
345 | 361 | |
399 | 415 | |
400 | 416 | instance = signo - rtscf->signo; |
401 | 417 | |
402 | if (si.si_band & POLLIN) { | |
403 | c->read->returned_instance = instance; | |
404 | } | |
405 | ||
406 | if (si.si_band & POLLOUT) { | |
407 | c->write->returned_instance = instance; | |
408 | } | |
409 | ||
418 | rev = c->read; | |
419 | ||
410 | 420 | if (c->read->instance != instance) { |
411 | 421 | |
412 | 422 | /* |
423 | 433 | } |
424 | 434 | |
425 | 435 | if (si.si_band & (POLLIN|POLLHUP|POLLERR)) { |
426 | if (c->read->active) { | |
427 | c->read->ready = 1; | |
428 | ||
429 | if (!ngx_threaded && !ngx_accept_mutex_held) { | |
430 | c->read->event_handler(c->read); | |
431 | ||
432 | } else if (c->read->accept) { | |
433 | if (ngx_accept_disabled <= 0) { | |
434 | c->read->event_handler(c->read); | |
435 | } | |
436 | ||
437 | } else { | |
436 | if (rev->active) { | |
437 | ||
438 | if (ngx_threaded && !rev->accept) { | |
438 | 439 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { |
439 | 440 | ngx_accept_mutex_unlock(); |
440 | 441 | return NGX_ERROR; |
441 | 442 | } |
442 | 443 | |
443 | ngx_post_event(c->read); | |
444 | rev->posted_ready = 1; | |
445 | ngx_post_event(rev); | |
444 | 446 | |
445 | 447 | ngx_mutex_unlock(ngx_posted_events_mutex); |
448 | ||
449 | } else { | |
450 | rev->ready = 1; | |
451 | ||
452 | if (!ngx_threaded && !ngx_accept_mutex_held) { | |
453 | rev->event_handler(rev); | |
454 | ||
455 | } else if (rev->accept) { | |
456 | if (ngx_accept_disabled <= 0) { | |
457 | rev->event_handler(rev); | |
458 | } | |
459 | ||
460 | } else { | |
461 | ngx_post_event(rev); | |
462 | } | |
446 | 463 | } |
447 | 464 | } |
448 | 465 | } |
449 | 466 | |
467 | wev = c->write; | |
468 | ||
450 | 469 | if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) { |
451 | if (c->write->active) { | |
452 | c->write->ready = 1; | |
453 | ||
454 | if (!ngx_threaded && !ngx_accept_mutex_held) { | |
455 | c->write->event_handler(c->write); | |
456 | ||
457 | } else { | |
458 | ||
470 | if (wev->active) { | |
471 | ||
472 | if (ngx_threaded) { | |
459 | 473 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { |
460 | 474 | ngx_accept_mutex_unlock(); |
461 | 475 | return NGX_ERROR; |
462 | 476 | } |
463 | 477 | |
464 | ngx_post_event(c->write); | |
478 | wev->posted_ready = 1; | |
479 | ngx_post_event(wev); | |
465 | 480 | |
466 | 481 | ngx_mutex_unlock(ngx_posted_events_mutex); |
482 | ||
483 | } else { | |
484 | wev->ready = 1; | |
485 | ||
486 | if (!ngx_threaded && !ngx_accept_mutex_held) { | |
487 | wev->event_handler(wev); | |
488 | ||
489 | } else { | |
490 | ngx_post_event(wev); | |
491 | } | |
467 | 492 | } |
468 | 493 | } |
469 | 494 | } |
511 | 536 | ngx_event_expire_timers((ngx_msec_t) delta); |
512 | 537 | } |
513 | 538 | |
514 | if (!ngx_threaded) { | |
515 | ngx_event_process_posted(cycle); | |
539 | if (ngx_posted_events) { | |
540 | if (ngx_threaded) { | |
541 | ngx_wakeup_worker_thread(cycle); | |
542 | ||
543 | } else { | |
544 | ngx_event_process_posted(cycle); | |
545 | } | |
516 | 546 | } |
517 | 547 | |
518 | 548 | if (signo == -1) { |
531 | 561 | size_t len; |
532 | 562 | ngx_int_t tested, n, i; |
533 | 563 | ngx_err_t err; |
564 | ngx_event_t *rev, *wev; | |
534 | 565 | ngx_connection_t *c; |
535 | 566 | ngx_rtsig_conf_t *rtscf; |
536 | 567 | |
586 | 617 | cycle->log, 0, |
587 | 618 | "poll() failed while the overflow recover"); |
588 | 619 | |
589 | if (err == NGX_EINTR) { | |
590 | continue; | |
620 | if (err != NGX_EINTR) { | |
621 | break; | |
591 | 622 | } |
592 | 623 | } |
593 | ||
594 | break; | |
595 | 624 | } |
596 | 625 | |
597 | 626 | if (ready <= 0) { |
598 | 627 | continue; |
599 | 628 | } |
600 | 629 | |
630 | if (n) { | |
631 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { | |
632 | return NGX_ERROR; | |
633 | } | |
634 | } | |
635 | ||
601 | 636 | for (i = 0; i < n; i++) { |
602 | 637 | c = &cycle->connections[overflow_list[i].fd]; |
603 | 638 | |
639 | rev = c->read; | |
640 | ||
604 | 641 | if (overflow_list[i].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) { |
605 | 642 | tested++; |
606 | c->read->ready = 1; | |
607 | ||
608 | if (!ngx_threaded) { | |
609 | c->read->event_handler(c->read); | |
643 | ||
644 | if (ngx_threaded) { | |
645 | rev->posted_ready = 1; | |
646 | ngx_post_event(rev); | |
610 | 647 | |
611 | 648 | } else { |
612 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { | |
613 | return NGX_ERROR; | |
614 | } | |
615 | ||
616 | ngx_post_event(c->read); | |
617 | c->read->returned_instance = c->read->instance; | |
618 | ||
619 | ngx_mutex_unlock(ngx_posted_events_mutex); | |
649 | rev->ready = 1; | |
650 | rev->event_handler(rev); | |
620 | 651 | } |
621 | 652 | } |
653 | ||
654 | wev = c->write; | |
622 | 655 | |
623 | 656 | if (overflow_list[i].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) { |
624 | 657 | tested++; |
625 | c->write->ready = 1; | |
626 | ||
627 | if (!ngx_threaded) { | |
628 | c->write->event_handler(c->write); | |
658 | ||
659 | if (ngx_threaded) { | |
660 | wev->posted_ready = 1; | |
661 | ngx_post_event(wev); | |
629 | 662 | |
630 | 663 | } else { |
631 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { | |
632 | return NGX_ERROR; | |
633 | } | |
634 | ||
635 | ngx_post_event(c->write); | |
636 | c->write->returned_instance = c->write->instance; | |
637 | ||
638 | ngx_mutex_unlock(ngx_posted_events_mutex); | |
664 | wev->ready = 1; | |
665 | wev->event_handler(wev); | |
639 | 666 | } |
640 | 667 | } |
668 | } | |
669 | ||
670 | if (n) { | |
671 | ngx_mutex_unlock(ngx_posted_events_mutex); | |
641 | 672 | } |
642 | 673 | |
643 | 674 | if (tested >= rtscf->overflow_test) { |
682 | 713 | } |
683 | 714 | } |
684 | 715 | |
685 | if (!ngx_threaded) { | |
686 | ngx_event_process_posted(cycle); | |
716 | if (ngx_posted_events) { | |
717 | if (ngx_threaded) { | |
718 | ngx_wakeup_worker_thread(cycle); | |
719 | ||
720 | } else { | |
721 | ngx_event_process_posted(cycle); | |
722 | } | |
687 | 723 | } |
688 | 724 | |
689 | 725 | ngx_log_error(NGX_LOG_INFO, cycle->log, 0, |
14 | 14 | static ngx_int_t ngx_select_add_event(ngx_event_t *ev, int event, u_int flags); |
15 | 15 | static ngx_int_t ngx_select_del_event(ngx_event_t *ev, int event, u_int flags); |
16 | 16 | static ngx_int_t ngx_select_process_events(ngx_cycle_t *cycle); |
17 | ||
18 | 17 | static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf); |
19 | 18 | |
20 | 19 | |
604 | 603 | } |
605 | 604 | #endif |
606 | 605 | |
606 | #if (NGX_THREADS) | |
607 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
608 | "select() is not supported in the threaded mode"); | |
609 | return NGX_CONF_ERROR; | |
610 | #else | |
607 | 611 | return NGX_CONF_OK; |
612 | #endif | |
608 | 613 | } |
40 | 40 | unsigned oneshot:1; |
41 | 41 | |
42 | 42 | /* used to detect the stale events in kqueue, rt signals and epoll */ |
43 | unsigned use_instance:1; | |
44 | 43 | unsigned instance:1; |
45 | unsigned returned_instance:1; | |
46 | 44 | |
47 | 45 | /* |
48 | 46 | * the event was passed or would be passed to a kernel; |
74 | 72 | |
75 | 73 | /* the pending eof reported by kqueue or in aio chain operation */ |
76 | 74 | unsigned pending_eof:1; |
75 | ||
76 | #if !(NGX_THREADS) | |
77 | unsigned posted_ready:1; | |
78 | #endif | |
77 | 79 | |
78 | 80 | #if (WIN32) |
79 | 81 | /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was succesfull */ |
243 | 245 | #define NGX_HAVE_LOWAT_EVENT 0x00000010 |
244 | 246 | |
245 | 247 | /* |
246 | * The event filter allows to pass instance information to check stale events - | |
247 | * kqueue, epoll, rt signals. | |
248 | */ | |
249 | #define NGX_HAVE_INSTANCE_EVENT 0x00000020 | |
250 | ||
251 | /* | |
252 | 248 | * The event filter requires to do i/o operation until EAGAIN - |
253 | 249 | * epoll, rt signals. |
254 | 250 | */ |
255 | #define NGX_HAVE_GREEDY_EVENT 0x00000040 | |
256 | ||
257 | /* | |
258 | * The event filter notifies only the changes (the edges) | |
259 | * but not an initial level - early epoll patches. | |
260 | */ | |
261 | #define NGX_USE_EDGE_EVENT 0x00000080 | |
251 | #define NGX_HAVE_GREEDY_EVENT 0x00000020 | |
252 | ||
253 | /* | |
254 | * The event filter is epoll, | |
255 | */ | |
256 | #define NGX_USE_EPOLL_EVENT 0x00000040 | |
262 | 257 | |
263 | 258 | /* |
264 | 259 | * No need to add or delete the event filters - rt signals. |
265 | 260 | */ |
266 | #define NGX_USE_RTSIG_EVENT 0x00000100 | |
261 | #define NGX_USE_RTSIG_EVENT 0x00000080 | |
267 | 262 | |
268 | 263 | /* |
269 | 264 | * No need to add or delete the event filters - overlapped, aio_read, |
270 | 265 | * aioread, io_submit. |
271 | 266 | */ |
272 | #define NGX_USE_AIO_EVENT 0x00000200 | |
267 | #define NGX_USE_AIO_EVENT 0x00000100 | |
273 | 268 | |
274 | 269 | /* |
275 | 270 | * Need to add socket or handle only once - i/o completion port. |
276 | 271 | * It also requires HAVE_AIO and NGX_USE_AIO_EVENT to be set. |
277 | 272 | */ |
278 | #define NGX_USE_IOCP_EVENT 0x00000400 | |
273 | #define NGX_USE_IOCP_EVENT 0x00000200 | |
279 | 274 | |
280 | 275 | |
281 | 276 |
15 | 15 | |
16 | 16 | void ngx_event_accept(ngx_event_t *ev) |
17 | 17 | { |
18 | ngx_uint_t instance, rinstance, winstance, accepted; | |
18 | ngx_uint_t instance, accepted; | |
19 | 19 | socklen_t len; |
20 | 20 | struct sockaddr *sa; |
21 | 21 | ngx_err_t err; |
29 | 29 | |
30 | 30 | ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module); |
31 | 31 | |
32 | if (ngx_event_flags & (NGX_USE_EDGE_EVENT|NGX_USE_RTSIG_EVENT)) { | |
32 | if (ngx_event_flags & NGX_USE_RTSIG_EVENT) { | |
33 | 33 | ev->available = 1; |
34 | 34 | |
35 | 35 | } else if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) { |
93 | 93 | err = ngx_socket_errno; |
94 | 94 | |
95 | 95 | if (err == NGX_EAGAIN) { |
96 | if (!(ngx_event_flags | |
97 | & (NGX_USE_EDGE_EVENT|NGX_USE_RTSIG_EVENT))) | |
96 | if (!(ngx_event_flags & NGX_USE_RTSIG_EVENT)) | |
98 | 97 | { |
99 | 98 | ngx_log_error(NGX_LOG_NOTICE, log, err, |
100 | 99 | "EAGAIN after %d accepted connection(s)", |
206 | 205 | #endif |
207 | 206 | |
208 | 207 | instance = rev->instance; |
209 | rinstance = rev->returned_instance; | |
210 | winstance = wev->returned_instance; | |
211 | 208 | |
212 | 209 | #if (NGX_THREADS) |
213 | 210 | |
230 | 227 | c->sockaddr = sa; |
231 | 228 | c->socklen = len; |
232 | 229 | |
233 | if (ngx_event_flags & NGX_HAVE_INSTANCE_EVENT) { | |
234 | rev->use_instance = 1; | |
235 | rev->instance = (u_char) !instance; | |
236 | rev->returned_instance = (u_char) rinstance; | |
237 | ||
238 | wev->use_instance = 1; | |
239 | wev->instance = (u_char) !instance; | |
240 | wev->returned_instance = (u_char) winstance; | |
241 | } | |
230 | rev->instance = !instance; | |
231 | wev->instance = !instance; | |
242 | 232 | |
243 | 233 | rev->index = NGX_INVALID_INDEX; |
244 | 234 | wev->index = NGX_INVALID_INDEX; |
255 | 245 | wev->write = 1; |
256 | 246 | wev->ready = 1; |
257 | 247 | |
258 | if (ngx_event_flags | |
259 | & (NGX_USE_AIO_EVENT|NGX_USE_EDGE_EVENT|NGX_USE_RTSIG_EVENT)) | |
260 | { | |
248 | if (ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_RTSIG_EVENT)) { | |
261 | 249 | /* epoll, rtsig, aio, iocp */ |
262 | 250 | rev->ready = 1; |
263 | 251 | } |
264 | 252 | |
265 | 253 | if (ev->deferred_accept) { |
266 | 254 | rev->ready = 1; |
267 | } | |
268 | ||
269 | if (rev->ready) { | |
270 | rev->returned_instance = rev->instance; | |
271 | 255 | } |
272 | 256 | |
273 | 257 | c->ctx = ls->ctx; |
317 | 301 | } |
318 | 302 | #endif |
319 | 303 | |
320 | if (ngx_add_conn) { | |
304 | if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) { | |
321 | 305 | if (ngx_add_conn(c) == NGX_ERROR) { |
322 | 306 | if (ngx_close_socket(s) == -1) { |
323 | 307 | ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno, |
10 | 10 | int ngx_event_connect_peer(ngx_peer_connection_t *pc) |
11 | 11 | { |
12 | 12 | int rc; |
13 | ngx_uint_t instance, rinstance, winstance; | |
13 | ngx_uint_t instance; | |
14 | 14 | u_int event; |
15 | 15 | time_t now; |
16 | 16 | ngx_err_t err; |
29 | 29 | |
30 | 30 | /* cached connection */ |
31 | 31 | |
32 | pc->connection = pc->peers->cached[pc->peers->last_cached]; | |
32 | c = pc->peers->cached[pc->peers->last_cached]; | |
33 | 33 | pc->peers->last_cached--; |
34 | 34 | |
35 | 35 | /* ngx_unlock_mutex(pc->peers->mutex); */ |
36 | 36 | |
37 | #if (NGX_THREADS) | |
38 | c->read->lock = c->read->own_lock; | |
39 | c->write->lock = c->write->own_lock; | |
40 | #endif | |
41 | ||
42 | pc->connection = c; | |
37 | 43 | pc->cached = 1; |
38 | 44 | return NGX_OK; |
39 | 45 | } |
179 | 185 | #endif |
180 | 186 | |
181 | 187 | instance = rev->instance; |
182 | rinstance = rev->returned_instance; | |
183 | winstance = wev->returned_instance; | |
184 | 188 | |
185 | 189 | #if (NGX_THREADS) |
186 | 190 | |
197 | 201 | ngx_memzero(rev, sizeof(ngx_event_t)); |
198 | 202 | ngx_memzero(wev, sizeof(ngx_event_t)); |
199 | 203 | |
200 | if (ngx_event_flags & NGX_HAVE_INSTANCE_EVENT) { | |
201 | rev->use_instance = 1; | |
202 | rev->instance = (u_char) !instance; | |
203 | rev->returned_instance = (u_char) rinstance; | |
204 | ||
205 | wev->use_instance = 1; | |
206 | wev->instance = (u_char) !instance; | |
207 | wev->returned_instance = (u_char) winstance; | |
208 | } | |
204 | rev->instance = !instance; | |
205 | wev->instance = !instance; | |
209 | 206 | |
210 | 207 | rev->index = NGX_INVALID_INDEX; |
211 | 208 | wev->index = NGX_INVALID_INDEX; |
72 | 72 | |
73 | 73 | ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle) |
74 | 74 | { |
75 | ngx_tls_t *tls; | |
76 | 75 | ngx_event_t *ev; |
77 | ||
78 | tls = ngx_thread_get_tls(); | |
79 | 76 | |
80 | 77 | for ( ;; ) { |
81 | 78 | |
135 | 132 | |
136 | 133 | ngx_mutex_unlock(ngx_posted_events_mutex); |
137 | 134 | |
138 | tls->event = ev; | |
139 | ||
140 | 135 | ev->event_handler(ev); |
141 | 136 | |
142 | 137 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { |
135 | 135 | } |
136 | 136 | |
137 | 137 | ev->posted_timedout = 1; |
138 | ev->returned_instance = ev->instance; | |
139 | 138 | ngx_post_event(ev); |
140 | 139 | |
141 | 140 | ngx_mutex_unlock(ngx_posted_events_mutex); |
455 | 455 | ngx_http_core_loc_conf_t *clcf, **clcfp; |
456 | 456 | ngx_http_core_srv_conf_t *cscf; |
457 | 457 | #if (HAVE_PCRE) |
458 | ngx_uint_t exact; | |
458 | ngx_uint_t exact; | |
459 | 459 | #endif |
460 | 460 | |
461 | 461 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
193 | 193 | rev->data = c; |
194 | 194 | wev->data = c; |
195 | 195 | |
196 | #if (NGX_THREADS) | |
197 | rev->lock = &c->lock; | |
198 | wev->lock = &c->lock; | |
199 | rev->own_lock = &c->lock; | |
200 | wev->own_lock = &c->lock; | |
201 | #endif | |
202 | ||
196 | 203 | ev = (event == NGX_READ_EVENT) ? rev : wev; |
197 | 204 | |
198 | 205 | ev->event_handler = handler; |
11 | 11 | #define NGX_ESRCH ESRCH |
12 | 12 | #define NGX_EINTR EINTR |
13 | 13 | #define NGX_ECHILD ECHILD |
14 | #define NGX_ENOMEM ENOMEM | |
14 | 15 | #define NGX_EACCES EACCES |
15 | 16 | #define NGX_EBUSY EBUSY |
16 | 17 | #define NGX_EEXIST EEXIST |
17 | 18 | #define NGX_ENOTDIR ENOTDIR |
19 | #define NGX_EINVAL EINVAL | |
18 | 20 | #define NGX_EPIPE EPIPE |
19 | 21 | #define NGX_EAGAIN EWOULDBLOCK |
20 | 22 | #define NGX_EINPROGRESS EINPROGRESS |
29 | 29 | */ |
30 | 30 | |
31 | 31 | |
32 | char *ngx_freebsd_kern_usrstack; | |
33 | size_t ngx_thread_stack_size; | |
34 | ||
35 | ||
36 | static size_t rz_size; | |
37 | static size_t usable_stack_size; | |
38 | static char *last_stack; | |
39 | ||
40 | static ngx_uint_t nthreads; | |
41 | static ngx_uint_t max_threads; | |
42 | static ngx_tid_t *tids; /* the threads tids array */ | |
43 | void **ngx_tls; /* the threads tls's array */ | |
32 | char *ngx_freebsd_kern_usrstack; | |
33 | size_t ngx_thread_stack_size; | |
34 | ||
35 | ||
36 | static size_t rz_size; | |
37 | static size_t usable_stack_size; | |
38 | static char *last_stack; | |
39 | ||
40 | static ngx_uint_t nthreads; | |
41 | static ngx_uint_t max_threads; | |
42 | ||
43 | static ngx_uint_t nkeys; | |
44 | static ngx_tid_t *tids; /* the threads tids array */ | |
45 | void **ngx_tls; /* the threads tls's array */ | |
44 | 46 | |
45 | 47 | /* the thread-safe libc errno */ |
46 | 48 | |
235 | 237 | |
236 | 238 | /* create the threads tls's array */ |
237 | 239 | |
238 | if (!(ngx_tls = ngx_calloc((n + 1) * sizeof(void *), cycle->log))) { | |
240 | ngx_tls = ngx_calloc(NGX_THREAD_KEYS_MAX * (n + 1) * sizeof(void *), | |
241 | cycle->log); | |
242 | if (ngx_tls == NULL) { | |
239 | 243 | return NGX_ERROR; |
240 | 244 | } |
241 | 245 | |
269 | 273 | } |
270 | 274 | |
271 | 275 | |
272 | ngx_int_t ngx_thread_set_tls(void *value) | |
273 | { | |
274 | ngx_tls[ngx_gettid()] = value; | |
275 | ||
276 | return NGX_OK; | |
276 | ngx_int_t ngx_thread_key_create(ngx_tls_key_t *key) | |
277 | { | |
278 | if (nkeys >= NGX_THREAD_KEYS_MAX) { | |
279 | return NGX_ENOMEM; | |
280 | } | |
281 | ||
282 | *key = nkeys++; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | ||
288 | ngx_int_t ngx_thread_set_tls(ngx_tls_key_t key, void *value) | |
289 | { | |
290 | if (key >= NGX_THREAD_KEYS_MAX) { | |
291 | return NGX_EINVAL; | |
292 | } | |
293 | ||
294 | ngx_tls[key * NGX_THREAD_KEYS_MAX + ngx_gettid()] = value; | |
295 | return 0; | |
277 | 296 | } |
278 | 297 | |
279 | 298 |
12 | 12 | #define ngx_log_tid 0 |
13 | 13 | |
14 | 14 | #define TID_T_FMT PID_T_FMT |
15 | ||
16 | ||
17 | extern void **ngx_tls; | |
18 | ||
19 | #define ngx_thread_create_tls() 0 | |
20 | #define ngx_thread_create_tls_n "" | |
21 | #define ngx_thread_get_tls() ngx_tls[ngx_gettid()] | |
22 | ngx_int_t ngx_thread_set_tls(void *value); | |
23 | 15 | |
24 | 16 | |
25 | 17 | #define NGX_MUTEX_LIGHT 1 |
86 | 78 | ngx_tid_t ngx_thread_self(); |
87 | 79 | |
88 | 80 | |
81 | typedef ngx_uint_t ngx_tls_key_t; | |
82 | ||
83 | #define NGX_THREAD_KEYS_MAX 16 | |
84 | ||
85 | extern void **ngx_tls; | |
86 | ||
87 | ngx_int_t ngx_thread_key_create(ngx_tls_key_t *key); | |
88 | #define ngx_thread_key_create_n "the tls key creation" | |
89 | ||
90 | ngx_int_t ngx_thread_set_tls(ngx_tls_key_t key, void *value); | |
91 | #define ngx_thread_set_tls_n "the tls key setting" | |
92 | ||
93 | ||
94 | static void *ngx_thread_get_tls(ngx_tls_key_t key) | |
95 | { | |
96 | if (key >= NGX_THREAD_KEYS_MAX) { | |
97 | return NULL; | |
98 | } | |
99 | ||
100 | return ngx_tls[key * NGX_THREAD_KEYS_MAX + ngx_gettid()]; | |
101 | } | |
102 | ||
103 | ||
89 | 104 | #define ngx_mutex_trylock(m) ngx_mutex_dolock(m, 1) |
90 | 105 | #define ngx_mutex_lock(m) ngx_mutex_dolock(m, 0) |
91 | 106 | ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try); |
525 | 525 | static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) |
526 | 526 | { |
527 | 527 | sigset_t set; |
528 | ngx_err_t err; | |
528 | 529 | ngx_int_t n; |
529 | 530 | ngx_uint_t i; |
530 | 531 | ngx_listening_t *ls; |
636 | 637 | exit(2); |
637 | 638 | } |
638 | 639 | |
640 | err = ngx_thread_key_create(&ngx_core_tls_key); | |
641 | if (err != 0) { | |
642 | ngx_log_error(NGX_LOG_ALERT, cycle->log, err, | |
643 | ngx_thread_key_create_n " failed"); | |
644 | /* fatal */ | |
645 | exit(2); | |
646 | } | |
647 | ||
639 | 648 | for (n = 0; n < ngx_threads_n; n++) { |
640 | 649 | |
641 | 650 | if (!(ngx_threads[n].cv = ngx_cond_init(cycle->log))) { |
828 | 837 | |
829 | 838 | sigset_t set; |
830 | 839 | ngx_err_t err; |
831 | ngx_tls_t *tls; | |
840 | ngx_core_tls_t *tls; | |
832 | 841 | ngx_cycle_t *cycle; |
833 | 842 | struct timeval tv; |
834 | 843 | |
853 | 862 | |
854 | 863 | ngx_setthrtitle("worker thread"); |
855 | 864 | |
856 | if (!(tls = ngx_calloc(sizeof(ngx_tls_t), cycle->log))) { | |
865 | if (!(tls = ngx_calloc(sizeof(ngx_core_tls_t), cycle->log))) { | |
857 | 866 | return (void *) 1; |
858 | 867 | } |
859 | 868 | |
860 | err = ngx_thread_create_tls(); | |
869 | err = ngx_thread_set_tls(ngx_core_tls_key, tls); | |
861 | 870 | if (err != 0) { |
862 | 871 | ngx_log_error(NGX_LOG_ALERT, cycle->log, err, |
863 | ngx_thread_create_tls_n " failed"); | |
872 | ngx_thread_set_tls_n " failed"); | |
864 | 873 | return (void *) 1; |
865 | 874 | } |
866 | ||
867 | ngx_thread_set_tls(tls); | |
868 | 875 | |
869 | 876 | if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { |
870 | 877 | return (void *) 1; |
882 | 889 | |
883 | 890 | ngx_mutex_unlock(ngx_posted_events_mutex); |
884 | 891 | |
885 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, ngx_errno, | |
892 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, | |
886 | 893 | "thread %d is done", ngx_thread_self()); |
887 | 894 | |
888 | 895 | return (void *) 0; |
898 | 905 | return (void *) 1; |
899 | 906 | } |
900 | 907 | |
901 | if (ngx_process_changes(cycle, 1) == NGX_ERROR) { | |
902 | return (void *) 1; | |
908 | if (ngx_process_changes) { | |
909 | if (ngx_process_changes(cycle, 1) == NGX_ERROR) { | |
910 | return (void *) 1; | |
911 | } | |
903 | 912 | } |
904 | 913 | } |
905 | 914 | } |
15 | 15 | #else /* use pthreads */ |
16 | 16 | |
17 | 17 | #include <pthread.h> |
18 | #include <pthread_np.h> | |
19 | 18 | |
20 | typedef pthread_t ngx_tid_t; | |
19 | typedef pthread_t ngx_tid_t; | |
21 | 20 | |
22 | #define ngx_thread_self() pthread_self() | |
23 | #define ngx_log_tid (int) ngx_thread_self() | |
21 | #define ngx_thread_self() pthread_self() | |
22 | #define ngx_log_tid (int) ngx_thread_self() | |
24 | 23 | |
25 | #define TID_T_FMT PTR_FMT | |
24 | #define TID_T_FMT PTR_FMT | |
26 | 25 | |
27 | 26 | |
28 | #define ngx_thread_create_tls() pthread_key_create(0, NULL) | |
29 | #define ngx_thread_create_tls_n "pthread_key_create(0, NULL)" | |
30 | #define ngx_thread_get_tls() pthread_getspecific(0) | |
31 | #define ngx_thread_set_tls(v) pthread_setspecific(0, v) | |
27 | typedef pthread_key_t ngx_tls_key_t; | |
28 | ||
29 | #define ngx_thread_key_create(key) pthread_key_create(key, NULL) | |
30 | #define ngx_thread_key_create_n "pthread_key_create()" | |
31 | #define ngx_thread_set_tls pthread_setspecific | |
32 | #define ngx_thread_set_tls_n "pthread_setspecific()" | |
33 | #define ngx_thread_get_tls pthread_getspecific | |
32 | 34 | |
33 | 35 | |
34 | 36 | #define NGX_MUTEX_LIGHT 0 |
110 | 112 | #endif |
111 | 113 | |
112 | 114 | |
113 | typedef struct { | |
114 | ngx_event_t *event; | |
115 | } ngx_tls_t; | |
116 | ||
117 | ||
118 | 115 | |
119 | 116 | #endif /* _NGX_THREAD_H_INCLUDED_ */ |