📄 threadpool.c
字号:
static worker_stack *idle_worker_stack;static void wakeup_listener(void){ apr_status_t rv; listener_may_exit = 1; if (!idle_worker_stack) { return; } if ((rv = apr_thread_mutex_lock(idle_worker_stack->mutex)) != APR_SUCCESS) { return; } if ((rv = apr_thread_cond_signal(idle_worker_stack->cond)) != APR_SUCCESS) { return; } if ((rv = apr_thread_mutex_unlock(idle_worker_stack->mutex)) != APR_SUCCESS) { return; } if (!listener_os_thread) { /* XXX there is an obscure path that this doesn't handle perfectly: * right after listener thread is created but before * listener_os_thread is set, the first worker thread hits an * error and starts graceful termination */ return; } /* * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all * platforms and wake up the listener thread since it is the only thread * with SIGHUP unblocked, but that doesn't work on Linux */#ifdef HAVE_PTHREAD_KILL pthread_kill(*listener_os_thread, LISTENER_SIGNAL);#else kill(ap_my_pid, LISTENER_SIGNAL);#endif}#define ST_INIT 0#define ST_GRACEFUL 1#define ST_UNGRACEFUL 2static int terminate_mode = ST_INIT;static void signal_threads(int mode){ if (terminate_mode == mode) { return; } terminate_mode = mode; mpm_state = AP_MPMQ_STOPPING; /* in case we weren't called from the listener thread, wake up the * listener thread */ wakeup_listener(); /* for ungraceful termination, let the workers exit now; * for graceful termination, the listener thread will notify the * workers to exit once it has stopped accepting new connections */ if (mode == ST_UNGRACEFUL) { workers_may_exit = 1; worker_stack_terminate(idle_worker_stack); }}AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result){ switch(query_code){ case AP_MPMQ_MAX_DAEMON_USED: *result = ap_max_daemons_limit; return APR_SUCCESS; case AP_MPMQ_IS_THREADED: *result = AP_MPMQ_STATIC; return APR_SUCCESS; case AP_MPMQ_IS_FORKED: *result = AP_MPMQ_DYNAMIC; return APR_SUCCESS; case AP_MPMQ_HARD_LIMIT_DAEMONS: *result = server_limit; return APR_SUCCESS; case AP_MPMQ_HARD_LIMIT_THREADS: *result = thread_limit; return APR_SUCCESS; case AP_MPMQ_MAX_THREADS: *result = ap_threads_per_child; return APR_SUCCESS; case AP_MPMQ_MIN_SPARE_DAEMONS: *result = 0; return APR_SUCCESS; case AP_MPMQ_MIN_SPARE_THREADS: *result = min_spare_threads; return APR_SUCCESS; case AP_MPMQ_MAX_SPARE_DAEMONS: *result = 0; return APR_SUCCESS; case AP_MPMQ_MAX_SPARE_THREADS: *result = max_spare_threads; return APR_SUCCESS; case AP_MPMQ_MAX_REQUESTS_DAEMON: *result = ap_max_requests_per_child; return APR_SUCCESS; case AP_MPMQ_MAX_DAEMONS: *result = ap_daemons_limit; return APR_SUCCESS; case AP_MPMQ_MPM_STATE: *result = mpm_state; return APR_SUCCESS; } return APR_ENOTIMPL;}/* a clean exit from a child with proper cleanup */ static void clean_child_exit(int code) __attribute__ ((noreturn));static void clean_child_exit(int code){ mpm_state = AP_MPMQ_STOPPING; if (pchild) { apr_pool_destroy(pchild); } exit(code);}static void just_die(int sig){ clean_child_exit(0);}/***************************************************************** * Connection structures and accounting... *//* volatile just in case */static int volatile shutdown_pending;static int volatile restart_pending;static int volatile is_graceful;static volatile int child_fatal;ap_generation_t volatile ap_my_generation;/* * ap_start_shutdown() and ap_start_restart(), below, are a first stab at * functions to initiate shutdown or restart without relying on signals. * Previously this was initiated in sig_term() and restart() signal handlers, * but we want to be able to start a shutdown/restart from other sources -- * e.g. on Win32, from the service manager. Now the service manager can * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that * these functions can also be called by the child processes, since global * variables are no longer used to pass on the required action to the parent. * * These should only be called from the parent process itself, since the * parent process will use the shutdown_pending and restart_pending variables * to determine whether to shutdown or restart. The child process should * call signal_parent() directly to tell the parent to die -- this will * cause neither of those variable to be set, which the parent will * assume means something serious is wrong (which it will be, for the * child to force an exit) and so do an exit anyway. */static void ap_start_shutdown(void){ mpm_state = AP_MPMQ_STOPPING; if (shutdown_pending == 1) { /* Um, is this _probably_ not an error, if the user has * tried to do a shutdown twice quickly, so we won't * worry about reporting it. */ return; } shutdown_pending = 1;}/* do a graceful restart if graceful == 1 */static void ap_start_restart(int graceful){ mpm_state = AP_MPMQ_STOPPING; if (restart_pending == 1) { /* Probably not an error - don't bother reporting it */ return; } restart_pending = 1; is_graceful = graceful;}static void sig_term(int sig){ ap_start_shutdown();}static void restart(int sig){ ap_start_restart(sig == AP_SIG_GRACEFUL);}static void set_signals(void){#ifndef NO_USE_SIGACTION struct sigaction sa;#endif if (!one_process) { ap_fatal_signal_setup(ap_server_conf, pconf); }#ifndef NO_USE_SIGACTION sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sig_term; if (sigaction(SIGTERM, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");#ifdef SIGINT if (sigaction(SIGINT, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");#endif#ifdef SIGXCPU sa.sa_handler = SIG_DFL; if (sigaction(SIGXCPU, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");#endif#ifdef SIGXFSZ sa.sa_handler = SIG_DFL; if (sigaction(SIGXFSZ, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");#endif#ifdef SIGPIPE sa.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");#endif /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy * processing one */ sigaddset(&sa.sa_mask, SIGHUP); sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL); sa.sa_handler = restart; if (sigaction(SIGHUP, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)"); if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");#else if (!one_process) {#ifdef SIGXCPU apr_signal(SIGXCPU, SIG_DFL);#endif /* SIGXCPU */#ifdef SIGXFSZ apr_signal(SIGXFSZ, SIG_DFL);#endif /* SIGXFSZ */ } apr_signal(SIGTERM, sig_term);#ifdef SIGHUP apr_signal(SIGHUP, restart);#endif /* SIGHUP */#ifdef AP_SIG_GRACEFUL apr_signal(AP_SIG_GRACEFUL, restart);#endif /* AP_SIG_GRACEFUL */#ifdef SIGPIPE apr_signal(SIGPIPE, SIG_IGN);#endif /* SIGPIPE */#endif}/***************************************************************** * Here follows a long bunch of generic server bookkeeping stuff... */int ap_graceful_stop_signalled(void) /* XXX this is really a bad confusing obsolete name * maybe it should be ap_mpm_process_exiting? */{ /* note: for a graceful termination, listener_may_exit will be set before * workers_may_exit, so check listener_may_exit */ return listener_may_exit;}/***************************************************************** * Child process main loop. */static void process_socket(apr_pool_t *p, apr_socket_t *sock, int my_child_num, int my_thread_num, apr_bucket_alloc_t *bucket_alloc){ conn_rec *current_conn; long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num); int csd; ap_sb_handle_t *sbh; ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num); apr_os_sock_get(&csd, sock); current_conn = ap_run_create_connection(p, ap_server_conf, sock, conn_id, sbh, bucket_alloc); if (current_conn) { ap_process_connection(current_conn, sock); ap_lingering_close(current_conn); }}/* requests_this_child has gone to zero or below. See if the admin coded "MaxRequestsPerChild 0", and keep going in that case. Doing it this way simplifies the hot path in worker_thread */static void check_infinite_requests(void){ if (ap_max_requests_per_child) { signal_threads(ST_GRACEFUL); } else { /* wow! if you're executing this code, you may have set a record. * either this child process has served over 2 billion requests, or * you're running a threaded 2.0 on a 16 bit machine. * * I'll buy pizza and beers at Apachecon for the first person to do * the former without cheating (dorking with INT_MAX, or running with * uncommitted performance patches, for example). * * for the latter case, you probably deserve a beer too. Greg Ames */ requests_this_child = INT_MAX; /* keep going */ }}static void unblock_signal(int sig){ sigset_t sig_mask; sigemptyset(&sig_mask); sigaddset(&sig_mask, sig);#if defined(SIGPROCMASK_SETS_THREAD_MASK) sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);#else pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);#endif}static void dummy_signal_handler(int sig){ /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall, * then we don't need this goofy function. */}static void *listener_thread(apr_thread_t *thd, void * dummy){ proc_info * ti = dummy; int process_slot = ti->pid; apr_pool_t *tpool = apr_thread_pool_get(thd); void *csd = NULL; apr_pool_t *ptrans; /* Pool for per-transaction stuff */ int n; apr_pollfd_t *pollset; apr_status_t rv; ap_listen_rec *lr, *last_lr = ap_listeners; worker_wakeup_info *worker = NULL; free(ti); apr_poll_setup(&pollset, num_listensocks, tpool); for(lr = ap_listeners ; lr != NULL ; lr = lr->next) apr_poll_socket_add(pollset, lr->sd, APR_POLLIN); /* Unblock the signal used to wake this thread up, and set a handler for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -