153a5a1b3Sopenharmony_ci/*** 253a5a1b3Sopenharmony_ci This file is part of PulseAudio. 353a5a1b3Sopenharmony_ci 453a5a1b3Sopenharmony_ci Copyright 2006 Lennart Poettering 553a5a1b3Sopenharmony_ci 653a5a1b3Sopenharmony_ci PulseAudio is free software; you can redistribute it and/or modify 753a5a1b3Sopenharmony_ci it under the terms of the GNU Lesser General Public License as published 853a5a1b3Sopenharmony_ci by the Free Software Foundation; either version 2.1 of the License, 953a5a1b3Sopenharmony_ci or (at your option) any later version. 1053a5a1b3Sopenharmony_ci 1153a5a1b3Sopenharmony_ci PulseAudio is distributed in the hope that it will be useful, but 1253a5a1b3Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 1353a5a1b3Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1453a5a1b3Sopenharmony_ci General Public License for more details. 1553a5a1b3Sopenharmony_ci 1653a5a1b3Sopenharmony_ci You should have received a copy of the GNU Lesser General Public License 1753a5a1b3Sopenharmony_ci along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 1853a5a1b3Sopenharmony_ci***/ 1953a5a1b3Sopenharmony_ci 2053a5a1b3Sopenharmony_ci#ifdef HAVE_CONFIG_H 2153a5a1b3Sopenharmony_ci#include <config.h> 2253a5a1b3Sopenharmony_ci#endif 2353a5a1b3Sopenharmony_ci 2453a5a1b3Sopenharmony_ci#include <errno.h> 2553a5a1b3Sopenharmony_ci#include <pthread.h> 2653a5a1b3Sopenharmony_ci#include <semaphore.h> 2753a5a1b3Sopenharmony_ci 2853a5a1b3Sopenharmony_ci#include <pulse/xmalloc.h> 2953a5a1b3Sopenharmony_ci#include <pulsecore/macro.h> 3053a5a1b3Sopenharmony_ci 3153a5a1b3Sopenharmony_ci#include "semaphore.h" 3253a5a1b3Sopenharmony_ci 3353a5a1b3Sopenharmony_cistruct pa_semaphore { 3453a5a1b3Sopenharmony_ci sem_t sem; 3553a5a1b3Sopenharmony_ci}; 3653a5a1b3Sopenharmony_ci 3753a5a1b3Sopenharmony_cipa_semaphore* pa_semaphore_new(unsigned value) { 3853a5a1b3Sopenharmony_ci pa_semaphore *s; 3953a5a1b3Sopenharmony_ci 4053a5a1b3Sopenharmony_ci s = pa_xnew(pa_semaphore, 1); 4153a5a1b3Sopenharmony_ci pa_assert_se(sem_init(&s->sem, 0, value) == 0); 4253a5a1b3Sopenharmony_ci return s; 4353a5a1b3Sopenharmony_ci} 4453a5a1b3Sopenharmony_ci 4553a5a1b3Sopenharmony_civoid pa_semaphore_free(pa_semaphore *s) { 4653a5a1b3Sopenharmony_ci pa_assert(s); 4753a5a1b3Sopenharmony_ci pa_assert_se(sem_destroy(&s->sem) == 0); 4853a5a1b3Sopenharmony_ci pa_xfree(s); 4953a5a1b3Sopenharmony_ci} 5053a5a1b3Sopenharmony_ci 5153a5a1b3Sopenharmony_civoid pa_semaphore_post(pa_semaphore *s) { 5253a5a1b3Sopenharmony_ci pa_assert(s); 5353a5a1b3Sopenharmony_ci pa_assert_se(sem_post(&s->sem) == 0); 5453a5a1b3Sopenharmony_ci} 5553a5a1b3Sopenharmony_ci 5653a5a1b3Sopenharmony_civoid pa_semaphore_wait(pa_semaphore *s) { 5753a5a1b3Sopenharmony_ci int ret; 5853a5a1b3Sopenharmony_ci pa_assert(s); 5953a5a1b3Sopenharmony_ci 6053a5a1b3Sopenharmony_ci do { 6153a5a1b3Sopenharmony_ci ret = sem_wait(&s->sem); 6253a5a1b3Sopenharmony_ci } while (ret < 0 && errno == EINTR); 6353a5a1b3Sopenharmony_ci 6453a5a1b3Sopenharmony_ci pa_assert(ret == 0); 6553a5a1b3Sopenharmony_ci} 6653a5a1b3Sopenharmony_ci 6753a5a1b3Sopenharmony_cipa_semaphore* pa_static_semaphore_get(pa_static_semaphore *s, unsigned value) { 6853a5a1b3Sopenharmony_ci pa_semaphore *m; 6953a5a1b3Sopenharmony_ci 7053a5a1b3Sopenharmony_ci pa_assert(s); 7153a5a1b3Sopenharmony_ci 7253a5a1b3Sopenharmony_ci /* First, check if already initialized and short cut */ 7353a5a1b3Sopenharmony_ci if ((m = pa_atomic_ptr_load(&s->ptr))) 7453a5a1b3Sopenharmony_ci return m; 7553a5a1b3Sopenharmony_ci 7653a5a1b3Sopenharmony_ci /* OK, not initialized, so let's allocate, and fill in */ 7753a5a1b3Sopenharmony_ci m = pa_semaphore_new(value); 7853a5a1b3Sopenharmony_ci if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m))) 7953a5a1b3Sopenharmony_ci return m; 8053a5a1b3Sopenharmony_ci 8153a5a1b3Sopenharmony_ci pa_semaphore_free(m); 8253a5a1b3Sopenharmony_ci 8353a5a1b3Sopenharmony_ci /* Him, filling in failed, so someone else must have filled in 8453a5a1b3Sopenharmony_ci * already */ 8553a5a1b3Sopenharmony_ci pa_assert_se(m = pa_atomic_ptr_load(&s->ptr)); 8653a5a1b3Sopenharmony_ci return m; 8753a5a1b3Sopenharmony_ci} 88