Use named semaphores

Unnamed semaphores are not supported on macOS.
This commit is contained in:
Saagar Jha 2023-05-07 04:18:30 -07:00
parent 14950d6954
commit 578c94ba69

12
httpd.c
View file

@ -299,7 +299,7 @@ void term_handler(int s)
static pthread_mutex_t job_mutex; static pthread_mutex_t job_mutex;
/* semaphre to trigger job processing */ /* semaphre to trigger job processing */
static sem_t job_sem; static sem_t *job_sem;
/* fifo of jobs */ /* fifo of jobs */
xs_list *job_fifo = NULL; xs_list *job_fifo = NULL;
@ -332,7 +332,7 @@ void job_post(const xs_val *job, int urgent)
} }
/* ask for someone to attend it */ /* 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; *job = NULL;
if (sem_wait(&job_sem) == 0) { if (sem_wait(job_sem) == 0) {
/* lock the mutex */ /* lock the mutex */
pthread_mutex_lock(&job_mutex); pthread_mutex_lock(&job_mutex);
@ -352,6 +352,10 @@ void job_wait(xs_val **job)
/* unlock the mutex */ /* unlock the mutex */
pthread_mutex_unlock(&job_mutex); pthread_mutex_unlock(&job_mutex);
} }
if (!*job) {
sem_close(job_sem);
}
} }
@ -503,7 +507,7 @@ void httpd(void)
/* initialize the job control engine */ /* initialize the job control engine */
pthread_mutex_init(&job_mutex, NULL); 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(); job_fifo = xs_list_new();
/* initialize sleep control */ /* initialize sleep control */