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

So, per-thread init/deinit functions are registered to allocate/release them.
This commit is contained in:
Christopher Faulet 2017-04-21 16:47:03 +02:00 committed by Willy Tarreau
parent ba39f23a9d
commit 6adad11283
2 changed files with 14 additions and 7 deletions

View File

@ -26,6 +26,8 @@
#include <common/config.h> #include <common/config.h>
#include <common/standard.h> #include <common/standard.h>
#include <common/hathreads.h>
#include <types/freq_ctr.h> #include <types/freq_ctr.h>
#include <types/listener.h> #include <types/listener.h>
#include <types/proxy.h> #include <types/proxy.h>
@ -172,7 +174,7 @@ extern int relative_pid; /* process id starting at 1 */
extern int actconn; /* # of active sessions */ extern int actconn; /* # of active sessions */
extern int listeners; extern int listeners;
extern int jobs; /* # of active jobs (listeners, sessions, open devices) */ 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 int nb_oldpids; /* contains the number of old pids found */
extern const int zero; extern const int zero;
extern const int one; extern const int one;

View File

@ -22,20 +22,20 @@
#include <types/global.h> #include <types/global.h>
/* trash chunks used for various conversions */ /* trash chunks used for various conversions */
static struct chunk *trash_chunk; static THREAD_LOCAL struct chunk *trash_chunk;
static struct chunk trash_chunk1; static THREAD_LOCAL struct chunk trash_chunk1;
static struct chunk trash_chunk2; static THREAD_LOCAL struct chunk trash_chunk2;
/* trash buffers used for various conversions */ /* trash buffers used for various conversions */
static int trash_size; static int trash_size;
static char *trash_buf1; static THREAD_LOCAL char *trash_buf1;
static char *trash_buf2; static THREAD_LOCAL char *trash_buf2;
/* the trash pool for reentrant allocations */ /* the trash pool for reentrant allocations */
struct pool_head *pool2_trash = NULL; struct pool_head *pool2_trash = NULL;
/* this is used to drain data, and as a temporary buffer for sprintf()... */ /* 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 * Returns a pre-allocated and initialized trash chunk that can be used for any
@ -78,6 +78,11 @@ static int alloc_trash_buffers(int bufsize)
/* Initialize the trash buffers. It returns 0 if an error occurred. */ /* Initialize the trash buffers. It returns 0 if an error occurred. */
int init_trash_buffers() 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); chunk_init(&trash, my_realloc2(trash.str, global.tune.bufsize), global.tune.bufsize);
if (!trash.str || !alloc_trash_buffers(global.tune.bufsize)) if (!trash.str || !alloc_trash_buffers(global.tune.bufsize))
return 0; return 0;