commit 3093bd2

grunfink  ·  2023-05-07 15:55:16 +0000 UTC
parents 14950d6, e25960c
Merge pull request 'Use named semaphores' (#32) from saagarjha/snac2:master into master

Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/32

This fixes httpd on macOS. It's fine if you don't want to merge this into snac itself; I can ask MacPorts to carry the patch themselves.
1 files changed,  +9, -5
+9, -5
 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@@ -362,7 +366,7 @@ void job_wait(xs_val **job)
40 static void *job_thread(void *arg)
41 /* job thread */
42 {
43-    int pid = (char *) arg - (char *) 0x0;
44+    int pid = (int)(uintptr_t)arg;
45 
46     srv_debug(1, xs_fmt("job thread %d started", pid));
47 
48@@ -503,7 +507,7 @@ void httpd(void)
49 
50     /* initialize the job control engine */
51     pthread_mutex_init(&job_mutex, NULL);
52-    sem_init(&job_sem, 0, 0);
53+    job_sem = sem_open("/job", O_CREAT, 0644, 0);
54     job_fifo = xs_list_new();
55 
56     /* initialize sleep control */