comparison src/os/unix/ngx_process_cycle.c @ 376:d0451e402e27

nginx-0.0.7-2004-07-05-10:55:54 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 05 Jul 2004 06:55:54 +0000
parents 744ccb59062d
children 41437e4fd9b4
comparison
equal deleted inserted replaced
375:744ccb59062d 376:d0451e402e27
11 static ngx_uint_t ngx_reap_childs(ngx_cycle_t *cycle); 11 static ngx_uint_t ngx_reap_childs(ngx_cycle_t *cycle);
12 static void ngx_master_exit(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx); 12 static void ngx_master_exit(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx);
13 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); 13 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
14 static void ngx_channel_handler(ngx_event_t *ev); 14 static void ngx_channel_handler(ngx_event_t *ev);
15 #if (NGX_THREADS) 15 #if (NGX_THREADS)
16 static int ngx_worker_thread_cycle(void *data); 16 static void ngx_wakeup_worker_threads(ngx_cycle_t *cycle);
17 static void *ngx_worker_thread_cycle(void *data);
17 #endif 18 #endif
18 19
19 20
20 ngx_uint_t ngx_process; 21 ngx_uint_t ngx_process;
21 ngx_pid_t ngx_pid; 22 ngx_pid_t ngx_pid;
36 ngx_uint_t ngx_daemonized; 37 ngx_uint_t ngx_daemonized;
37 38
38 sig_atomic_t ngx_noaccept; 39 sig_atomic_t ngx_noaccept;
39 ngx_uint_t ngx_noaccepting; 40 ngx_uint_t ngx_noaccepting;
40 ngx_uint_t ngx_restart; 41 ngx_uint_t ngx_restart;
42
43
44 #if (NGX_THREADS)
45 volatile ngx_thread_t ngx_threads[NGX_MAX_THREADS];
46 ngx_int_t ngx_threads_n;
47 #endif
41 48
42 49
43 u_char master_process[] = "master process"; 50 u_char master_process[] = "master process";
44 51
45 52
522 ngx_int_t n; 529 ngx_int_t n;
523 ngx_uint_t i; 530 ngx_uint_t i;
524 ngx_listening_t *ls; 531 ngx_listening_t *ls;
525 ngx_core_conf_t *ccf; 532 ngx_core_conf_t *ccf;
526 ngx_connection_t *c; 533 ngx_connection_t *c;
527 #if (NGX_THREADS)
528 ngx_tid_t tid;
529 #endif
530 534
531 ngx_process = NGX_PROCESS_WORKER; 535 ngx_process = NGX_PROCESS_WORKER;
532 536
533 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 537 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
534 538
618 622
619 ngx_setproctitle("worker process"); 623 ngx_setproctitle("worker process");
620 624
621 #if (NGX_THREADS) 625 #if (NGX_THREADS)
622 626
623 if (ngx_init_threads(5, 128 * 1024 * 1024, cycle) == NGX_ERROR) { 627 if (ngx_time_mutex_init(cycle->log) == NGX_ERROR) {
624 /* fatal */ 628 /* fatal */
625 exit(2); 629 exit(2);
626 } 630 }
627 631
628 if (!(ngx_posted_events_cv = ngx_cond_init(cycle->log))) { 632 if (ngx_threads_n) {
629 /* fatal */ 633 if (ngx_init_threads(ngx_threads_n,
630 exit(2); 634 ccf->thread_stack_size, cycle) == NGX_ERROR)
631 }
632
633 for (i = 0; i < 2; i++) {
634 if (ngx_create_thread(&tid, ngx_worker_thread_cycle,
635 cycle, cycle->log) != 0)
636 { 635 {
637 /* fatal */ 636 /* fatal */
638 exit(2); 637 exit(2);
638 }
639
640 for (n = 0; n < ngx_threads_n; n++) {
641
642 if (!(ngx_threads[n].cv = ngx_cond_init(cycle->log))) {
643 /* fatal */
644 exit(2);
645 }
646
647 if (ngx_create_thread((ngx_tid_t *) &ngx_threads[n].tid,
648 ngx_worker_thread_cycle,
649 (void *) &ngx_threads[n], cycle->log) != 0)
650 {
651 /* fatal */
652 exit(2);
653 }
639 } 654 }
640 } 655 }
641 656
642 #endif 657 #endif
643 658
644 for ( ;; ) { 659 for ( ;; ) {
645 if (ngx_exiting 660 if (ngx_exiting
646 && ngx_event_timer_rbtree == &ngx_event_timer_sentinel) 661 && ngx_event_timer_rbtree == &ngx_event_timer_sentinel)
647 { 662 {
648 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting"); 663 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
664
665
666 #if (NGX_THREADS)
667 ngx_terminate = 1;
668
669 ngx_wakeup_worker_threads(cycle);
670 #endif
671
649 /* 672 /*
650 * we do not destroy cycle->pool here because a signal handler 673 * we do not destroy cycle->pool here because a signal handler
651 * that uses cycle->log can be called at this point 674 * that uses cycle->log can be called at this point
652 */ 675 */
653 exit(0); 676 exit(0);
657 680
658 ngx_process_events(cycle); 681 ngx_process_events(cycle);
659 682
660 if (ngx_terminate) { 683 if (ngx_terminate) {
661 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting"); 684 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
685
686 #if (NGX_THREADS)
687 ngx_wakeup_worker_threads(cycle);
688 #endif
689
662 /* 690 /*
663 * we do not destroy cycle->pool here because a signal handler 691 * we do not destroy cycle->pool here because a signal handler
664 * that uses cycle->log can be called at this point 692 * that uses cycle->log can be called at this point
665 */ 693 */
666 exit(0); 694 exit(0);
750 } 778 }
751 779
752 780
753 #if (NGX_THREADS) 781 #if (NGX_THREADS)
754 782
755 int ngx_worker_thread_cycle(void *data) 783 static void ngx_wakeup_worker_threads(ngx_cycle_t *cycle)
756 { 784 {
757 ngx_cycle_t *cycle = data; 785 ngx_int_t i;
758 786 ngx_uint_t live;
787
788 for ( ;; ) {
789
790 live = 0;
791
792 for (i = 0; i < ngx_threads_n; i++) {
793 if (ngx_threads[i].state < NGX_THREAD_EXIT) {
794 ngx_cond_signal(ngx_threads[i].cv);
795 live = 1;
796 }
797
798 if (ngx_threads[i].state == NGX_THREAD_EXIT) {
799 ngx_thread_join(ngx_threads[i].tid, NULL);
800 ngx_threads[i].state = NGX_THREAD_DONE;
801 }
802 }
803
804 if (live == 0) {
805 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0,
806 "all worker threads are joined");
807
808 /* STUB */
809 ngx_mutex_destroy(ngx_event_timer_mutex);
810 ngx_mutex_destroy(ngx_posted_events_mutex);
811
812 return;
813 }
814
815 ngx_sched_yield();
816 }
817 }
818
819
820 static void* ngx_worker_thread_cycle(void *data)
821 {
822 ngx_thread_t *thr = data;
823
824 sigset_t set;
759 ngx_err_t err; 825 ngx_err_t err;
760 sigset_t set;
761 struct timeval tv; 826 struct timeval tv;
827
828 thr->cv->tid = ngx_thread_self();
762 829
763 sigemptyset(&set); 830 sigemptyset(&set);
764 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL)); 831 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
765 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL)); 832 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
766 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL)); 833 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
767 834
768 err = ngx_thread_sigmask(SIG_BLOCK, &set, NULL); 835 err = ngx_thread_sigmask(SIG_BLOCK, &set, NULL);
769 if (err) { 836 if (err) {
770 ngx_log_error(NGX_LOG_ALERT, cycle->log, err, 837 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
771 ngx_thread_sigmask_n " failed"); 838 ngx_thread_sigmask_n " failed");
772 return 1; 839 return (void *) 1;
773 } 840 }
774 841
775 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, ngx_errno, 842 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, ngx_errno,
776 "thread %d started", ngx_thread_self()); 843 "thread " TID_T_FMT " started", ngx_thread_self());
777 844
778 ngx_setproctitle("worker thread"); 845 ngx_setthrtitle("worker thread");
846
847 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
848 return (void *) 1;
849 }
779 850
780 for ( ;; ) { 851 for ( ;; ) {
781 if (ngx_cond_wait(ngx_posted_events_cv, ngx_posted_events_mutex) 852 thr->state = NGX_THREAD_FREE;
853
854 if (ngx_cond_wait(thr->cv, ngx_posted_events_mutex) == NGX_ERROR) {
855 return (void *) 1;
856 }
857
858 if (ngx_terminate) {
859 thr->state = NGX_THREAD_EXIT;
860
861 ngx_mutex_unlock(ngx_posted_events_mutex);
862
863 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, ngx_errno,
864 "thread %d is done", ngx_thread_self());
865
866 return (void *) 0;
867 }
868
869 thr->state = NGX_THREAD_BUSY;
870
871 if (ngx_event_thread_process_posted((ngx_cycle_t *) ngx_cycle)
782 == NGX_ERROR) 872 == NGX_ERROR)
783 { 873 {
784 return 1; 874 return (void *) 1;
785 } 875 }
786 876 }
787 if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
788 return 1;
789 }
790 }
791
792 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, ngx_errno,
793 "thread %d done", ngx_thread_self());
794
795 return 0;
796 } 877 }
797 878
798 #endif 879 #endif