Skip to content

Commit 5ba6ecd

Browse files
authored
Minor refactoring of main/main.c and TSRM (#8608)
1 parent 9f06bb3 commit 5ba6ecd

File tree

7 files changed

+69
-77
lines changed

7 files changed

+69
-77
lines changed

TSRM/TSRM.c

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ static pthread_key_t tls_key;
112112
# define tsrm_tls_get() pthread_getspecific(tls_key)
113113
#endif
114114

115-
TSRM_TLS uint8_t in_main_thread = 0;
116-
TSRM_TLS uint8_t is_thread_shutdown = 0;
115+
TSRM_TLS bool in_main_thread = false;
116+
TSRM_TLS bool is_thread_shutdown = false;
117117

118118
/* Startup TSRM (call once for the entire process) */
119-
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename)
119+
TSRM_API bool tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename)
120120
{/*{{{*/
121121
#ifdef TSRM_WIN32
122122
tls_key = TlsAlloc();
@@ -125,8 +125,8 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
125125
#endif
126126

127127
/* ensure singleton */
128-
in_main_thread = 1;
129-
is_thread_shutdown = 0;
128+
in_main_thread = true;
129+
is_thread_shutdown = false;
130130

131131
tsrm_error_file = stderr;
132132
tsrm_error_set(debug_level, debug_filename);
@@ -135,7 +135,7 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
135135
tsrm_tls_table = (tsrm_tls_entry **) calloc(tsrm_tls_table_size, sizeof(tsrm_tls_entry *));
136136
if (!tsrm_tls_table) {
137137
TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate TLS table"));
138-
is_thread_shutdown = 1;
138+
is_thread_shutdown = true;
139139
return 0;
140140
}
141141
id_count=0;
@@ -144,7 +144,7 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
144144
resource_types_table = (tsrm_resource_type *) calloc(resource_types_table_size, sizeof(tsrm_resource_type));
145145
if (!resource_types_table) {
146146
TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate resource types table"));
147-
is_thread_shutdown = 1;
147+
is_thread_shutdown = true;
148148
free(tsrm_tls_table);
149149
return 0;
150150
}
@@ -165,28 +165,24 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
165165
/* Shutdown TSRM (call once for the entire process) */
166166
TSRM_API void tsrm_shutdown(void)
167167
{/*{{{*/
168-
int i;
169-
170168
if (is_thread_shutdown) {
171169
/* shutdown must only occur once */
172170
return;
173171
}
174172

175-
is_thread_shutdown = 1;
173+
is_thread_shutdown = true;
176174

177175
if (!in_main_thread) {
178176
/* only the main thread may shutdown tsrm */
179177
return;
180178
}
181179

182-
for (i=0; i<tsrm_tls_table_size; i++) {
180+
for (int i=0; i<tsrm_tls_table_size; i++) {
183181
tsrm_tls_entry *p = tsrm_tls_table[i], *next_p;
184182

185183
while (p) {
186-
int j;
187-
188184
next_p = p->next;
189-
for (j=0; j<p->count; j++) {
185+
for (int j=0; j<p->count; j++) {
190186
if (p->storage[j]) {
191187
if (resource_types_table) {
192188
if (!resource_types_table[j].done) {
@@ -244,9 +240,7 @@ TSRM_API void tsrm_env_unlock(void) {
244240
/* enlarge the arrays for the already active threads */
245241
static void tsrm_update_active_threads(void)
246242
{/*{{{*/
247-
int i;
248-
249-
for (i=0; i<tsrm_tls_table_size; i++) {
243+
for (int i=0; i<tsrm_tls_table_size; i++) {
250244
tsrm_tls_entry *p = tsrm_tls_table[i];
251245

252246
while (p) {
@@ -370,8 +364,6 @@ TSRM_API ts_rsrc_id ts_allocate_fast_id(ts_rsrc_id *rsrc_id, size_t *offset, siz
370364

371365
static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_T thread_id)
372366
{/*{{{*/
373-
int i;
374-
375367
TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Creating data structures for thread %x", thread_id));
376368
(*thread_resources_ptr) = (tsrm_tls_entry *) malloc(TSRM_ALIGNED_SIZE(sizeof(tsrm_tls_entry)) + tsrm_reserved_size);
377369
(*thread_resources_ptr)->storage = NULL;
@@ -389,7 +381,7 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
389381
if (tsrm_new_thread_begin_handler) {
390382
tsrm_new_thread_begin_handler(thread_id);
391383
}
392-
for (i=0; i<id_count; i++) {
384+
for (int i=0; i<id_count; i++) {
393385
if (resource_types_table[i].done) {
394386
(*thread_resources_ptr)->storage[i] = NULL;
395387
} else {
@@ -479,7 +471,6 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
479471
void ts_free_thread(void)
480472
{/*{{{*/
481473
tsrm_tls_entry *thread_resources;
482-
int i;
483474
THREAD_T thread_id = tsrm_thread_id();
484475
int hash_value;
485476
tsrm_tls_entry *last=NULL;
@@ -492,12 +483,12 @@ void ts_free_thread(void)
492483

493484
while (thread_resources) {
494485
if (thread_resources->thread_id == thread_id) {
495-
for (i=0; i<thread_resources->count; i++) {
486+
for (int i=0; i<thread_resources->count; i++) {
496487
if (resource_types_table[i].dtor) {
497488
resource_types_table[i].dtor(thread_resources->storage[i]);
498489
}
499490
}
500-
for (i=0; i<thread_resources->count; i++) {
491+
for (int i=0; i<thread_resources->count; i++) {
501492
if (!resource_types_table[i].fast_offset) {
502493
free(thread_resources->storage[i]);
503494
}
@@ -523,34 +514,33 @@ void ts_free_thread(void)
523514
/* deallocates all occurrences of a given id */
524515
void ts_free_id(ts_rsrc_id id)
525516
{/*{{{*/
526-
int i;
527-
int j = TSRM_UNSHUFFLE_RSRC_ID(id);
517+
int rsrc_id = TSRM_UNSHUFFLE_RSRC_ID(id);
528518

529519
tsrm_mutex_lock(tsmm_mutex);
530520

531521
TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Freeing resource id %d", id));
532522

533523
if (tsrm_tls_table) {
534-
for (i=0; i<tsrm_tls_table_size; i++) {
524+
for (int i=0; i<tsrm_tls_table_size; i++) {
535525
tsrm_tls_entry *p = tsrm_tls_table[i];
536526

537527
while (p) {
538-
if (p->count > j && p->storage[j]) {
528+
if (p->count > rsrc_id && p->storage[rsrc_id]) {
539529
if (resource_types_table) {
540-
if (resource_types_table[j].dtor) {
541-
resource_types_table[j].dtor(p->storage[j]);
530+
if (resource_types_table[rsrc_id].dtor) {
531+
resource_types_table[rsrc_id].dtor(p->storage[rsrc_id]);
542532
}
543-
if (!resource_types_table[j].fast_offset) {
544-
free(p->storage[j]);
533+
if (!resource_types_table[rsrc_id].fast_offset) {
534+
free(p->storage[rsrc_id]);
545535
}
546536
}
547-
p->storage[j] = NULL;
537+
p->storage[rsrc_id] = NULL;
548538
}
549539
p = p->next;
550540
}
551541
}
552542
}
553-
resource_types_table[j].done = 1;
543+
resource_types_table[rsrc_id].done = 1;
554544

555545
tsrm_mutex_unlock(tsmm_mutex);
556546

@@ -770,12 +760,12 @@ TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void)
770760
#endif
771761
}/*}}}*/
772762

773-
TSRM_API uint8_t tsrm_is_main_thread(void)
763+
TSRM_API bool tsrm_is_main_thread(void)
774764
{/*{{{*/
775765
return in_main_thread;
776766
}/*}}}*/
777767

778-
TSRM_API uint8_t tsrm_is_shutdown(void)
768+
TSRM_API bool tsrm_is_shutdown(void)
779769
{/*{{{*/
780770
return is_thread_shutdown;
781771
}/*}}}*/

TSRM/TSRM.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#endif
2222

2323
#include "main/php_stdint.h"
24+
#include <stdbool.h>
2425

2526
#ifdef TSRM_WIN32
2627
# ifdef TSRM_EXPORTS
@@ -79,7 +80,7 @@ extern "C" {
7980
#endif
8081

8182
/* startup/shutdown */
82-
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename);
83+
TSRM_API bool tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename);
8384
TSRM_API void tsrm_shutdown(void);
8485

8586
/* environ lock API */
@@ -133,8 +134,8 @@ TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);
133134

134135
TSRM_API void *tsrm_get_ls_cache(void);
135136
TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void);
136-
TSRM_API uint8_t tsrm_is_main_thread(void);
137-
TSRM_API uint8_t tsrm_is_shutdown(void);
137+
TSRM_API bool tsrm_is_main_thread(void);
138+
TSRM_API bool tsrm_is_shutdown(void);
138139
TSRM_API const char *tsrm_api_name(void);
139140

140141
#ifdef TSRM_WIN32

main/main.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static void php_binary_init(void)
358358
binary_location = (char *)malloc(MAXPATHLEN);
359359
if (binary_location && !strchr(sapi_module.executable_location, '/')) {
360360
char *envpath, *path;
361-
int found = 0;
361+
bool found = false;
362362

363363
if ((envpath = getenv("PATH")) != NULL) {
364364
char *search_dir, search_path[MAXPATHLEN];
@@ -371,7 +371,7 @@ static void php_binary_init(void)
371371
while (search_dir) {
372372
snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location);
373373
if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK) && VCWD_STAT(binary_location, &s) == 0 && S_ISREG(s.st_mode)) {
374-
found = 1;
374+
found = true;
375375
break;
376376
}
377377
search_dir = php_strtok_r(NULL, ":", &last);
@@ -768,26 +768,26 @@ PHP_INI_END()
768768

769769
/* True globals (no need for thread safety */
770770
/* But don't make them a single int bitfield */
771-
static int module_initialized = 0;
772-
static int module_startup = 1;
773-
static int module_shutdown = 0;
771+
static bool module_initialized = false;
772+
static bool module_startup = true;
773+
static bool module_shutdown = false;
774774

775775
/* {{{ php_during_module_startup */
776-
PHPAPI int php_during_module_startup(void)
776+
PHPAPI bool php_during_module_startup(void)
777777
{
778778
return module_startup;
779779
}
780780
/* }}} */
781781

782782
/* {{{ php_during_module_shutdown */
783-
PHPAPI int php_during_module_shutdown(void)
783+
PHPAPI bool php_during_module_shutdown(void)
784784
{
785785
return module_shutdown;
786786
}
787787
/* }}} */
788788

789789
/* {{{ php_get_module_initialized */
790-
PHPAPI int php_get_module_initialized(void)
790+
PHPAPI bool php_get_module_initialized(void)
791791
{
792792
return module_initialized;
793793
}
@@ -1467,14 +1467,14 @@ PHP_FUNCTION(set_time_limit)
14671467
{
14681468
zend_long new_timeout;
14691469
char *new_timeout_str;
1470-
int new_timeout_strlen;
1470+
size_t new_timeout_strlen;
14711471
zend_string *key;
14721472

14731473
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &new_timeout) == FAILURE) {
14741474
RETURN_THROWS();
14751475
}
14761476

1477-
new_timeout_strlen = (int)zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
1477+
new_timeout_strlen = zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
14781478

14791479
key = zend_string_init("max_execution_time", sizeof("max_execution_time")-1, 0);
14801480
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)
16941694
#endif
16951695

16961696
/* {{{ php_request_startup */
1697-
int php_request_startup(void)
1697+
zend_result php_request_startup(void)
16981698
{
1699-
int retval = SUCCESS;
1699+
zend_result retval = SUCCESS;
17001700

17011701
zend_interned_strings_activate();
17021702

@@ -1945,7 +1945,7 @@ PHP_MINFO_FUNCTION(php_core) { /* {{{ */
19451945
/* }}} */
19461946

19471947
/* {{{ php_register_extensions */
1948-
int php_register_extensions(zend_module_entry * const * ptr, int count)
1948+
zend_result php_register_extensions(zend_module_entry * const * ptr, int count)
19491949
{
19501950
zend_module_entry * const * end = ptr + count;
19511951

@@ -2032,8 +2032,8 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
20322032
}
20332033
#endif
20342034

2035-
module_shutdown = 0;
2036-
module_startup = 1;
2035+
module_shutdown = false;
2036+
module_startup = true;
20372037
sapi_initialize_empty_request();
20382038
sapi_activate();
20392039

@@ -2270,7 +2270,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
22702270
/* Extensions that add engine hooks after this point do so at their own peril */
22712271
zend_finalize_system_id();
22722272

2273-
module_initialized = 1;
2273+
module_initialized = true;
22742274

22752275
if (zend_post_startup() != SUCCESS) {
22762276
return FAILURE;
@@ -2343,7 +2343,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
23432343
virtual_cwd_deactivate();
23442344

23452345
sapi_deactivate();
2346-
module_startup = 0;
2346+
module_startup = false;
23472347

23482348
/* Don't leak errors from startup into the per-request phase. */
23492349
clear_last_error();
@@ -2376,7 +2376,7 @@ void php_module_shutdown(void)
23762376
{
23772377
int module_number=0;
23782378

2379-
module_shutdown = 1;
2379+
module_shutdown = true;
23802380

23812381
if (!module_initialized) {
23822382
return;
@@ -2431,7 +2431,7 @@ void php_module_shutdown(void)
24312431
cb();
24322432
}
24332433

2434-
module_initialized = 0;
2434+
module_initialized = false;
24352435

24362436
#ifndef ZTS
24372437
core_globals_dtor(&core_globals);
@@ -2451,7 +2451,7 @@ void php_module_shutdown(void)
24512451
/* }}} */
24522452

24532453
/* {{{ php_execute_script */
2454-
PHPAPI int php_execute_script(zend_file_handle *primary_file)
2454+
PHPAPI bool php_execute_script(zend_file_handle *primary_file)
24552455
{
24562456
zend_file_handle *prepend_file_p = NULL, *append_file_p = NULL;
24572457
zend_file_handle prepend_file, append_file;
@@ -2461,7 +2461,7 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file)
24612461
char *old_cwd;
24622462
ALLOCA_FLAG(use_heap)
24632463
#endif
2464-
int retval = 0;
2464+
bool retval = false;
24652465

24662466
#ifndef HAVE_BROKEN_GETCWD
24672467
# define OLD_CWD_SIZE 4096
@@ -2644,10 +2644,10 @@ PHPAPI int php_handle_auth_data(const char *auth)
26442644
/* }}} */
26452645

26462646
/* {{{ php_lint_script */
2647-
PHPAPI int php_lint_script(zend_file_handle *file)
2647+
PHPAPI zend_result php_lint_script(zend_file_handle *file)
26482648
{
26492649
zend_op_array *op_array;
2650-
int retval = FAILURE;
2650+
zend_result retval = FAILURE;
26512651

26522652
zend_try {
26532653
op_array = zend_compile_file(file, ZEND_INCLUDE);
@@ -2688,9 +2688,9 @@ PHPAPI void php_reserve_tsrm_memory(void)
26882688
/* }}} */
26892689

26902690
/* {{{ php_tsrm_startup */
2691-
PHPAPI int php_tsrm_startup(void)
2691+
PHPAPI bool php_tsrm_startup(void)
26922692
{
2693-
int ret = tsrm_startup(1, 1, 0, NULL);
2693+
bool ret = tsrm_startup(1, 1, 0, NULL);
26942694
php_reserve_tsrm_memory();
26952695
(void)ts_resource(0);
26962696
return ret;

0 commit comments

Comments
 (0)