MEDIUM: threads/chunks: Transform trash chunks in thread-local variables

So, per-thread init/deinit functions are registered to allocate/release them.
diff --git a/include/types/global.h b/include/types/global.h
index 1b2148b..817c09b 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -26,6 +26,8 @@
 
 #include <common/config.h>
 #include <common/standard.h>
+#include <common/hathreads.h>
+
 #include <types/freq_ctr.h>
 #include <types/listener.h>
 #include <types/proxy.h>
@@ -172,7 +174,7 @@
 extern int  actconn;            /* # of active sessions */
 extern int  listeners;
 extern int  jobs;               /* # of active jobs (listeners, sessions, open devices) */
-extern struct chunk trash;
+extern THREAD_LOCAL struct chunk trash;
 extern int nb_oldpids;          /* contains the number of old pids found */
 extern const int zero;
 extern const int one;
diff --git a/src/chunk.c b/src/chunk.c
index e99535a..1fdcebb 100644
--- a/src/chunk.c
+++ b/src/chunk.c
@@ -22,20 +22,20 @@
 #include <types/global.h>
 
 /* trash chunks used for various conversions */
-static struct chunk *trash_chunk;
-static struct chunk trash_chunk1;
-static struct chunk trash_chunk2;
+static THREAD_LOCAL struct chunk *trash_chunk;
+static THREAD_LOCAL struct chunk trash_chunk1;
+static THREAD_LOCAL struct chunk trash_chunk2;
 
 /* trash buffers used for various conversions */
 static int trash_size;
-static char *trash_buf1;
-static char *trash_buf2;
+static THREAD_LOCAL char *trash_buf1;
+static THREAD_LOCAL char *trash_buf2;
 
 /* the trash pool for reentrant allocations */
 struct pool_head *pool2_trash = NULL;
 
 /* this is used to drain data, and as a temporary buffer for sprintf()... */
-struct chunk trash = { .str = NULL };
+THREAD_LOCAL struct chunk trash = { .str = NULL };
 
 /*
 * Returns a pre-allocated and initialized trash chunk that can be used for any
@@ -78,6 +78,11 @@
 /* Initialize the trash buffers. It returns 0 if an error occurred. */
 int init_trash_buffers()
 {
+	if (global.nbthread > 1 && tid == (unsigned int)(-1)) {
+		hap_register_per_thread_init(init_trash_buffers);
+		hap_register_per_thread_deinit(deinit_trash_buffers);
+	}
+
 	chunk_init(&trash, my_realloc2(trash.str, global.tune.bufsize), global.tune.bufsize);
 	if (!trash.str || !alloc_trash_buffers(global.tune.bufsize))
 		return 0;