]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
core: Follow up to r1897460: Provide ap_thread_main_create().
authorYann Ylavic <ylavic@apache.org>
Thu, 27 Jan 2022 12:34:53 +0000 (12:34 +0000)
committerYann Ylavic <ylavic@apache.org>
Thu, 27 Jan 2022 12:34:53 +0000 (12:34 +0000)
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

include/ap_mmn.h
include/httpd.h
server/main.c
server/mpm/event/event.c
server/mpm/prefork/prefork.c
server/mpm/worker/worker.c
server/util.c

index 6e35c4fee191b29de1e21baaff61fb30c909132e..6060c766d790c6363e528134a19880248a890376 100644 (file)
  *                         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()
  *
  */
index 419314fe3103b8624a322c4fc7d192dc08f92d03..96618c282af019f56d20259a387e803be0de67da 100644 (file)
@@ -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
index 2ca2aea15d1638c49cd92be20623978bc35fb858..2681521542c4dfd6be6bfe3a6d08589ba1f4464c 100644 (file)
@@ -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
 
index 6b97826fc9cef340d3d9ffa4127f13c3d9111371..8fda4bee7b361a01ef1fc02dc13d75413f9beb57 100644 (file)
@@ -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
 
index 8f94f1cc54e914ebeae6499d9376bec722684c79..9679d6406886f81a040ec5ecbf828f28ba833a79 100644 (file)
@@ -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
     {
index 2688c8f86fca0608ce5d79dad571dc12601de17b..ed326a13672b20e61d5a0902796fe97cca32b5ea 100644 (file)
@@ -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
 
index 2e8899d4dd16a5cf2b419847e8a947634215ea05..7b7cbdd60b134253bcfed14d6328283d2761ac01 100644 (file)
@@ -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;