2
This file is part of PulseAudio.
4
Copyright 2006 Lennart Poettering
5
Copyright 2013 Albert Zeyer
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.
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.
17
You should have received a copy of the GNU Lesser General Public License
18
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
28
#include <semaphore.h>
29
#include <sys/types.h>
32
#include <pulse/xmalloc.h>
33
#include <pulsecore/macro.h>
34
#include <pulsecore/atomic.h>
35
#include <pulsecore/core-util.h>
37
#include "semaphore.h"
39
/* OSX doesn't support unnamed semaphores (via sem_init).
40
* Thus, we use a counter to give them enumerated names. */
41
static pa_atomic_t id_counter = PA_ATOMIC_INIT(0);
48
static char *sem_name(char *fn, size_t l, int id) {
49
pa_snprintf(fn, l, "/pulse-sem-%u-%u", getpid(), id);
53
pa_semaphore *pa_semaphore_new(unsigned value) {
57
s = pa_xnew(pa_semaphore, 1);
58
s->id = pa_atomic_inc(&id_counter);
59
sem_name(fn, sizeof(fn), s->id);
60
sem_unlink(fn); /* in case an old stale semaphore is left around */
61
pa_assert_se(s->sem = sem_open(fn, O_CREAT|O_EXCL, 0700, value));
62
pa_assert(s->sem != SEM_FAILED);
66
void pa_semaphore_free(pa_semaphore *s) {
71
pa_assert_se(sem_close(s->sem) == 0);
72
sem_name(fn, sizeof(fn), s->id);
73
pa_assert_se(sem_unlink(fn) == 0);
77
void pa_semaphore_post(pa_semaphore *s) {
79
pa_assert_se(sem_post(s->sem) == 0);
82
void pa_semaphore_wait(pa_semaphore *s) {
88
ret = sem_wait(s->sem);
89
} while (ret < 0 && errno == EINTR);