From 93c3e6a7546a186965c488e756431a07bfb0ac13 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:10:34 +0100 Subject: [PATCH] Implement GH-10854: TSRM should set a smarter value for expected_threads The tsrm_startup() function is currently always called with expected_threads = 1. This means that the hashtable used in the TSRM will only contain a single bucket, and all thread resources will therefore be in the same linked list. So it's not really a hashtable right now, even though it's supposed to be. This patch adds a function tsrm_startup_ex() which takes the expected thread count as an argument. It also keeps the tsrm_startup() function so there are no BC breaks. In the Apache SAPI we query how many threads we have, and pass that to the tsrm_startup_ex() function. --- main/main.c | 11 ++++++++--- main/php_main.h | 1 + sapi/apache2handler/sapi_apache2.c | 11 ++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/main/main.c b/main/main.c index 8e09f4cd4c0b2..d9defc974cc40 100644 --- a/main/main.c +++ b/main/main.c @@ -2647,13 +2647,18 @@ PHPAPI void php_reserve_tsrm_memory(void) } /* }}} */ -/* {{{ php_tsrm_startup */ -PHPAPI bool php_tsrm_startup(void) +PHPAPI bool php_tsrm_startup_ex(int expected_threads) { - bool ret = tsrm_startup(1, 1, 0, NULL); + bool ret = tsrm_startup(expected_threads, 1, 0, NULL); php_reserve_tsrm_memory(); (void)ts_resource(0); return ret; } + +/* {{{ php_tsrm_startup */ +PHPAPI bool php_tsrm_startup(void) +{ + return php_tsrm_startup_ex(1); +} /* }}} */ #endif diff --git a/main/php_main.h b/main/php_main.h index 561635faea6c3..40c1b773fd2f0 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -47,6 +47,7 @@ extern int php_shutdown_environ(void); #ifdef ZTS PHPAPI void php_reserve_tsrm_memory(void); +PHPAPI bool php_tsrm_startup_ex(int expected_threads); PHPAPI bool php_tsrm_startup(void); #define PHP_ZTS 1 diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c index 30e05aec79ef8..a5d4e8984a5a8 100644 --- a/sapi/apache2handler/sapi_apache2.c +++ b/sapi/apache2handler/sapi_apache2.c @@ -485,7 +485,16 @@ php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override; } #ifdef ZTS - php_tsrm_startup(); + int expected_threads; +#ifdef AP_MPMQ_MAX_THREADS + if (ap_mpm_query(AP_MPMQ_MAX_THREADS, &expected_threads) != APR_SUCCESS) { + expected_threads = 1; + } +#else + expected_threads = 1; +#endif + + php_tsrm_startup_ex(expected_threads); # ifdef PHP_WIN32 ZEND_TSRMLS_CACHE_UPDATE(); # endif