From: Yann Ylavic Date: Thu, 27 Jan 2022 12:34:53 +0000 (+0000) Subject: core: Follow up to r1897460: Provide ap_thread_main_create(). X-Git-Tag: 2.5.0-alpha2-ci-test-only~535 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3e0035d1892b7e8506cc2f53921282aaab8e4c23;p=thirdparty%2Fapache%2Fhttpd.git core: Follow up to r1897460: Provide ap_thread_main_create(). Replace ap_thread_current_create() by ap_thread_main_create() which is how it's used by httpd. The former is now a local helper only to implement the latter. This allows to consolidate/factorize common code in the main() of httpd and the unix MPMs. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1897543 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/include/ap_mmn.h b/include/ap_mmn.h index 6e35c4fee19..6060c766d79 100644 --- a/include/ap_mmn.h +++ b/include/ap_mmn.h @@ -700,7 +700,7 @@ * add PROXY_WORKER_UDS_PATH_SIZE. * 20211221.1 (2.5.1-dev) Add read_line to scoreboard. * 20211221.2 (2.5.1-dev) Add AGAIN, AP_MPMQ_CAN_AGAIN. - * 20211221.3 (2.5.1-dev) Add ap_thread_create(), ap_thread_current_create() + * 20211221.3 (2.5.1-dev) Add ap_thread_create(), ap_thread_main_create() * and ap_thread_current() * */ diff --git a/include/httpd.h b/include/httpd.h index 419314fe310..96618c282af 100644 --- a/include/httpd.h +++ b/include/httpd.h @@ -2579,7 +2579,6 @@ AP_DECLARE(void *) ap_realloc(void *ptr, size_t size) #endif #define ap_thread_create apr_thread_create #define ap_thread_current apr_thread_current -#define ap_thread_current_create apr_thread_current_create #define ap_thread_current_after_fork apr_thread_current_after_fork #else /* !APR_VERSION_AT_LEAST(1,8,0) */ @@ -2607,14 +2606,15 @@ AP_DECLARE(apr_status_t) ap_thread_create(apr_thread_t **thread, apr_thread_start_t func, void *data, apr_pool_t *pool); #endif /* AP_THREAD_LOCAL */ -AP_DECLARE(apr_status_t) ap_thread_current_create(apr_thread_t **current, - apr_threadattr_t *attr, - apr_pool_t *pool); + AP_DECLARE(void) ap_thread_current_after_fork(void); AP_DECLARE(apr_thread_t *) ap_thread_current(void); #endif /* !APR_VERSION_AT_LEAST(1,8,0) */ +AP_DECLARE(apr_status_t) ap_thread_main_create(apr_thread_t **thread, + apr_pool_t *pool); + #else /* !APR_HAS_THREADS */ #define AP_HAS_THREAD_LOCAL 0 diff --git a/server/main.c b/server/main.c index 2ca2aea15d1..2681521542c 100644 --- a/server/main.c +++ b/server/main.c @@ -339,15 +339,6 @@ static void reset_process_pconf(process_rec *process) apr_pool_pre_cleanup_register(process->pconf, NULL, deregister_all_hooks); } -#if AP_HAS_THREAD_LOCAL -static apr_status_t main_thread_cleanup(void *arg) -{ - apr_thread_t *thd = arg; - apr_pool_destroy(apr_thread_pool_get(thd)); - return APR_SUCCESS; -} -#endif - static process_rec *init_process(int *argc, const char * const * *argv) { process_rec *process; @@ -400,28 +391,18 @@ static process_rec *init_process(int *argc, const char * const * *argv) process->short_name = apr_filepath_name_get((*argv)[0]); #if AP_HAS_THREAD_LOCAL - /* Create an apr_thread_t for the main thread to set up its - * Thread Local Storage. Since it's detached and it won't - * apr_thread_exit(), destroy its pool before exiting via - * a process->pool cleanup - */ { - apr_thread_t *main_thd = NULL; - apr_threadattr_t *main_thd_attr = NULL; - if (apr_threadattr_create(&main_thd_attr, process->pool) - || apr_threadattr_detach_set(main_thd_attr, 1) - || ap_thread_current_create(&main_thd, main_thd_attr, - process->pool)) { + apr_status_t rv; + apr_thread_t *thd = NULL; + if ((rv = ap_thread_main_create(&thd, process->pool))) { char ctimebuff[APR_CTIME_LEN]; apr_ctime(ctimebuff, apr_time_now()); fprintf(stderr, "[%s] [crit] (%d) %s: %s failed " - "to initialize thread context, exiting\n", - ctimebuff, stat, (*argv)[0], failed); + "to initialize thread context (%i), exiting\n", + ctimebuff, stat, (*argv)[0], failed, rv); apr_terminate(); exit(1); } - apr_pool_cleanup_register(process->pool, main_thd, main_thread_cleanup, - apr_pool_cleanup_null); } #endif diff --git a/server/mpm/event/event.c b/server/mpm/event/event.c index 6b97826fc9c..8fda4bee7b3 100644 --- a/server/mpm/event/event.c +++ b/server/mpm/event/event.c @@ -2880,15 +2880,6 @@ static void join_start_thread(apr_thread_t * start_thread_id) } } -#if AP_HAS_THREAD_LOCAL -static apr_status_t main_thread_cleanup(void *arg) -{ - apr_thread_t *thd = arg; - apr_pool_destroy(apr_thread_pool_get(thd)); - return APR_SUCCESS; -} -#endif - static void child_main(int child_num_arg, int child_bucket) { apr_thread_t **threads; @@ -2912,24 +2903,13 @@ static void child_main(int child_num_arg, int child_bucket) apr_pool_tag(pchild, "pchild"); #if AP_HAS_THREAD_LOCAL - /* Create an apr_thread_t for the main child thread to set up its - * Thread Local Storage. Since it's detached and it won't - * apr_thread_exit(), destroy its pool before exiting via - * a pchild cleanup - */ if (!one_process) { - apr_thread_t *main_thd = NULL; - apr_threadattr_t *main_thd_attr = NULL; - if (apr_threadattr_create(&main_thd_attr, pchild) - || apr_threadattr_detach_set(main_thd_attr, 1) - || ap_thread_current_create(&main_thd, main_thd_attr, - pchild)) { - ap_log_error(APLOG_MARK, APLOG_EMERG, 0, ap_server_conf, APLOGNO(10377) + apr_thread_t *thd = NULL; + if ((rv = ap_thread_main_create(&thd, pchild))) { + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf, APLOGNO(10377) "Couldn't initialize child main thread"); clean_child_exit(APEXIT_CHILDFATAL); } - apr_pool_cleanup_register(pchild, main_thd, main_thread_cleanup, - apr_pool_cleanup_null); } #endif diff --git a/server/mpm/prefork/prefork.c b/server/mpm/prefork/prefork.c index 8f94f1cc54e..9679d640688 100644 --- a/server/mpm/prefork/prefork.c +++ b/server/mpm/prefork/prefork.c @@ -398,15 +398,6 @@ static void child_sigmask(sigset_t *new_mask, sigset_t *old_mask) } #endif -#if AP_HAS_THREAD_LOCAL -static apr_status_t main_thread_cleanup(void *arg) -{ - apr_thread_t *thd = arg; - apr_pool_destroy(apr_thread_pool_get(thd)); - return APR_SUCCESS; -} -#endif - static void child_main(int child_num_arg, int child_bucket) { #if APR_HAS_THREADS @@ -443,26 +434,13 @@ static void child_main(int child_num_arg, int child_bucket) apr_pool_tag(pchild, "pchild"); #if AP_HAS_THREAD_LOCAL - /* Create an apr_thread_t for the main child thread to set up its - * Thread Local Storage. Since it's detached and it won't - * apr_thread_exit(), destroy its pool before exiting via - * a pchild cleanup - */ if (one_process) { thd = ap_thread_current(); } - else { - apr_threadattr_t *main_thd_attr = NULL; - if (apr_threadattr_create(&main_thd_attr, pchild) - || apr_threadattr_detach_set(main_thd_attr, 1) - || ap_thread_current_create(&thd, main_thd_attr, - pchild)) { - ap_log_error(APLOG_MARK, APLOG_EMERG, 0, ap_server_conf, APLOGNO(10378) - "Couldn't initialize child main thread"); - clean_child_exit(APEXIT_CHILDFATAL); - } - apr_pool_cleanup_register(pchild, thd, main_thread_cleanup, - apr_pool_cleanup_null); + else if ((status = ap_thread_main_create(&thd, pchild))) { + ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf, APLOGNO(10378) + "Couldn't initialize child main thread"); + clean_child_exit(APEXIT_CHILDFATAL); } #elif APR_HAS_THREADS { diff --git a/server/mpm/worker/worker.c b/server/mpm/worker/worker.c index 2688c8f86fc..ed326a13672 100644 --- a/server/mpm/worker/worker.c +++ b/server/mpm/worker/worker.c @@ -1102,15 +1102,6 @@ static void join_start_thread(apr_thread_t *start_thread_id) } } -#if AP_HAS_THREAD_LOCAL -static apr_status_t main_thread_cleanup(void *arg) -{ - apr_thread_t *thd = arg; - apr_pool_destroy(apr_thread_pool_get(thd)); - return APR_SUCCESS; -} -#endif - static void child_main(int child_num_arg, int child_bucket) { apr_thread_t **threads; @@ -1133,24 +1124,13 @@ static void child_main(int child_num_arg, int child_bucket) apr_pool_tag(pchild, "pchild"); #if AP_HAS_THREAD_LOCAL - /* Create an apr_thread_t for the main child thread to set up its - * Thread Local Storage. Since it's detached and it won't - * apr_thread_exit(), destroy its pool before exiting via - * a pchild cleanup - */ if (!one_process) { - apr_thread_t *main_thd = NULL; - apr_threadattr_t *main_thd_attr = NULL; - if (apr_threadattr_create(&main_thd_attr, pchild) - || apr_threadattr_detach_set(main_thd_attr, 1) - || ap_thread_current_create(&main_thd, main_thd_attr, - pchild)) { - ap_log_error(APLOG_MARK, APLOG_EMERG, 0, ap_server_conf, APLOGNO(10375) + apr_thread_t *thd = NULL; + if ((rv = ap_thread_main_create(&thd, pchild))) { + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf, APLOGNO(10375) "Couldn't initialize child main thread"); clean_child_exit(APEXIT_CHILDFATAL); } - apr_pool_cleanup_register(pchild, main_thd, main_thread_cleanup, - apr_pool_cleanup_null); } #endif diff --git a/server/util.c b/server/util.c index 2e8899d4dd1..7b7cbdd60b1 100644 --- a/server/util.c +++ b/server/util.c @@ -3261,9 +3261,16 @@ AP_DECLARE(void *) ap_realloc(void *ptr, size_t size) return p; } -#if APR_HAS_THREADS && !APR_VERSION_AT_LEAST(1,8,0) +#if APR_HAS_THREADS + +#if APR_VERSION_AT_LEAST(1,8,0) + +#define ap_thread_current_create apr_thread_current_create + +#else /* !APR_VERSION_AT_LEAST(1,8,0) */ #if AP_HAS_THREAD_LOCAL + struct thread_ctx { apr_thread_start_t func; void *data; @@ -3290,16 +3297,17 @@ AP_DECLARE(apr_status_t) ap_thread_create(apr_thread_t **thread, ctx->data = data; return apr_thread_create(thread, attr, thread_start, ctx, pool); } + #endif /* AP_HAS_THREAD_LOCAL */ -AP_DECLARE(apr_status_t) ap_thread_current_create(apr_thread_t **current, - apr_threadattr_t *attr, - apr_pool_t *pool) +static apr_status_t ap_thread_current_create(apr_thread_t **current, + apr_threadattr_t *attr, + apr_pool_t *pool) { apr_status_t rv; - apr_os_thread_t osthd; apr_abortfunc_t abort_fn = apr_pool_abort_get(pool); apr_allocator_t *allocator; + apr_os_thread_t osthd; apr_pool_t *p; *current = ap_thread_current(); @@ -3351,6 +3359,37 @@ AP_DECLARE(apr_thread_t *) ap_thread_current(void) #endif /* !APR_VERSION_AT_LEAST(1,8,0) */ +static apr_status_t main_thread_cleanup(void *arg) +{ + apr_thread_t *thd = arg; + apr_pool_destroy(apr_thread_pool_get(thd)); + return APR_SUCCESS; +} + +AP_DECLARE(apr_status_t) ap_thread_main_create(apr_thread_t **thread, + apr_pool_t *pool) +{ + apr_status_t rv; + apr_threadattr_t *attr = NULL; + + /* Create an apr_thread_t for the main child thread to set up its Thread + * Local Storage. Since it's detached and won't apr_thread_exit(), destroy + * its pool before exiting via a cleanup of the given pool. + */ + if ((rv = apr_threadattr_create(&attr, pool)) + || (rv = apr_threadattr_detach_set(attr, 1)) + || (rv = ap_thread_current_create(thread, attr, pool))) { + *thread = NULL; + return rv; + } + + apr_pool_cleanup_register(pool, *thread, main_thread_cleanup, + apr_pool_cleanup_null); + return APR_SUCCESS; +} + +#endif /* APR_HAS_THREADS */ + AP_DECLARE(void) ap_get_sload(ap_sload_t *ld) { int i, j, server_limit, thread_limit;