From 522cfbc1ea86b1baf54856ba8908eb313a162cbe Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 3 May 2019 10:16:39 +0200 Subject: [PATCH] MINOR: init/threads: make the global threads an array of structs This way we'll be able to store more per-thread information than just the pthread pointer. The storage became an array of struct instead of an allocated array since it's very small (typically 512 bytes) and not worth the hassle of dealing with memory allocation on this. The array was also renamed thread_info to make its intended usage more explicit. --- include/types/global.h | 5 ++++- src/debug.c | 2 +- src/haproxy.c | 20 +++++--------------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/include/types/global.h b/include/types/global.h index 091506f61..db8829567 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -242,7 +242,10 @@ 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); + +extern struct thread_info { + __decl_hathreads(pthread_t pthread); +} thread_info[MAX_THREADS]; /* bit values to go with "warned" above */ #define WARN_BLOCK_DEPRECATED 0x00000001 diff --git a/src/debug.c b/src/debug.c index 51ded9ed7..fce23c389 100644 --- a/src/debug.c +++ b/src/debug.c @@ -184,7 +184,7 @@ void ha_thread_dump_all_to_trash() #ifdef USE_THREAD for (thr = 0; thr < global.nbthread; thr++) { if (thr != tid) - pthread_kill(threads[thr], DEBUGSIG); + pthread_kill(thread_info[thr].pthread, DEBUGSIG); } #endif /* dump ourselves last */ diff --git a/src/haproxy.c b/src/haproxy.c index 661cbd1c1..742268172 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -138,7 +138,7 @@ 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); +struct thread_info thread_info[MAX_THREADS] = { }; volatile unsigned long sleeping_thread_mask; /* Threads that are about to sleep in poll() */ /* global options */ @@ -3139,13 +3139,6 @@ int main(int argc, char **argv) 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); sigdelset(&blocked_sig, SIGPROF); @@ -3162,9 +3155,9 @@ int main(int argc, char **argv) threads_want_rdv_mask = all_threads_mask; /* Create nbthread-1 thread. The first thread is the current process */ - threads[0] = pthread_self(); + thread_info[0].pthread = pthread_self(); for (i = 1; i < global.nbthread; i++) - pthread_create(&threads[i], NULL, &run_thread_poll_loop, (void *)(long)i); + pthread_create(&thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i); #ifdef USE_CPU_AFFINITY /* Now the CPU affinity for all threads */ @@ -3188,7 +3181,7 @@ int main(int argc, char **argv) CPU_SET(j - 1, &cpuset); cpu_map &= ~(1UL << (j - 1)); } - pthread_setaffinity_np(threads[i], + pthread_setaffinity_np(thread_info[i].pthread, sizeof(cpuset), &cpuset); } } @@ -3202,10 +3195,7 @@ int main(int argc, char **argv) /* Wait the end of other threads */ for (i = 1; i < global.nbthread; i++) - pthread_join(threads[i], NULL); - - free(threads); - threads = NULL; + pthread_join(thread_info[i].pthread, NULL); #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) show_lock_stats();