]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: threads/buffers: Define and register per-thread init/deinit functions
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 29 Aug 2017 12:43:04 +0000 (14:43 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 31 Oct 2017 12:58:29 +0000 (13:58 +0100)
For now, only the swap_buffer is handled in these functions. Moreover,
swap_buffer has been changed to be a thread-local variable.

src/buffer.c

index 7a49ba63d4986d67f11b70544cc2963dc8436e86..83e4e9e36f5468fc1f137c13577dd104f5e2948d 100644 (file)
@@ -37,7 +37,20 @@ struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
 /* this buffer is always the same size as standard buffers and is used for
  * swapping data inside a buffer.
  */
-static char *swap_buffer = NULL;
+static THREAD_LOCAL char *swap_buffer = NULL;
+
+static int init_buffer_per_thread()
+{
+       swap_buffer = calloc(1, global.tune.bufsize);
+       if (swap_buffer == NULL)
+               return 0;
+       return 1;
+}
+
+static void deinit_buffer_per_thread()
+{
+       free(swap_buffer); swap_buffer = NULL;
+}
 
 /* perform minimal intializations, report 0 in case of error, 1 if OK. */
 int init_buffer()
@@ -65,16 +78,19 @@ int init_buffer()
 
        pool_free2(pool2_buffer, buffer);
 
-       swap_buffer = calloc(1, global.tune.bufsize);
-       if (swap_buffer == NULL)
-               return 0;
+       if (global.nbthread > 1) {
+               hap_register_per_thread_init(init_buffer_per_thread);
+               hap_register_per_thread_deinit(deinit_buffer_per_thread);
+       }
+       else if (!init_buffer_per_thread())
+                    return 0;
 
        return 1;
 }
 
 void deinit_buffer()
 {
-       free(swap_buffer); swap_buffer = NULL;
+       deinit_buffer_per_thread();
        pool_destroy2(pool2_buffer);
 }