]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: threads: Add mechanism to register per-thread init/deinit functions
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 25 Jul 2017 14:52:58 +0000 (16:52 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 31 Oct 2017 12:58:29 +0000 (13:58 +0100)
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
src/haproxy.c

index 03a728234214ce827cb676c2eac6eeb7d0734901..205a49497b853e54fb58cbebecc38f4b7d772037 100644 (file)
@@ -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 */
 
 /*
index 2cba7a9e0b177b711183564797240ad4c578fc63..a4e62a7c278ffd9c9d6e12908b425020b4ddcb9c 100644 (file)
@@ -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");