]> code.delx.au - pulseaudio/blob - src/pulsecore/semaphore-osx.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / semaphore-osx.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5 Copyright 2013 Albert Zeyer
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <pthread.h>
30 #include <semaphore.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/atomic.h>
37 #include <pulsecore/core-util.h>
38
39 #include "semaphore.h"
40
41 /* OSX doesn't support unnamed semaphores (via sem_init).
42 * Thus, we use a counter to give them enumerated names. */
43 static pa_atomic_t id_counter = PA_ATOMIC_INIT(0);
44
45 struct pa_semaphore {
46 sem_t *sem;
47 int id;
48 };
49
50 static char *sem_name(char *fn, size_t l, int id) {
51 pa_snprintf(fn, l, "/pulse-sem-%u-%u", getpid(), id);
52 return fn;
53 }
54
55 pa_semaphore *pa_semaphore_new(unsigned value) {
56 pa_semaphore *s;
57 char fn[32];
58
59 s = pa_xnew(pa_semaphore, 1);
60 s->id = pa_atomic_inc(&id_counter);
61 sem_name(fn, sizeof(fn), s->id);
62 sem_unlink(fn); /* in case an old stale semaphore is left around */
63 pa_assert_se(s->sem = sem_open(fn, O_CREAT|O_EXCL, 0700, value));
64 pa_assert(s->sem != SEM_FAILED);
65 return s;
66 }
67
68 void pa_semaphore_free(pa_semaphore *s) {
69 char fn[32];
70
71 pa_assert(s);
72
73 pa_assert_se(sem_close(s->sem) == 0);
74 sem_name(fn, sizeof(fn), s->id);
75 pa_assert_se(sem_unlink(fn) == 0);
76 pa_xfree(s);
77 }
78
79 void pa_semaphore_post(pa_semaphore *s) {
80 pa_assert(s);
81 pa_assert_se(sem_post(s->sem) == 0);
82 }
83
84 void pa_semaphore_wait(pa_semaphore *s) {
85 int ret;
86
87 pa_assert(s);
88
89 do {
90 ret = sem_wait(s->sem);
91 } while (ret < 0 && errno == EINTR);
92
93 pa_assert(ret == 0);
94 }