commit 578c94b

Saagar Jha  ·  2023-05-07 11:18:30 +0000 UTC
parent 14950d6
Use named semaphores

Unnamed semaphores are not supported on macOS.
1 files changed,  +8, -4
+8, -4
 1@@ -299,7 +299,7 @@ void term_handler(int s)
 2 static pthread_mutex_t job_mutex;
 3 
 4 /* semaphre to trigger job processing */
 5-static sem_t job_sem;
 6+static sem_t *job_sem;
 7 
 8 /* fifo of jobs */
 9 xs_list *job_fifo = NULL;
10@@ -332,7 +332,7 @@ void job_post(const xs_val *job, int urgent)
11     }
12 
13     /* ask for someone to attend it */
14-    sem_post(&job_sem);
15+    sem_post(job_sem);
16 }
17 
18 
19@@ -341,7 +341,7 @@ void job_wait(xs_val **job)
20 {
21     *job = NULL;
22 
23-    if (sem_wait(&job_sem) == 0) {
24+    if (sem_wait(job_sem) == 0) {
25         /* lock the mutex */
26         pthread_mutex_lock(&job_mutex);
27 
28@@ -352,6 +352,10 @@ void job_wait(xs_val **job)
29         /* unlock the mutex */
30         pthread_mutex_unlock(&job_mutex);
31     }
32+
33+    if (!*job) {
34+        sem_close(job_sem);
35+    }
36 }
37 
38 
39@@ -503,7 +507,7 @@ void httpd(void)
40 
41     /* initialize the job control engine */
42     pthread_mutex_init(&job_mutex, NULL);
43-    sem_init(&job_sem, 0, 0);
44+    job_sem = sem_open("/job", O_CREAT, 0644, 0);
45     job_fifo = xs_list_new();
46 
47     /* initialize sleep control */