Minor refactoring of main/main.c and TSRM (#8608)

This commit is contained in:
George Peter Banyard 2022-05-24 08:34:55 +01:00 committed by GitHub
parent 9f06bb3bb6
commit 5ba6ecd523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 77 deletions

View File

@ -112,11 +112,11 @@ static pthread_key_t tls_key;
# define tsrm_tls_get() pthread_getspecific(tls_key)
#endif
TSRM_TLS uint8_t in_main_thread = 0;
TSRM_TLS uint8_t is_thread_shutdown = 0;
TSRM_TLS bool in_main_thread = false;
TSRM_TLS bool is_thread_shutdown = false;
/* Startup TSRM (call once for the entire process) */
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename)
TSRM_API bool tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename)
{/*{{{*/
#ifdef TSRM_WIN32
tls_key = TlsAlloc();
@ -125,8 +125,8 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
#endif
/* ensure singleton */
in_main_thread = 1;
is_thread_shutdown = 0;
in_main_thread = true;
is_thread_shutdown = false;
tsrm_error_file = stderr;
tsrm_error_set(debug_level, debug_filename);
@ -135,7 +135,7 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
tsrm_tls_table = (tsrm_tls_entry **) calloc(tsrm_tls_table_size, sizeof(tsrm_tls_entry *));
if (!tsrm_tls_table) {
TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate TLS table"));
is_thread_shutdown = 1;
is_thread_shutdown = true;
return 0;
}
id_count=0;
@ -144,7 +144,7 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
resource_types_table = (tsrm_resource_type *) calloc(resource_types_table_size, sizeof(tsrm_resource_type));
if (!resource_types_table) {
TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate resource types table"));
is_thread_shutdown = 1;
is_thread_shutdown = true;
free(tsrm_tls_table);
return 0;
}
@ -165,28 +165,24 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
/* Shutdown TSRM (call once for the entire process) */
TSRM_API void tsrm_shutdown(void)
{/*{{{*/
int i;
if (is_thread_shutdown) {
/* shutdown must only occur once */
return;
}
is_thread_shutdown = 1;
is_thread_shutdown = true;
if (!in_main_thread) {
/* only the main thread may shutdown tsrm */
return;
}
for (i=0; i<tsrm_tls_table_size; i++) {
for (int i=0; i<tsrm_tls_table_size; i++) {
tsrm_tls_entry *p = tsrm_tls_table[i], *next_p;
while (p) {
int j;
next_p = p->next;
for (j=0; j<p->count; j++) {
for (int j=0; j<p->count; j++) {
if (p->storage[j]) {
if (resource_types_table) {
if (!resource_types_table[j].done) {
@ -244,9 +240,7 @@ TSRM_API void tsrm_env_unlock(void) {
/* enlarge the arrays for the already active threads */
static void tsrm_update_active_threads(void)
{/*{{{*/
int i;
for (i=0; i<tsrm_tls_table_size; i++) {
for (int i=0; i<tsrm_tls_table_size; i++) {
tsrm_tls_entry *p = tsrm_tls_table[i];
while (p) {
@ -370,8 +364,6 @@ TSRM_API ts_rsrc_id ts_allocate_fast_id(ts_rsrc_id *rsrc_id, size_t *offset, siz
static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_T thread_id)
{/*{{{*/
int i;
TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Creating data structures for thread %x", thread_id));
(*thread_resources_ptr) = (tsrm_tls_entry *) malloc(TSRM_ALIGNED_SIZE(sizeof(tsrm_tls_entry)) + tsrm_reserved_size);
(*thread_resources_ptr)->storage = NULL;
@ -389,7 +381,7 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
if (tsrm_new_thread_begin_handler) {
tsrm_new_thread_begin_handler(thread_id);
}
for (i=0; i<id_count; i++) {
for (int i=0; i<id_count; i++) {
if (resource_types_table[i].done) {
(*thread_resources_ptr)->storage[i] = NULL;
} else {
@ -479,7 +471,6 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
void ts_free_thread(void)
{/*{{{*/
tsrm_tls_entry *thread_resources;
int i;
THREAD_T thread_id = tsrm_thread_id();
int hash_value;
tsrm_tls_entry *last=NULL;
@ -492,12 +483,12 @@ void ts_free_thread(void)
while (thread_resources) {
if (thread_resources->thread_id == thread_id) {
for (i=0; i<thread_resources->count; i++) {
for (int i=0; i<thread_resources->count; i++) {
if (resource_types_table[i].dtor) {
resource_types_table[i].dtor(thread_resources->storage[i]);
}
}
for (i=0; i<thread_resources->count; i++) {
for (int i=0; i<thread_resources->count; i++) {
if (!resource_types_table[i].fast_offset) {
free(thread_resources->storage[i]);
}
@ -523,34 +514,33 @@ void ts_free_thread(void)
/* deallocates all occurrences of a given id */
void ts_free_id(ts_rsrc_id id)
{/*{{{*/
int i;
int j = TSRM_UNSHUFFLE_RSRC_ID(id);
int rsrc_id = TSRM_UNSHUFFLE_RSRC_ID(id);
tsrm_mutex_lock(tsmm_mutex);
TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Freeing resource id %d", id));
if (tsrm_tls_table) {
for (i=0; i<tsrm_tls_table_size; i++) {
for (int i=0; i<tsrm_tls_table_size; i++) {
tsrm_tls_entry *p = tsrm_tls_table[i];
while (p) {
if (p->count > j && p->storage[j]) {
if (p->count > rsrc_id && p->storage[rsrc_id]) {
if (resource_types_table) {
if (resource_types_table[j].dtor) {
resource_types_table[j].dtor(p->storage[j]);
if (resource_types_table[rsrc_id].dtor) {
resource_types_table[rsrc_id].dtor(p->storage[rsrc_id]);
}
if (!resource_types_table[j].fast_offset) {
free(p->storage[j]);
if (!resource_types_table[rsrc_id].fast_offset) {
free(p->storage[rsrc_id]);
}
}
p->storage[j] = NULL;
p->storage[rsrc_id] = NULL;
}
p = p->next;
}
}
}
resource_types_table[j].done = 1;
resource_types_table[rsrc_id].done = 1;
tsrm_mutex_unlock(tsmm_mutex);
@ -770,12 +760,12 @@ TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void)
#endif
}/*}}}*/
TSRM_API uint8_t tsrm_is_main_thread(void)
TSRM_API bool tsrm_is_main_thread(void)
{/*{{{*/
return in_main_thread;
}/*}}}*/
TSRM_API uint8_t tsrm_is_shutdown(void)
TSRM_API bool tsrm_is_shutdown(void)
{/*{{{*/
return is_thread_shutdown;
}/*}}}*/

View File

@ -21,6 +21,7 @@
#endif
#include "main/php_stdint.h"
#include <stdbool.h>
#ifdef TSRM_WIN32
# ifdef TSRM_EXPORTS
@ -79,7 +80,7 @@ extern "C" {
#endif
/* startup/shutdown */
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename);
TSRM_API bool tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename);
TSRM_API void tsrm_shutdown(void);
/* environ lock API */
@ -133,8 +134,8 @@ TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);
TSRM_API void *tsrm_get_ls_cache(void);
TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void);
TSRM_API uint8_t tsrm_is_main_thread(void);
TSRM_API uint8_t tsrm_is_shutdown(void);
TSRM_API bool tsrm_is_main_thread(void);
TSRM_API bool tsrm_is_shutdown(void);
TSRM_API const char *tsrm_api_name(void);
#ifdef TSRM_WIN32

View File

@ -358,7 +358,7 @@ static void php_binary_init(void)
binary_location = (char *)malloc(MAXPATHLEN);
if (binary_location && !strchr(sapi_module.executable_location, '/')) {
char *envpath, *path;
int found = 0;
bool found = false;
if ((envpath = getenv("PATH")) != NULL) {
char *search_dir, search_path[MAXPATHLEN];
@ -371,7 +371,7 @@ static void php_binary_init(void)
while (search_dir) {
snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location);
if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK) && VCWD_STAT(binary_location, &s) == 0 && S_ISREG(s.st_mode)) {
found = 1;
found = true;
break;
}
search_dir = php_strtok_r(NULL, ":", &last);
@ -768,26 +768,26 @@ PHP_INI_END()
/* True globals (no need for thread safety */
/* But don't make them a single int bitfield */
static int module_initialized = 0;
static int module_startup = 1;
static int module_shutdown = 0;
static bool module_initialized = false;
static bool module_startup = true;
static bool module_shutdown = false;
/* {{{ php_during_module_startup */
PHPAPI int php_during_module_startup(void)
PHPAPI bool php_during_module_startup(void)
{
return module_startup;
}
/* }}} */
/* {{{ php_during_module_shutdown */
PHPAPI int php_during_module_shutdown(void)
PHPAPI bool php_during_module_shutdown(void)
{
return module_shutdown;
}
/* }}} */
/* {{{ php_get_module_initialized */
PHPAPI int php_get_module_initialized(void)
PHPAPI bool php_get_module_initialized(void)
{
return module_initialized;
}
@ -1467,14 +1467,14 @@ PHP_FUNCTION(set_time_limit)
{
zend_long new_timeout;
char *new_timeout_str;
int new_timeout_strlen;
size_t new_timeout_strlen;
zend_string *key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &new_timeout) == FAILURE) {
RETURN_THROWS();
}
new_timeout_strlen = (int)zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
new_timeout_strlen = zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
key = zend_string_init("max_execution_time", sizeof("max_execution_time")-1, 0);
if (zend_alter_ini_entry_chars_ex(key, new_timeout_str, new_timeout_strlen, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == SUCCESS) {
@ -1694,9 +1694,9 @@ static void sigchld_handler(int apar)
#endif
/* {{{ php_request_startup */
int php_request_startup(void)
zend_result php_request_startup(void)
{
int retval = SUCCESS;
zend_result retval = SUCCESS;
zend_interned_strings_activate();
@ -1945,7 +1945,7 @@ PHP_MINFO_FUNCTION(php_core) { /* {{{ */
/* }}} */
/* {{{ php_register_extensions */
int php_register_extensions(zend_module_entry * const * ptr, int count)
zend_result php_register_extensions(zend_module_entry * const * ptr, int count)
{
zend_module_entry * const * end = ptr + count;
@ -2032,8 +2032,8 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
}
#endif
module_shutdown = 0;
module_startup = 1;
module_shutdown = false;
module_startup = true;
sapi_initialize_empty_request();
sapi_activate();
@ -2270,7 +2270,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
/* Extensions that add engine hooks after this point do so at their own peril */
zend_finalize_system_id();
module_initialized = 1;
module_initialized = true;
if (zend_post_startup() != SUCCESS) {
return FAILURE;
@ -2343,7 +2343,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
virtual_cwd_deactivate();
sapi_deactivate();
module_startup = 0;
module_startup = false;
/* Don't leak errors from startup into the per-request phase. */
clear_last_error();
@ -2376,7 +2376,7 @@ void php_module_shutdown(void)
{
int module_number=0;
module_shutdown = 1;
module_shutdown = true;
if (!module_initialized) {
return;
@ -2431,7 +2431,7 @@ void php_module_shutdown(void)
cb();
}
module_initialized = 0;
module_initialized = false;
#ifndef ZTS
core_globals_dtor(&core_globals);
@ -2451,7 +2451,7 @@ void php_module_shutdown(void)
/* }}} */
/* {{{ php_execute_script */
PHPAPI int php_execute_script(zend_file_handle *primary_file)
PHPAPI bool php_execute_script(zend_file_handle *primary_file)
{
zend_file_handle *prepend_file_p = NULL, *append_file_p = NULL;
zend_file_handle prepend_file, append_file;
@ -2461,7 +2461,7 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file)
char *old_cwd;
ALLOCA_FLAG(use_heap)
#endif
int retval = 0;
bool retval = false;
#ifndef HAVE_BROKEN_GETCWD
# define OLD_CWD_SIZE 4096
@ -2644,10 +2644,10 @@ PHPAPI int php_handle_auth_data(const char *auth)
/* }}} */
/* {{{ php_lint_script */
PHPAPI int php_lint_script(zend_file_handle *file)
PHPAPI zend_result php_lint_script(zend_file_handle *file)
{
zend_op_array *op_array;
int retval = FAILURE;
zend_result retval = FAILURE;
zend_try {
op_array = zend_compile_file(file, ZEND_INCLUDE);
@ -2688,9 +2688,9 @@ PHPAPI void php_reserve_tsrm_memory(void)
/* }}} */
/* {{{ php_tsrm_startup */
PHPAPI int php_tsrm_startup(void)
PHPAPI bool php_tsrm_startup(void)
{
int ret = tsrm_startup(1, 1, 0, NULL);
bool ret = tsrm_startup(1, 1, 0, NULL);
php_reserve_tsrm_memory();
(void)ts_resource(0);
return ret;

View File

@ -297,9 +297,9 @@ void phperror(char *error);
PHPAPI size_t php_write(void *buf, size_t size);
PHPAPI size_t php_printf(const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 1, 2);
PHPAPI size_t php_printf_unchecked(const char *format, ...);
PHPAPI int php_during_module_startup(void);
PHPAPI int php_during_module_shutdown(void);
PHPAPI int php_get_module_initialized(void);
PHPAPI bool php_during_module_startup(void);
PHPAPI bool php_during_module_shutdown(void);
PHPAPI bool php_get_module_initialized(void);
#ifdef HAVE_SYSLOG_H
#include "php_syslog.h"
#define php_log_err(msg) php_log_err_with_severity(msg, LOG_NOTICE)

View File

@ -23,24 +23,23 @@
#include "SAPI.h"
BEGIN_EXTERN_C()
PHPAPI int php_request_startup(void);
PHPAPI zend_result php_request_startup(void);
PHPAPI void php_request_shutdown(void *dummy);
PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module);
PHPAPI void php_module_shutdown(void);
PHPAPI int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals);
PHPAPI int php_register_extensions(zend_module_entry * const * ptr, int count);
PHPAPI zend_result php_register_extensions(zend_module_entry * const * ptr, int count);
PHPAPI int php_execute_script(zend_file_handle *primary_file);
PHPAPI bool php_execute_script(zend_file_handle *primary_file);
PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval *ret);
PHPAPI int php_handle_special_queries(void);
PHPAPI int php_lint_script(zend_file_handle *file);
PHPAPI zend_result php_lint_script(zend_file_handle *file);
PHPAPI void php_handle_aborted_connection(void);
PHPAPI int php_handle_auth_data(const char *auth);
PHPAPI void php_html_puts(const char *str, size_t siz);
PHPAPI int php_stream_open_for_zend_ex(zend_file_handle *handle, int mode);
PHPAPI zend_result php_stream_open_for_zend_ex(zend_file_handle *handle, int mode);
/* environment module */
extern int php_init_environ(void);
@ -48,7 +47,7 @@ extern int php_shutdown_environ(void);
#ifdef ZTS
PHPAPI void php_reserve_tsrm_memory(void);
PHPAPI int php_tsrm_startup(void);
PHPAPI bool php_tsrm_startup(void);
#endif
END_EXTERN_C()

View File

@ -2501,11 +2501,12 @@ parent_loop_end:
break;
case PHP_MODE_LINT:
PG(during_request_startup) = 0;
exit_status = php_lint_script(&file_handle);
if (exit_status == SUCCESS) {
if (php_lint_script(&file_handle) == SUCCESS) {
zend_printf("No syntax errors detected in %s\n", ZSTR_VAL(file_handle.filename));
exit_status = 0;
} else {
zend_printf("Errors parsing %s\n", ZSTR_VAL(file_handle.filename));
exit_status = -1;
}
break;
case PHP_MODE_STRIP:

View File

@ -966,11 +966,12 @@ do_repeat:
}
break;
case PHP_MODE_LINT:
EG(exit_status) = php_lint_script(&file_handle);
if (EG(exit_status) == SUCCESS) {
if (php_lint_script(&file_handle) == SUCCESS) {
zend_printf("No syntax errors detected in %s\n", php_self);
EG(exit_status) = 0;
} else {
zend_printf("Errors parsing %s\n", php_self);
EG(exit_status) = 255;
}
break;
case PHP_MODE_STRIP: