]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: init/threads: make the global threads an array of structs
authorWilly Tarreau <w@1wt.eu>
Fri, 3 May 2019 08:16:39 +0000 (10:16 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 20 May 2019 09:37:57 +0000 (11:37 +0200)
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
src/debug.c
src/haproxy.c

index 091506f61fcac748f435f0691de9e08bea1113e2..db8829567c3a88a8c9bf77132fe57145c7f82057 100644 (file)
@@ -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
index 51ded9ed7ae7e468078e2768c75f78109059e6fe..fce23c389b87d8294e632184cb5afbc0cce464be 100644 (file)
@@ -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 */
index 661cbd1c115242d0ba26dc332f6446ea277d3c89..74226817232fa638bbf38426b81f2b131be502e5 100644 (file)
@@ -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();