From 21cfbb9e47def2436772d33c98affc7842806144 Mon Sep 17 00:00:00 2001 From: ylavic Date: Tue, 25 Jan 2022 20:16:03 +0000 Subject: apr_thread: Follow up to r1897207: Provide apr_thread_current_after_fork(). thread_local variables are not (always?) reset on fork(), so APR (and the user) needs a way to set the current_thread to NULL. Use apr_thread_current_after_fork() in apr_proc_fork()'s child process. Merge r1897470 from trunk. Submitted by: ylavic Reviewed by: ylavic git-svn-id: https://svn.apache.org/repos/asf/apr/apr/branches/1.8.x@1897471 13f79535-47bb-0310-9956-ffa450edef68 --- include/apr_thread_proc.h | 8 +++++++- threadproc/beos/proc.c | 3 +++ threadproc/beos/thread.c | 7 +++++++ threadproc/netware/proc.c | 3 +++ threadproc/netware/thread.c | 7 +++++++ threadproc/os2/proc.c | 3 +++ threadproc/os2/thread.c | 7 +++++++ threadproc/unix/proc.c | 3 +++ threadproc/unix/thread.c | 7 +++++++ threadproc/win32/thread.c | 7 +++++++ 10 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/apr_thread_proc.h b/include/apr_thread_proc.h index b6ac1f85d..04759e5a5 100644 --- a/include/apr_thread_proc.h +++ b/include/apr_thread_proc.h @@ -289,7 +289,7 @@ APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new_thread, void *data, apr_pool_t *cont); /** - * Setup the current os_thread as an apr_thread + * Setup the current native thread as an apr_thread * @param current The current apr_thread set up (or reused) * @param attr The threadattr associated with the current thread * @param pool The parent pool of the current thread @@ -299,6 +299,12 @@ APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new_thread, APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current, apr_threadattr_t *attr, apr_pool_t *pool); + +/** + * Clear the current thread after fork() + */ +APR_DECLARE(void) apr_thread_current_after_fork(void); + /** * Get the current thread * @param The current apr_thread, NULL if it is not an apr_thread or if diff --git a/threadproc/beos/proc.c b/threadproc/beos/proc.c index e3698082f..cd6f9a250 100644 --- a/threadproc/beos/proc.c +++ b/threadproc/beos/proc.c @@ -170,6 +170,9 @@ APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool) } } +#if AP_HAS_THREAD_LOCAL + apr_thread_current_after_fork(); +#endif proc->pid = pid; proc->in = NULL; proc->out = NULL; diff --git a/threadproc/beos/thread.c b/threadproc/beos/thread.c index a2f4ef991..0ce9638f6 100644 --- a/threadproc/beos/thread.c +++ b/threadproc/beos/thread.c @@ -183,6 +183,13 @@ APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current, return APR_SUCCESS; } +APR_DECLARE(void) apr_thread_current_after_fork(void) +{ +#if APR_HAS_THREAD_LOCAL + current_thread = NULL; +#endif +} + APR_DECLARE(apr_thread_t *) apr_thread_current(void) { #if APR_HAS_THREAD_LOCAL diff --git a/threadproc/netware/proc.c b/threadproc/netware/proc.c index e5306f9d8..5e0ebcead 100644 --- a/threadproc/netware/proc.c +++ b/threadproc/netware/proc.c @@ -226,6 +226,9 @@ APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool) return errno; } else if (pid == 0) { +#if AP_HAS_THREAD_LOCAL + apr_thread_current_after_fork(); +#endif proc->pid = pid; proc->in = NULL; proc->out = NULL; diff --git a/threadproc/netware/thread.c b/threadproc/netware/thread.c index fe66b46cf..019358a35 100644 --- a/threadproc/netware/thread.c +++ b/threadproc/netware/thread.c @@ -218,6 +218,13 @@ APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current, return APR_SUCCESS; } +APR_DECLARE(void) apr_thread_current_after_fork(void) +{ +#if APR_HAS_THREAD_LOCAL + current_thread = NULL; +#endif +} + APR_DECLARE(apr_thread_t *) apr_thread_current(void) { #if APR_HAS_THREAD_LOCAL diff --git a/threadproc/os2/proc.c b/threadproc/os2/proc.c index 96f76d699..a2f7ab9db 100644 --- a/threadproc/os2/proc.c +++ b/threadproc/os2/proc.c @@ -227,6 +227,9 @@ APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool) return errno; } else if (pid == 0) { +#if AP_HAS_THREAD_LOCAL + apr_thread_current_after_fork(); +#endif proc->pid = pid; proc->in = NULL; proc->out = NULL; diff --git a/threadproc/os2/thread.c b/threadproc/os2/thread.c index 99ae1d292..44f7e7b11 100644 --- a/threadproc/os2/thread.c +++ b/threadproc/os2/thread.c @@ -186,6 +186,13 @@ APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current, return APR_SUCCESS; } +APR_DECLARE(void) apr_thread_current_after_fork(void) +{ +#if APR_HAS_THREAD_LOCAL + current_thread = NULL; +#endif +} + APR_DECLARE(apr_thread_t *) apr_thread_current(void) { #if APR_HAS_THREAD_LOCAL diff --git a/threadproc/unix/proc.c b/threadproc/unix/proc.c index 004772ff8..553d2578d 100644 --- a/threadproc/unix/proc.c +++ b/threadproc/unix/proc.c @@ -226,6 +226,9 @@ APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool) return errno; } else if (pid == 0) { +#if AP_HAS_THREAD_LOCAL + apr_thread_current_after_fork(); +#endif proc->pid = getpid(); apr_random_after_fork(proc); diff --git a/threadproc/unix/thread.c b/threadproc/unix/thread.c index 504cbb4e9..05eb689b8 100644 --- a/threadproc/unix/thread.c +++ b/threadproc/unix/thread.c @@ -259,6 +259,13 @@ APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current, return APR_SUCCESS; } +APR_DECLARE(void) apr_thread_current_after_fork(void) +{ +#if APR_HAS_THREAD_LOCAL + current_thread = NULL; +#endif +} + APR_DECLARE(apr_thread_t *) apr_thread_current(void) { #if APR_HAS_THREAD_LOCAL diff --git a/threadproc/win32/thread.c b/threadproc/win32/thread.c index daeb28db5..e7e9ebc00 100644 --- a/threadproc/win32/thread.c +++ b/threadproc/win32/thread.c @@ -209,6 +209,13 @@ APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current, return APR_SUCCESS; } +APR_DECLARE(void) apr_thread_current_after_fork(void) +{ +#if APR_HAS_THREAD_LOCAL + current_thread = NULL; +#endif +} + APR_DECLARE(apr_thread_t *) apr_thread_current(void) { #if APR_HAS_THREAD_LOCAL -- cgit v1.2.1