]> code.delx.au - pulseaudio/commitdiff
semaphore-osx: posix implementation based on named semaphores
authorAlbert Zeyer <albert.zeyer@rwth-aachen.de>
Tue, 2 Apr 2013 11:10:39 +0000 (13:10 +0200)
committerTanu Kaskinen <tanuk@iki.fi>
Tue, 9 Apr 2013 09:09:46 +0000 (12:09 +0300)
BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=63014
src/pulsecore/semaphore-osx.c

index 42afd154ba5085f719ccc242a974d5e9266a37a8..835c4cfc526872e7672aa7b8eb34f233754ad54c 100644 (file)
@@ -1,7 +1,8 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2009 Kim Lester <kim@dfusion.com.au>
+  Copyright 2006 Lennart Poettering
+  Copyright 2013 Albert Zeyer
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
 #include <config.h>
 #endif
 
-#include <Multiprocessing.h>
+#include <stdio.h>
+#include <errno.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include <pulse/xmalloc.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/atomic.h>
+#include <pulsecore/core-util.h>
 
 #include "semaphore.h"
 
+/* OSX doesn't support unnamed semaphores (via sem_init).
+ * Thus, we use a counter to give them enumerated names. */
+static pa_atomic_t id_counter = PA_ATOMIC_INIT(0);
+
 struct pa_semaphore {
-    MPSemaphoreID sema;
+    sem_t *sem;
+    int id;
 };
 
-pa_semaphore* pa_semaphore_new(unsigned int value) {
-    /* NOTE: Can't assume boolean - ie value = 0,1, so use UINT_MAX (boolean more efficient ?) */
+static char *sem_name(char *fn, size_t l, int id) {
+    pa_snprintf(fn, l, "/pulse-sem-%u-%u", getpid(), id);
+    return fn;
+}
+
+pa_semaphore *pa_semaphore_new(unsigned value) {
     pa_semaphore *s;
+    char fn[32];
 
     s = pa_xnew(pa_semaphore, 1);
-    pa_assert_se(MPCreateSemaphore(UINT_MAX, value, &s->sema) == 0);
-
+    s->id = pa_atomic_inc(&id_counter);
+    sem_name(fn, sizeof(fn), s->id);
+    sem_unlink(fn); /* in case an old stale semaphore is left around */
+    pa_assert_se(s->sem = sem_open(fn, O_CREAT|O_EXCL, 0700, value));
+    pa_assert(s->sem != SEM_FAILED);
     return s;
 }
 
 void pa_semaphore_free(pa_semaphore *s) {
+    char fn[32];
+
     pa_assert(s);
-    pa_assert_se(MPDeleteSemaphore(s->sema) == 0);
+
+    pa_assert_se(sem_close(s->sem) == 0);
+    sem_name(fn, sizeof(fn), s->id);
+    pa_assert_se(sem_unlink(fn) == 0);
     pa_xfree(s);
 }
 
 void pa_semaphore_post(pa_semaphore *s) {
     pa_assert(s);
-    pa_assert_se(MPSignalSemaphore(s->sema) == 0);
+    pa_assert_se(sem_post(s->sem) == 0);
 }
 
 void pa_semaphore_wait(pa_semaphore *s) {
+    int ret;
+
     pa_assert(s);
-    /* should probably check return value (-ve is error), noErr is ok. */
-    pa_assert_se(MPWaitOnSemaphore(s->sema, kDurationForever) == 0);
+
+    do {
+        ret = sem_wait(s->sem);
+    } while (ret < 0 && errno == EINTR);
+
+    pa_assert(ret == 0);
 }