On 1/25/22 6:34 PM, yla...@apache.org wrote:
> Author: ylavic
> Date: Tue Jan 25 17:34:57 2022
> New Revision: 1897460
> 
> URL: http://svn.apache.org/viewvc?rev=1897460&view=rev
> Log:
> core: Efficient ap_thread_current() when apr_thread_local() is missing.
> 
> #define ap_thread_create, ap_thread_current_create and ap_thread_current to
> their apr-1.8+ equivalent if available, or implement them using the compiler's
> thread_local mechanism if available, or finally provide stubs otherwise.
> 
> #define AP_HAS_THREAD_LOCAL to 1 in the two former case or 0 otherwise, while
> AP_THREAD_LOCAL is defined to the compiler's keyword iff AP_HAS_THREAD_LOCAL.
> 
> Replace all apr_thread_create() calls with ap_thread_create() so that httpd
> threads can use ap_thread_current()'s pool data as Thread Local Storage.
> 
> Bump MMN minor.
> 
> * include/httpd.h():
>   Define AP_HAS_THREAD_LOCAL, AP_THREAD_LOCAL (eventually), 
> ap_thread_create(),
>   ap_thread_current_create() and ap_thread_current().
>   
> * server/util.c:
>   Implement ap_thread_create(), ap_thread_current_create() and
>   ap_thread_current() when APR < 1.8.
> 
> * modules/core/mod_watchdog.c, modules/http2/h2_workers.c,
>     modules/ssl/mod_ssl_ct.c:
>   Use ap_thread_create() instead of apr_thread_create.
> 
> * server/main.c:
>   Use AP_HAS_THREAD_LOCAL and ap_thread_current_create instead of APR's.
> 
> * server/util_pcre.c:
>   Use AP_HAS_THREAD_LOCAL and ap_thread_current instead of APR's.
> 
> * server/mpm/event/event.c, server/mpm/worker/worker.c,
>     server/mpm/prefork/prefork.c:
>   Use ap_thread_create() instead of apr_thread_create.
>   Create an apr_thread_t/ap_thread_current() for the main chaild thread usable
>   at child_init().
>   
> * server/mpm/winnt/child.c:
>   Use ap_thread_create() instead of CreateThread().
>   Create an apr_thread_t/ap_thread_current() for the main chaild thread usable
> 
> 
> Modified:
>     httpd/httpd/trunk/include/ap_mmn.h
>     httpd/httpd/trunk/include/httpd.h
>     httpd/httpd/trunk/modules/core/mod_watchdog.c
>     httpd/httpd/trunk/modules/http2/h2_workers.c
>     httpd/httpd/trunk/modules/ssl/mod_ssl_ct.c
>     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/winnt/child.c
>     httpd/httpd/trunk/server/mpm/worker/worker.c
>     httpd/httpd/trunk/server/util.c
>     httpd/httpd/trunk/server/util_pcre.c
> 

> 
> Modified: httpd/httpd/trunk/server/mpm/event/event.c
> URL: 
> http://svn.apache.org/viewvc/httpd/httpd/trunk/server/mpm/event/event.c?rev=1897460&r1=1897459&r2=1897460&view=diff
> ==============================================================================
> --- httpd/httpd/trunk/server/mpm/event/event.c (original)
> +++ httpd/httpd/trunk/server/mpm/event/event.c Tue Jan 25 17:34:57 2022

> @@ -2880,6 +2880,15 @@ 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;
> @@ -2902,6 +2911,28 @@ static void child_main(int child_num_arg
>      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
> +     */
> +    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()
> +                         "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
> +
>      /* close unused listeners and pods */
>      for (i = 0; i < retained->mpm->num_buckets; i++) {
>          if (i != child_bucket) {

Would it make sense to move the above code to mpm_common.c as it looks very 
similar for each MPM?

Regards

RĂ¼diger

Reply via email to