From 55d9b1556e26dd2ecab887d3378a6ff2988a1976 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Thu, 20 May 2021 08:41:33 +0200 Subject: [PATCH] mxshadowsrv: Prefork threads Prefork threads to avoid overhead per client connect. - Rename MAX_THREADS to THREADS - Hoist accept loop into client threads - Do not detach threads, use join instead --- mxshadowsrv.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/mxshadowsrv.c b/mxshadowsrv.c index 5eb2ed1..a90bde6 100644 --- a/mxshadowsrv.c +++ b/mxshadowsrv.c @@ -62,14 +62,13 @@ static void unmap_shadow(char *shadow_buf, struct stat *statbuf) { if (sts == -1) { perror("munmap"); exit(1); } } -#define MAX_THREADS 8 +#define THREADS 8 #define TIMEOUT 30000 // client timeout in msec static pthread_mutex_t shadow_mutex = PTHREAD_MUTEX_INITIALIZER ; static char *shadow_buf = NULL; // protected by shadow_mutex static struct stat statbuf; // protected by shadow_mutex static SSL_CTX *ssl_ctx; -static sem_t free_worker; static int listen_socket; static char *filename; @@ -188,12 +187,13 @@ static void process_client(int socket) { } static void *client_thread(void *arg) { - int socket = *((int *)arg); - process_client(socket); - close(socket); - free(arg); - int status = sem_post(&free_worker); - if (status != 0) { errno = status; perror("sem_post"); exit(1); } + + while (1) { + int _cleanup_(free_fd) socket = accept4(listen_socket, NULL, NULL, SOCK_NONBLOCK); + if (socket == -1 ) { perror("accept"); exit(1); } + validate_shadow(&shadow_buf, filename, &statbuf); + process_client(socket); + } return NULL; } @@ -260,23 +260,13 @@ int main(int argc, char **argv) { status = listen(listen_socket, 40); if (status == -1) { perror("listen"); exit(1); } - status = sem_init(&free_worker, 0, MAX_THREADS); - if (status) { errno = status; perror("sem_init"); exit(1); } - - while (1) { - int _cleanup_(free_fd) socket = accept4(listen_socket, NULL, NULL, SOCK_NONBLOCK); - if (socket == -1 ) { perror("accept"); exit(1); } - status = sem_wait(&free_worker); - if (status == -1) { perror("sem_wait"); exit(1); } - validate_shadow(&shadow_buf, filename, &statbuf); - pthread_t thread; - int *arg = malloc(sizeof *arg); - if (arg == NULL) { perror("malloc"); exit(1); } - *arg = socket; - status = pthread_create(&thread, NULL, client_thread, arg); + pthread_t thread[THREADS]; + for (int i=0; i