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/25 20:28:28 UTC

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

Author: ylavic
Date: Tue Jan 25 20:28:28 2022
New Revision: 1897472

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


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

Modified: httpd/httpd/trunk/include/httpd.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/httpd.h?rev=1897472&r1=1897471&r2=1897472&view=diff
==============================================================================
--- httpd/httpd/trunk/include/httpd.h (original)
+++ httpd/httpd/trunk/include/httpd.h Tue Jan 25 20:28:28 2022
@@ -2572,14 +2572,15 @@ AP_DECLARE(void *) ap_realloc(void *ptr,
  * 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_creat
 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) */

Modified: httpd/httpd/trunk/server/mpm/event/event.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/event/event.c?rev=1897472&r1=1897471&r2=1897472&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/event/event.c (original)
+++ httpd/httpd/trunk/server/mpm/event/event.c Tue Jan 25 20:28:28 2022
@@ -3144,6 +3144,10 @@ static int make_child(server_rec * s, in
     }
 
     if (!pid) {
+#if AP_HAS_THREAD_LOCAL
+        ap_thread_current_after_fork();
+#endif
+
         my_bucket = &retained->buckets[bucket];
 
 #ifdef HAVE_BINDPROCESSOR

Modified: httpd/httpd/trunk/server/mpm/prefork/prefork.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/prefork/prefork.c?rev=1897472&r1=1897471&r2=1897472&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/prefork/prefork.c (original)
+++ httpd/httpd/trunk/server/mpm/prefork/prefork.c Tue Jan 25 20:28:28 2022
@@ -769,6 +769,10 @@ static int make_child(server_rec *s, int
     }
 
     if (!pid) {
+#if AP_HAS_THREAD_LOCAL
+        ap_thread_current_after_fork();
+#endif
+
         my_bucket = &retained->buckets[bucket];
 
 #ifdef HAVE_BINDPROCESSOR

Modified: httpd/httpd/trunk/server/mpm/winnt/child.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/winnt/child.c?rev=1897472&r1=1897471&r2=1897472&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/winnt/child.c (original)
+++ httpd/httpd/trunk/server/mpm/winnt/child.c Tue Jan 25 20:28:28 2022
@@ -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
     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);

Modified: httpd/httpd/trunk/server/mpm/worker/worker.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/worker/worker.c?rev=1897472&r1=1897471&r2=1897472&view=diff
==============================================================================
--- httpd/httpd/trunk/server/mpm/worker/worker.c (original)
+++ httpd/httpd/trunk/server/mpm/worker/worker.c Tue Jan 25 20:28:28 2022
@@ -1354,6 +1354,10 @@ static int make_child(server_rec *s, int
     }
 
     if (!pid) {
+#if AP_HAS_THREAD_LOCAL
+        ap_thread_current_after_fork();
+#endif
+
         my_bucket = &retained->buckets[bucket];
 
 #ifdef HAVE_BINDPROCESSOR

Modified: httpd/httpd/trunk/server/util.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/util.c?rev=1897472&r1=1897471&r2=1897472&view=diff
==============================================================================
--- httpd/httpd/trunk/server/util.c (original)
+++ httpd/httpd/trunk/server/util.c Tue Jan 25 20:28:28 2022
@@ -3302,7 +3302,10 @@ AP_DECLARE(apr_status_t) ap_thread_curre
     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_curre
     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



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

Posted by Yann Ylavic <yl...@gmail.com>.
On Thu, Jan 27, 2022 at 9:40 AM Ruediger Pluem <rp...@apache.org> wrote:
>
> On 1/25/22 9:28 PM, ylavic@apache.org wrote:
> >
> > -#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
> > -
>
> Why is this no longer needed?

Because Windows children processes run the main() too, so
ap_thread_main_create() has been called already.

Regards;
Yann.

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

Posted by Ruediger Pluem <rp...@apache.org>.

On 1/25/22 9:28 PM, ylavic@apache.org wrote:
> Author: ylavic
> Date: Tue Jan 25 20:28:28 2022
> New Revision: 1897472
> 
> URL: http://svn.apache.org/viewvc?rev=1897472&view=rev
> Log:
> 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.
> 
> 
> Modified:
>     httpd/httpd/trunk/include/httpd.h
>     httpd/httpd/trunk/server/mpm/event/event.c
>     httpd/httpd/trunk/server/mpm/prefork/prefork.c
>     httpd/httpd/trunk/server/mpm/winnt/child.c
>     httpd/httpd/trunk/server/mpm/worker/worker.c
>     httpd/httpd/trunk/server/util.c
> 

> Modified: httpd/httpd/trunk/server/mpm/winnt/child.c
> URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/winnt/child.c?rev=1897472&r1=1897471&r2=1897472&view=diff
> ==============================================================================
> --- httpd/httpd/trunk/server/mpm/winnt/child.c (original)
> +++ httpd/httpd/trunk/server/mpm/winnt/child.c Tue Jan 25 20:28:28 2022
> @@ -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
>      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);
> 

Why is this no longer needed?

Regards

RĂ¼diger