From 578c94ba69c183ce9329c909cff415f6732885e6 Mon Sep 17 00:00:00 2001 From: Saagar Jha Date: Sun, 7 May 2023 04:18:30 -0700 Subject: [PATCH 1/2] Use named semaphores Unnamed semaphores are not supported on macOS. --- httpd.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/httpd.c b/httpd.c index fe93727..8350b53 100644 --- a/httpd.c +++ b/httpd.c @@ -299,7 +299,7 @@ void term_handler(int s) static pthread_mutex_t job_mutex; /* semaphre to trigger job processing */ -static sem_t job_sem; +static sem_t *job_sem; /* fifo of jobs */ xs_list *job_fifo = NULL; @@ -332,7 +332,7 @@ void job_post(const xs_val *job, int urgent) } /* ask for someone to attend it */ - sem_post(&job_sem); + sem_post(job_sem); } @@ -341,7 +341,7 @@ void job_wait(xs_val **job) { *job = NULL; - if (sem_wait(&job_sem) == 0) { + if (sem_wait(job_sem) == 0) { /* lock the mutex */ pthread_mutex_lock(&job_mutex); @@ -352,6 +352,10 @@ void job_wait(xs_val **job) /* unlock the mutex */ pthread_mutex_unlock(&job_mutex); } + + if (!*job) { + sem_close(job_sem); + } } @@ -503,7 +507,7 @@ void httpd(void) /* initialize the job control engine */ pthread_mutex_init(&job_mutex, NULL); - sem_init(&job_sem, 0, 0); + job_sem = sem_open("/job", O_CREAT, 0644, 0); job_fifo = xs_list_new(); /* initialize sleep control */ From e25960c5484392a6fd208ad3f63a483f5bbb695b Mon Sep 17 00:00:00 2001 From: Saagar Jha Date: Sun, 7 May 2023 04:19:37 -0700 Subject: [PATCH 2/2] Fix pointer-to-int cast Resolves "warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]" --- httpd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpd.c b/httpd.c index 8350b53..7a25b84 100644 --- a/httpd.c +++ b/httpd.c @@ -366,7 +366,7 @@ void job_wait(xs_val **job) static void *job_thread(void *arg) /* job thread */ { - int pid = (char *) arg - (char *) 0x0; + int pid = (int)(uintptr_t)arg; srv_debug(1, xs_fmt("job thread %d started", pid));