]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
core: Follow up to r1897460: Implement and use ap_thread_current_after_fork().
authorYann Ylavic <ylavic@apache.org>
Tue, 25 Jan 2022 20:28:28 +0000 (20:28 +0000)
committerYann Ylavic <ylavic@apache.org>
Tue, 25 Jan 2022 20:28:28 +0000 (20:28 +0000)
thread_local variables are not (always?) reset on fork(), so we need a way
to set the current_thread to NULL in the child process.

Implement and use ap_thread_current_after_fork() for that.

* include/httpd.h:
  Define ap_thread_current_after_fork().

* server/util.c:
  Implement ap_thread_current_after_fork().

* server/mpm/event/event.c, server/mpm/prefork/prefork.c,
    server/mpm/worker/worker.c:
  Use ap_thread_current_after_fork().

* server/mpm/winnt/child.c:
  Windows processes are not fork()ed and each child runs the main(), so
  ap_thread_current_create() was already called there.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1897472 13f79535-47bb-0310-9956-ffa450edef68

include/httpd.h
server/mpm/event/event.c
server/mpm/prefork/prefork.c
server/mpm/winnt/child.c
server/mpm/worker/worker.c
server/util.c

index 103e04921bdedfca012085cd6468b8542064db5b..419314fe3103b8624a322c4fc7d192dc08f92d03 100644 (file)
@@ -2572,14 +2572,15 @@ AP_DECLARE(void *) ap_realloc(void *ptr, size_t size)
  * APR 1.8+ implement those already.
  */
 #if APR_HAS_THREAD_LOCAL
-#define AP_HAS_THREAD_LOCAL         1
-#define AP_THREAD_LOCAL             APR_THREAD_LOCAL
+#define AP_HAS_THREAD_LOCAL 1
+#define AP_THREAD_LOCAL     APR_THREAD_LOCAL
 #else
-#define AP_HAS_THREAD_LOCAL         0
+#define AP_HAS_THREAD_LOCAL 0
 #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_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) */
 
@@ -2609,6 +2610,7 @@ AP_DECLARE(apr_status_t) ap_thread_create(apr_thread_t **thread,
 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) */
index adb7fcf36f1759d3fd041ef0276aaf3e0a77b5ab..6b97826fc9cef340d3d9ffa4127f13c3d9111371 100644 (file)
@@ -3144,6 +3144,10 @@ static int make_child(server_rec * s, int slot, int bucket)
     }
 
     if (!pid) {
+#if AP_HAS_THREAD_LOCAL
+        ap_thread_current_after_fork();
+#endif
+
         my_bucket = &retained->buckets[bucket];
 
 #ifdef HAVE_BINDPROCESSOR
index 6b5a266d88462fc7d80cc0c06f6fd0acff5bf5fb..8f94f1cc54e914ebeae6499d9376bec722684c79 100644 (file)
@@ -769,6 +769,10 @@ static int make_child(server_rec *s, int slot)
     }
 
     if (!pid) {
+#if AP_HAS_THREAD_LOCAL
+        ap_thread_current_after_fork();
+#endif
+
         my_bucket = &retained->buckets[bucket];
 
 #ifdef HAVE_BINDPROCESSOR
index 4b53eb73ea05d09fb46cab20911e99334f77bd04..6f5dbecd839a118e47526349f79d917fc65416eb 100644 (file)
@@ -891,15 +891,6 @@ static void create_listener_thread(void)
 }
 
 
-#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
-
 void child_main(apr_pool_t *pconf, DWORD parent_pid)
 {
     apr_status_t status;
@@ -921,28 +912,6 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
     apr_pool_create(&pchild, pconf);
     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
-     */
-    {
-        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(10376)
-                         "Couldn't initialize child main thread");
-            exit(APEXIT_CHILDINIT);
-        }
-        apr_pool_cleanup_register(pchild, main_thd, main_thread_cleanup,
-                                  apr_pool_cleanup_null);
-    }
-#endif
-
     ap_run_child_init(pchild, ap_server_conf);
 
     listener_shutdown_event = CreateEvent(NULL, TRUE, FALSE, NULL);
index e4973f146ba263dcbe5cef66ffe3799ba48391b5..2688c8f86fca0608ce5d79dad571dc12601de17b 100644 (file)
@@ -1354,6 +1354,10 @@ static int make_child(server_rec *s, int slot, int bucket)
     }
 
     if (!pid) {
+#if AP_HAS_THREAD_LOCAL
+        ap_thread_current_after_fork();
+#endif
+
         my_bucket = &retained->buckets[bucket];
 
 #ifdef HAVE_BINDPROCESSOR
index 6889ea9a3f1e863280e375a6930072c49b56ac00..2e8899d4dd16a5cf2b419847e8a947634215ea05 100644 (file)
@@ -3302,7 +3302,10 @@ AP_DECLARE(apr_status_t) ap_thread_current_create(apr_thread_t **current,
     apr_allocator_t *allocator;
     apr_pool_t *p;
 
-    *current = NULL;
+    *current = ap_thread_current();
+    if (*current) {
+        return APR_EEXIST;
+    }
 
     rv = apr_allocator_create(&allocator);
     if (rv != APR_SUCCESS) {
@@ -3330,6 +3333,13 @@ AP_DECLARE(apr_status_t) ap_thread_current_create(apr_thread_t **current,
     return APR_SUCCESS;
 }
 
+AP_DECLARE(void) ap_thread_current_after_fork(void)
+{
+#if AP_HAS_THREAD_LOCAL
+    current_thread = NULL;
+#endif
+}
+
 AP_DECLARE(apr_thread_t *) ap_thread_current(void)
 {
 #if AP_HAS_THREAD_LOCAL