From 415f611ff48916cb46a8f29c7510667d3fd01a1a Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Tue, 25 Jul 2017 16:52:58 +0200 Subject: [PATCH] MINOR: threads: Add mechanism to register per-thread init/deinit functions hap_register_per_thread_init and hap_register_per_thread_deinit functions has been added to register functions to do, for each thread, respectively, some initialization and deinitialization. These functions are added in the global lists per_thread_init_list and per_thread_deinit_list. These functions are called only when HAProxy is started with more than 1 thread (global.nbthread > 1). --- include/types/global.h | 3 +++ src/haproxy.c | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/types/global.h b/include/types/global.h index 03a728234..205a49497 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -206,6 +206,9 @@ void hap_register_build_opts(const char *str, int must_free); void hap_register_post_check(int (*fct)()); void hap_register_post_deinit(void (*fct)()); +void hap_register_per_thread_init(int (*fct)()); +void hap_register_per_thread_deinit(void (*fct)()); + #endif /* _TYPES_GLOBAL_H */ /* diff --git a/src/haproxy.c b/src/haproxy.c index 2cba7a9e0..a4e62a7c2 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -244,6 +244,25 @@ struct post_deinit_fct { void (*fct)(); }; +/* These functions are called for each thread just after the thread creation + * and before running the scheduler. They should be used to do per-thread + * initializations. They must return 0 if an error occurred. */ +struct list per_thread_init_list = LIST_HEAD_INIT(per_thread_init_list); +struct per_thread_init_fct { + struct list list; + int (*fct)(); +}; + +/* These functions are called for each thread just after the scheduler loop and + * before exiting the thread. They don't return anything and, as for post-deinit + * functions, they work in best effort mode as their sole goal is to make + * valgrind mostly happy. */ +struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list); +struct per_thread_deinit_fct { + struct list list; + void (*fct)(); +}; + /*********************************************************************/ /* general purpose functions ***************************************/ /*********************************************************************/ @@ -295,6 +314,34 @@ void hap_register_post_deinit(void (*fct)()) LIST_ADDQ(&post_deinit_list, &b->list); } +/* used to register some initialization functions to call for each thread. */ +void hap_register_per_thread_init(int (*fct)()) +{ + struct per_thread_init_fct *b; + + b = calloc(1, sizeof(*b)); + if (!b) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + b->fct = fct; + LIST_ADDQ(&per_thread_init_list, &b->list); +} + +/* used to register some de-initialization functions to call for each thread. */ +void hap_register_per_thread_deinit(void (*fct)()) +{ + struct per_thread_deinit_fct *b; + + b = calloc(1, sizeof(*b)); + if (!b) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + b->fct = fct; + LIST_ADDQ(&per_thread_deinit_list, &b->list); +} + static void display_version() { printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");