summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYann Ylavic <ylavic@apache.org>2022-01-25 20:28:28 +0000
committerYann Ylavic <ylavic@apache.org>2022-01-25 20:28:28 +0000
commitaa95fe54f45cbd518a1d53bf9eda5cf17acc37f4 (patch)
treef944de6e1c4546f48662ed01956ee9dce571adfe
parent15cf51108fe054a74c6b5846f81927725db16027 (diff)
downloadhttpd-aa95fe54f45cbd518a1d53bf9eda5cf17acc37f4.tar.gz
core: Follow up to r1897460: Implement and use ap_thread_current_after_fork().
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
-rw-r--r--include/httpd.h14
-rw-r--r--server/mpm/event/event.c4
-rw-r--r--server/mpm/prefork/prefork.c4
-rw-r--r--server/mpm/winnt/child.c31
-rw-r--r--server/mpm/worker/worker.c4
-rw-r--r--server/util.c12
6 files changed, 31 insertions, 38 deletions
diff --git a/include/httpd.h b/include/httpd.h
index 103e04921b..419314fe31 100644
--- a/include/httpd.h
+++ b/include/httpd.h
@@ -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) */
diff --git a/server/mpm/event/event.c b/server/mpm/event/event.c
index adb7fcf36f..6b97826fc9 100644
--- a/server/mpm/event/event.c
+++ b/server/mpm/event/event.c
@@ -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
diff --git a/server/mpm/prefork/prefork.c b/server/mpm/prefork/prefork.c
index 6b5a266d88..8f94f1cc54 100644
--- a/server/mpm/prefork/prefork.c
+++ b/server/mpm/prefork/prefork.c
@@ -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
diff --git a/server/mpm/winnt/child.c b/server/mpm/winnt/child.c
index 4b53eb73ea..6f5dbecd83 100644
--- a/server/mpm/winnt/child.c
+++ b/server/mpm/winnt/child.c
@@ -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);
diff --git a/server/mpm/worker/worker.c b/server/mpm/worker/worker.c
index e4973f146b..2688c8f86f 100644
--- a/server/mpm/worker/worker.c
+++ b/server/mpm/worker/worker.c
@@ -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
diff --git a/server/util.c b/server/util.c
index 6889ea9a3f..2e8899d4dd 100644
--- a/server/util.c
+++ b/server/util.c
@@ -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