Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions TSRM/TSRM.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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]);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}/*}}}*/
Expand Down
7 changes: 4 additions & 3 deletions TSRM/TSRM.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif

#include "main/php_stdint.h"
#include <stdbool.h>

#ifdef TSRM_WIN32
# ifdef TSRM_EXPORTS
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
50 changes: 25 additions & 25 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -2376,7 +2376,7 @@ void php_module_shutdown(void)
{
int module_number=0;

module_shutdown = 1;
module_shutdown = true;

if (!module_initialized) {
return;
Expand Down Expand Up @@ -2431,7 +2431,7 @@ void php_module_shutdown(void)
cb();
}

module_initialized = 0;
module_initialized = false;

#ifndef ZTS
core_globals_dtor(&core_globals);
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Loading