Skip to content

Commit

Permalink
mxshadowsrv: Prefork threads
Browse files Browse the repository at this point in the history
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
  • Loading branch information
donald committed May 20, 2021
1 parent 89843f1 commit 55d9b15
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions mxshadowsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<THREADS; i++) {
int status = pthread_create(&thread[i], NULL, client_thread, NULL);
if (status != 0) { errno = status; perror("pthread_create"); exit(1); }
socket = -1;
status = pthread_detach(thread);
if (status != 0) { errno = status; perror("pthread_detach"); exit(1); }
}
for (int i=0; i<THREADS; i++) {
int status = pthread_join(thread[i], NULL);
if (status != 0) { errno = status; perror("pthread_join"); exit(1); }
}
}

0 comments on commit 55d9b15

Please sign in to comment.