From c40efc19194a7b21137f9d2b3a39e83003689fef Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 3 May 2019 09:22:44 +0200 Subject: [PATCH] MINOR: init/threads: make the threads array global Currently the thread array is a local variable inside a function block and there is no access to it from outside, which often complicates debugging. Let's make it global and export it. Also the allocation return is now checked. --- include/types/global.h | 1 + src/haproxy.c | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/types/global.h b/include/types/global.h index b2e79693b..2df0da9b4 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -242,6 +242,7 @@ extern int master; /* 1 if in master, 0 otherwise */ extern unsigned int rlim_fd_cur_at_boot; extern unsigned int rlim_fd_max_at_boot; extern int atexit_flag; +__decl_hathreads(extern pthread_t *threads); /* bit values to go with "warned" above */ #define WARN_BLOCK_DEPRECATED 0x00000001 diff --git a/src/haproxy.c b/src/haproxy.c index 43b2118e9..33f945362 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -139,6 +139,8 @@ int relative_pid = 1; /* process id starting at 1 */ unsigned long pid_bit = 1; /* bit corresponding to the process id */ unsigned long all_proc_mask = 1; /* mask of all processes */ +__decl_hathreads(pthread_t *threads = NULL); + volatile unsigned long sleeping_thread_mask; /* Threads that are about to sleep in poll() */ /* global options */ struct global global = { @@ -3125,9 +3127,15 @@ int main(int argc, char **argv) */ #ifdef USE_THREAD { - pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t)); - int i; sigset_t blocked_sig, old_sig; + int i; + + threads = calloc(global.nbthread, sizeof(*threads)); + if (!threads) { + ha_alert("Cannot allocate memory for threads.\n"); + protocol_unbind_all(); + exit(1); + } /* ensure the signals will be blocked in every thread */ sigfillset(&blocked_sig); @@ -3182,6 +3190,7 @@ int main(int argc, char **argv) pthread_join(threads[i], NULL); free(threads); + threads = NULL; #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) show_lock_stats();