You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by yl...@apache.org on 2022/01/27 12:34:53 UTC

svn commit: r1897543 - in /httpd/httpd/trunk: 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

Author: ylavic
Date: Thu Jan 27 12:34:53 2022
New Revision: 1897543

URL: http://svn.apache.org/viewvc?rev=1897543&view=rev
Log:
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.


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

Modified: httpd/httpd/trunk/include/ap_mmn.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_mmn.h?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/include/ap_mmn.h (original)
+++ httpd/httpd/trunk/include/ap_mmn.h Thu Jan 27 12:34:53 2022
@@ -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()
  *
  */

Modified: httpd/httpd/trunk/include/httpd.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/httpd.h?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/include/httpd.h (original)
+++ httpd/httpd/trunk/include/httpd.h Thu Jan 27 12:34:53 2022
@@ -2579,7 +2579,6 @@ AP_DECLARE(void *) ap_realloc(void *ptr,
 #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_creat
                                           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

Modified: httpd/httpd/trunk/server/main.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/main.c?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/server/main.c (original)
+++ httpd/httpd/trunk/server/main.c Thu Jan 27 12:34:53 2022
@@ -339,15 +339,6 @@ static void reset_process_pconf(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 *ar
     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
 

Modified: httpd/httpd/trunk/server/mpm/event/event.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/event/event.c?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/event/event.c (original)
+++ httpd/httpd/trunk/server/mpm/event/event.c Thu Jan 27 12:34:53 2022
@@ -2880,15 +2880,6 @@ static void join_start_thread(apr_thread
     }
 }
 
-#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
     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
 

Modified: httpd/httpd/trunk/server/mpm/prefork/prefork.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/prefork/prefork.c?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/prefork/prefork.c (original)
+++ httpd/httpd/trunk/server/mpm/prefork/prefork.c Thu Jan 27 12:34:53 2022
@@ -398,15 +398,6 @@ static void child_sigmask(sigset_t *new_
 }
 #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
     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
     {

Modified: httpd/httpd/trunk/server/mpm/worker/worker.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/worker/worker.c?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/worker/worker.c (original)
+++ httpd/httpd/trunk/server/mpm/worker/worker.c Thu Jan 27 12:34:53 2022
@@ -1102,15 +1102,6 @@ static void join_start_thread(apr_thread
     }
 }
 
-#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
     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
 

Modified: httpd/httpd/trunk/server/util.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/util.c?rev=1897543&r1=1897542&r2=1897543&view=diff
==============================================================================
--- httpd/httpd/trunk/server/util.c (original)
+++ httpd/httpd/trunk/server/util.c Thu Jan 27 12:34:53 2022
@@ -3261,9 +3261,16 @@ AP_DECLARE(void *) ap_realloc(void *ptr,
     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_creat
     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_cur
 
 #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;