Klaus Demo nginx / 2b97993
nginx-0.0.7-2004-07-07-19:01:00 import Igor Sysoev 18 years ago
22 changed file(s) with 447 addition(s) and 242 deletion(s). Raw diff Collapse all Expand all
1313 static ngx_event_t ngx_cleaner_event;
1414
1515 ngx_uint_t ngx_test_config;
16
17 #if (NGX_THREADS)
18 ngx_tls_key_t ngx_core_tls_key;
19 #endif
1620
1721
1822 /* STUB NAME */
4848 } ngx_core_conf_t;
4949
5050
51 typedef struct {
52 ngx_pool_t *pool; /* pcre's malloc() pool */
53 } ngx_core_tls_t;
54
55
5156 ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle);
5257 ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle);
5358 void ngx_delete_pidfile(ngx_cycle_t *cycle);
5964 extern ngx_array_t ngx_old_cycles;
6065 extern ngx_module_t ngx_core_module;
6166 extern ngx_uint_t ngx_test_config;
67 #if (NGX_THREADS)
68 extern ngx_tls_key_t ngx_core_tls_key;
69 #endif
6270
6371
6472 #endif /* _NGX_CYCLE_H_INCLUDED_ */
66 static void ngx_regex_free(void *p);
77
88
9 /* THREADS: this pool should be private for each thread */
109 static ngx_pool_t *ngx_pcre_pool;
1110
1211
2019 ngx_regex_t *ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options,
2120 ngx_pool_t *pool, ngx_str_t *err)
2221 {
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
2640
2741 ngx_pcre_pool = pool;
42
43 #endif
2844
2945 re = pcre_compile((const char *) pattern->data, (int) options,
3046 &errstr, &erroff, NULL);
4359
4460 /* ensure that there is no current pool */
4561
62 #if (NGX_THREADS)
63 if (ngx_threaded) {
64 tls->pool = NULL;
65 } else {
66 ngx_pcre_pool = NULL;
67 }
68 #else
4669 ngx_pcre_pool = NULL;
70 #endif
4771
4872 return re;
4973 }
6791
6892 static void *ngx_regex_malloc(size_t size)
6993 {
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);
72110 }
73111
74112 return NULL;
321321 ngx_msec_t timer;
322322 ngx_err_t err;
323323 ngx_cycle_t **old_cycle;
324 ngx_event_t *rev, *wev;
324325 ngx_connection_t *c;
325326 ngx_epoch_msec_t delta;
326327 struct dvpoll dvp;
475476 event_list[i].events, event_list[i].revents);
476477 }
477478
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;
482483
483484 if (!ngx_threaded && !ngx_accept_mutex_held) {
484 c->write->event_handler(c->write);
485 wev->event_handler(wev);
485486
486487 } else {
487 ngx_post_event(c->write);
488 ngx_post_event(wev);
488489 }
489490 }
490491
494495 * if the accept event is the last one.
495496 */
496497
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;
501502
502503 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);
507508
508509 } else if (ngx_accept_disabled <= 0) {
509510 ngx_mutex_unlock(ngx_posted_events_mutex);
510511
511 c->read->event_handler(c->read);
512 c->read->event_handler(rev);
512513
513514 if (ngx_accept_disabled > 0) {
514515 ngx_accept_mutex_unlock();
7474 static int ngx_epoll_add_event(ngx_event_t *ev, int event, u_int flags);
7575 static int ngx_epoll_del_event(ngx_event_t *ev, int event, u_int flags);
7676 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);
7878 static int ngx_epoll_process_events(ngx_cycle_t *cycle);
7979
8080 static void *ngx_epoll_create_conf(ngx_cycle_t *cycle);
110110 ngx_epoll_del_event, /* delete an event */
111111 ngx_epoll_add_event, /* enable an event */
112112 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 */
115115 NULL, /* process the changes */
116116 ngx_epoll_process_events, /* process the events */
117117 ngx_epoll_init, /* init the events */
123123 NGX_MODULE,
124124 &ngx_epoll_module_ctx, /* module context */
125125 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 */
129129 };
130130
131131
173173 ngx_event_flags = NGX_USE_LEVEL_EVENT
174174 #endif
175175 |NGX_HAVE_GREEDY_EVENT
176 |NGX_HAVE_INSTANCE_EVENT;
176 |NGX_USE_EPOLL_EVENT;
177177
178178 return NGX_OK;
179179 }
305305 }
306306
307307
308 #if 0
309308 static int ngx_epoll_add_connection(ngx_connection_t *c)
310309 {
311310 struct epoll_event ee;
329328 }
330329
331330
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
334361 c->read->active = 0;
335362 c->write->active = 0;
336363
337364 return NGX_OK;
338365 }
339 #endif
340366
341367
342368 int ngx_epoll_process_events(ngx_cycle_t *cycle)
348374 ngx_err_t err;
349375 ngx_log_t *log;
350376 ngx_msec_t timer;
377 ngx_event_t *rev, *wev;
351378 struct timeval tv;
352379 ngx_connection_t *c;
353380 ngx_epoch_msec_t delta;
355382 for ( ;; ) {
356383 timer = ngx_event_find_timer();
357384
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
358398 if (timer != 0) {
359399 break;
360400 }
364404
365405 ngx_event_expire_timers((ngx_msec_t)
366406 (ngx_elapsed_msec - ngx_old_elapsed_msec));
407
408 if (ngx_posted_events && ngx_threaded) {
409 ngx_wakeup_worker_thread(cycle);
410 }
367411 }
368412
369413 /* NGX_TIMER_INFINITE == INFTIM */
437481 return NGX_ERROR;
438482 }
439483
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
446496 log = cycle->log;
447497
448498 for (i = 0; i < events; i++) {
451501 instance = (uintptr_t) c & 1;
452502 c = (ngx_connection_t *) ((uintptr_t) c & (uintptr_t) ~1);
453503
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) {
463507
464508 /*
465509 * the stale event from a file descriptor
491535 c->fd, event_list[i].events);
492536 }
493537
538 wev = c->write;
539
494540 if ((event_list[i].events & (EPOLLOUT|EPOLLERR|EPOLLHUP))
495 && c->write->active)
541 && wev->active)
496542 {
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);
501546
502547 } 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 }
504556 }
505557 }
506558
511563 */
512564
513565 if ((event_list[i].events & (EPOLLIN|EPOLLERR|EPOLLHUP))
514 && c->read->active)
566 && rev->active)
515567 {
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;
517577
518578 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);
523583
524584 } else if (ngx_accept_disabled <= 0) {
525585
526586 ngx_mutex_unlock(ngx_posted_events_mutex);
527587
528 c->read->event_handler(c->read);
588 rev->event_handler(rev);
529589
530590 if (ngx_accept_disabled > 0) {
531591 ngx_accept_mutex_unlock();
559619 ngx_event_expire_timers((ngx_msec_t) delta);
560620 }
561621
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 }
564629 }
565630
566631 return NGX_OK;
5959 NULL, /* disable an event */
6060 NULL, /* add an connection */
6161 ngx_iocp_del_connection, /* delete an connection */
62 NULL, /* process the changes */
6263 ngx_iocp_process_events, /* process the events */
6364 ngx_iocp_init, /* init the events */
6465 ngx_iocp_done /* done the events */
7273 ngx_iocp_commands, /* module directives */
7374 NGX_EVENT_MODULE, /* module type */
7475 NULL, /* init module */
75 NULL /* init child */
76 NULL /* init process */
7677 };
7778
7879
420420 timer = ngx_event_find_timer();
421421
422422 #if (NGX_THREADS)
423
423424 if (timer == NGX_TIMER_ERROR) {
424425 return NGX_ERROR;
425426 }
441442 ngx_event_expire_timers((ngx_msec_t)
442443 (ngx_elapsed_msec - ngx_old_elapsed_msec));
443444
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 }
445448 }
446449
447450 ngx_old_elapsed_msec = ngx_elapsed_msec;
1313 static ngx_int_t ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags);
1414 static ngx_int_t ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags);
1515 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);
1617
1718
1819 static struct pollfd *event_list;
3031 ngx_event_module_t ngx_poll_module_ctx = {
3132 &poll_name,
3233 NULL, /* create configuration */
33 NULL, /* init configuration */
34 ngx_poll_init_conf, /* init configuration */
3435
3536 {
3637 ngx_poll_add_event, /* add an event */
576577
577578 return nready;
578579 }
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 }
157157
158158 ngx_event_actions = ngx_rtsig_module_ctx.actions;
159159
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;
163161
164162 return NGX_OK;
165163 }
274272 ngx_msec_t timer;
275273 ngx_err_t err;
276274 siginfo_t si;
275 ngx_event_t *rev, *wev;
277276 struct timeval tv;
278277 struct timespec ts, *tp;
279278 struct sigaction sa;
289288 for ( ;; ) {
290289 timer = ngx_event_find_timer();
291290
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
292304 if (timer != 0) {
293305 break;
294306 }
298310
299311 ngx_event_expire_timers((ngx_msec_t)
300312 (ngx_elapsed_msec - ngx_old_elapsed_msec));
313
314 if (ngx_posted_events && ngx_threaded) {
315 ngx_wakeup_worker_thread(cycle);
316 }
301317 }
302318
303319 expire = 1;
339355 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
340356 "rtsig timer: %d", timer);
341357
342 /* Linux sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
358 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
343359
344360 signo = sigtimedwait(&set, &si, tp);
345361
399415
400416 instance = signo - rtscf->signo;
401417
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
410420 if (c->read->instance != instance) {
411421
412422 /*
423433 }
424434
425435 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) {
438439 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
439440 ngx_accept_mutex_unlock();
440441 return NGX_ERROR;
441442 }
442443
443 ngx_post_event(c->read);
444 rev->posted_ready = 1;
445 ngx_post_event(rev);
444446
445447 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 }
446463 }
447464 }
448465 }
449466
467 wev = c->write;
468
450469 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) {
459473 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
460474 ngx_accept_mutex_unlock();
461475 return NGX_ERROR;
462476 }
463477
464 ngx_post_event(c->write);
478 wev->posted_ready = 1;
479 ngx_post_event(wev);
465480
466481 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 }
467492 }
468493 }
469494 }
511536 ngx_event_expire_timers((ngx_msec_t) delta);
512537 }
513538
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 }
516546 }
517547
518548 if (signo == -1) {
531561 size_t len;
532562 ngx_int_t tested, n, i;
533563 ngx_err_t err;
564 ngx_event_t *rev, *wev;
534565 ngx_connection_t *c;
535566 ngx_rtsig_conf_t *rtscf;
536567
586617 cycle->log, 0,
587618 "poll() failed while the overflow recover");
588619
589 if (err == NGX_EINTR) {
590 continue;
620 if (err != NGX_EINTR) {
621 break;
591622 }
592623 }
593
594 break;
595624 }
596625
597626 if (ready <= 0) {
598627 continue;
599628 }
600629
630 if (n) {
631 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
632 return NGX_ERROR;
633 }
634 }
635
601636 for (i = 0; i < n; i++) {
602637 c = &cycle->connections[overflow_list[i].fd];
603638
639 rev = c->read;
640
604641 if (overflow_list[i].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) {
605642 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);
610647
611648 } 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);
620651 }
621652 }
653
654 wev = c->write;
622655
623656 if (overflow_list[i].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
624657 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);
629662
630663 } 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);
639666 }
640667 }
668 }
669
670 if (n) {
671 ngx_mutex_unlock(ngx_posted_events_mutex);
641672 }
642673
643674 if (tested >= rtscf->overflow_test) {
682713 }
683714 }
684715
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 }
687723 }
688724
689725 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
1414 static ngx_int_t ngx_select_add_event(ngx_event_t *ev, int event, u_int flags);
1515 static ngx_int_t ngx_select_del_event(ngx_event_t *ev, int event, u_int flags);
1616 static ngx_int_t ngx_select_process_events(ngx_cycle_t *cycle);
17
1817 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf);
1918
2019
604603 }
605604 #endif
606605
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
607611 return NGX_CONF_OK;
612 #endif
608613 }
4040 unsigned oneshot:1;
4141
4242 /* used to detect the stale events in kqueue, rt signals and epoll */
43 unsigned use_instance:1;
4443 unsigned instance:1;
45 unsigned returned_instance:1;
4644
4745 /*
4846 * the event was passed or would be passed to a kernel;
7472
7573 /* the pending eof reported by kqueue or in aio chain operation */
7674 unsigned pending_eof:1;
75
76 #if !(NGX_THREADS)
77 unsigned posted_ready:1;
78 #endif
7779
7880 #if (WIN32)
7981 /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was succesfull */
243245 #define NGX_HAVE_LOWAT_EVENT 0x00000010
244246
245247 /*
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 /*
252248 * The event filter requires to do i/o operation until EAGAIN -
253249 * epoll, rt signals.
254250 */
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
262257
263258 /*
264259 * No need to add or delete the event filters - rt signals.
265260 */
266 #define NGX_USE_RTSIG_EVENT 0x00000100
261 #define NGX_USE_RTSIG_EVENT 0x00000080
267262
268263 /*
269264 * No need to add or delete the event filters - overlapped, aio_read,
270265 * aioread, io_submit.
271266 */
272 #define NGX_USE_AIO_EVENT 0x00000200
267 #define NGX_USE_AIO_EVENT 0x00000100
273268
274269 /*
275270 * Need to add socket or handle only once - i/o completion port.
276271 * It also requires HAVE_AIO and NGX_USE_AIO_EVENT to be set.
277272 */
278 #define NGX_USE_IOCP_EVENT 0x00000400
273 #define NGX_USE_IOCP_EVENT 0x00000200
279274
280275
281276
1515
1616 void ngx_event_accept(ngx_event_t *ev)
1717 {
18 ngx_uint_t instance, rinstance, winstance, accepted;
18 ngx_uint_t instance, accepted;
1919 socklen_t len;
2020 struct sockaddr *sa;
2121 ngx_err_t err;
2929
3030 ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module);
3131
32 if (ngx_event_flags & (NGX_USE_EDGE_EVENT|NGX_USE_RTSIG_EVENT)) {
32 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
3333 ev->available = 1;
3434
3535 } else if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) {
9393 err = ngx_socket_errno;
9494
9595 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))
9897 {
9998 ngx_log_error(NGX_LOG_NOTICE, log, err,
10099 "EAGAIN after %d accepted connection(s)",
206205 #endif
207206
208207 instance = rev->instance;
209 rinstance = rev->returned_instance;
210 winstance = wev->returned_instance;
211208
212209 #if (NGX_THREADS)
213210
230227 c->sockaddr = sa;
231228 c->socklen = len;
232229
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;
242232
243233 rev->index = NGX_INVALID_INDEX;
244234 wev->index = NGX_INVALID_INDEX;
255245 wev->write = 1;
256246 wev->ready = 1;
257247
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)) {
261249 /* epoll, rtsig, aio, iocp */
262250 rev->ready = 1;
263251 }
264252
265253 if (ev->deferred_accept) {
266254 rev->ready = 1;
267 }
268
269 if (rev->ready) {
270 rev->returned_instance = rev->instance;
271255 }
272256
273257 c->ctx = ls->ctx;
317301 }
318302 #endif
319303
320 if (ngx_add_conn) {
304 if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {
321305 if (ngx_add_conn(c) == NGX_ERROR) {
322306 if (ngx_close_socket(s) == -1) {
323307 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
1010 int ngx_event_connect_peer(ngx_peer_connection_t *pc)
1111 {
1212 int rc;
13 ngx_uint_t instance, rinstance, winstance;
13 ngx_uint_t instance;
1414 u_int event;
1515 time_t now;
1616 ngx_err_t err;
2929
3030 /* cached connection */
3131
32 pc->connection = pc->peers->cached[pc->peers->last_cached];
32 c = pc->peers->cached[pc->peers->last_cached];
3333 pc->peers->last_cached--;
3434
3535 /* ngx_unlock_mutex(pc->peers->mutex); */
3636
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;
3743 pc->cached = 1;
3844 return NGX_OK;
3945 }
179185 #endif
180186
181187 instance = rev->instance;
182 rinstance = rev->returned_instance;
183 winstance = wev->returned_instance;
184188
185189 #if (NGX_THREADS)
186190
197201 ngx_memzero(rev, sizeof(ngx_event_t));
198202 ngx_memzero(wev, sizeof(ngx_event_t));
199203
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;
209206
210207 rev->index = NGX_INVALID_INDEX;
211208 wev->index = NGX_INVALID_INDEX;
7272
7373 ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle)
7474 {
75 ngx_tls_t *tls;
7675 ngx_event_t *ev;
77
78 tls = ngx_thread_get_tls();
7976
8077 for ( ;; ) {
8178
135132
136133 ngx_mutex_unlock(ngx_posted_events_mutex);
137134
138 tls->event = ev;
139
140135 ev->event_handler(ev);
141136
142137 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
135135 }
136136
137137 ev->posted_timedout = 1;
138 ev->returned_instance = ev->instance;
139138 ngx_post_event(ev);
140139
141140 ngx_mutex_unlock(ngx_posted_events_mutex);
455455 ngx_http_core_loc_conf_t *clcf, **clcfp;
456456 ngx_http_core_srv_conf_t *cscf;
457457 #if (HAVE_PCRE)
458 ngx_uint_t exact;
458 ngx_uint_t exact;
459459 #endif
460460
461461 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
193193 rev->data = c;
194194 wev->data = c;
195195
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
196203 ev = (event == NGX_READ_EVENT) ? rev : wev;
197204
198205 ev->event_handler = handler;
1111 #define NGX_ESRCH ESRCH
1212 #define NGX_EINTR EINTR
1313 #define NGX_ECHILD ECHILD
14 #define NGX_ENOMEM ENOMEM
1415 #define NGX_EACCES EACCES
1516 #define NGX_EBUSY EBUSY
1617 #define NGX_EEXIST EEXIST
1718 #define NGX_ENOTDIR ENOTDIR
19 #define NGX_EINVAL EINVAL
1820 #define NGX_EPIPE EPIPE
1921 #define NGX_EAGAIN EWOULDBLOCK
2022 #define NGX_EINPROGRESS EINPROGRESS
2929 */
3030
3131
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 */
4446
4547 /* the thread-safe libc errno */
4648
235237
236238 /* create the threads tls's array */
237239
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) {
239243 return NGX_ERROR;
240244 }
241245
269273 }
270274
271275
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;
277296 }
278297
279298
1212 #define ngx_log_tid 0
1313
1414 #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);
2315
2416
2517 #define NGX_MUTEX_LIGHT 1
8678 ngx_tid_t ngx_thread_self();
8779
8880
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
89104 #define ngx_mutex_trylock(m) ngx_mutex_dolock(m, 1)
90105 #define ngx_mutex_lock(m) ngx_mutex_dolock(m, 0)
91106 ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try);
525525 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
526526 {
527527 sigset_t set;
528 ngx_err_t err;
528529 ngx_int_t n;
529530 ngx_uint_t i;
530531 ngx_listening_t *ls;
636637 exit(2);
637638 }
638639
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
639648 for (n = 0; n < ngx_threads_n; n++) {
640649
641650 if (!(ngx_threads[n].cv = ngx_cond_init(cycle->log))) {
828837
829838 sigset_t set;
830839 ngx_err_t err;
831 ngx_tls_t *tls;
840 ngx_core_tls_t *tls;
832841 ngx_cycle_t *cycle;
833842 struct timeval tv;
834843
853862
854863 ngx_setthrtitle("worker thread");
855864
856 if (!(tls = ngx_calloc(sizeof(ngx_tls_t), cycle->log))) {
865 if (!(tls = ngx_calloc(sizeof(ngx_core_tls_t), cycle->log))) {
857866 return (void *) 1;
858867 }
859868
860 err = ngx_thread_create_tls();
869 err = ngx_thread_set_tls(ngx_core_tls_key, tls);
861870 if (err != 0) {
862871 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
863 ngx_thread_create_tls_n " failed");
872 ngx_thread_set_tls_n " failed");
864873 return (void *) 1;
865874 }
866
867 ngx_thread_set_tls(tls);
868875
869876 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
870877 return (void *) 1;
882889
883890 ngx_mutex_unlock(ngx_posted_events_mutex);
884891
885 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, ngx_errno,
892 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
886893 "thread %d is done", ngx_thread_self());
887894
888895 return (void *) 0;
898905 return (void *) 1;
899906 }
900907
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 }
903912 }
904913 }
905914 }
1515 #else /* use pthreads */
1616
1717 #include <pthread.h>
18 #include <pthread_np.h>
1918
20 typedef pthread_t ngx_tid_t;
19 typedef pthread_t ngx_tid_t;
2120
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()
2423
25 #define TID_T_FMT PTR_FMT
24 #define TID_T_FMT PTR_FMT
2625
2726
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
3234
3335
3436 #define NGX_MUTEX_LIGHT 0
110112 #endif
111113
112114
113 typedef struct {
114 ngx_event_t *event;
115 } ngx_tls_t;
116
117
118115
119116 #endif /* _NGX_THREAD_H_INCLUDED_ */