2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/macro.h>
35 pthread_mutex_t mutex;
42 pa_mutex* pa_mutex_new(pa_bool_t recursive, pa_bool_t inherit_priority) {
44 pthread_mutexattr_t attr;
47 pa_assert_se(pthread_mutexattr_init(&attr) == 0);
50 pa_assert_se(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
52 #ifdef HAVE_PTHREAD_PRIO_INHERIT
53 if (inherit_priority) {
54 r = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
55 pa_assert(r == 0 || r == ENOTSUP);
59 m = pa_xnew(pa_mutex, 1);
61 #ifndef HAVE_PTHREAD_PRIO_INHERIT
62 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
65 if ((r = pthread_mutex_init(&m->mutex, &attr))) {
67 /* If this failed, then this was probably due to non-available
68 * priority inheritance. In which case we fall back to normal
70 pa_assert(r == ENOTSUP && inherit_priority);
72 pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE) == 0);
73 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
80 void pa_mutex_free(pa_mutex *m) {
83 pa_assert_se(pthread_mutex_destroy(&m->mutex) == 0);
87 void pa_mutex_lock(pa_mutex *m) {
90 pa_assert_se(pthread_mutex_lock(&m->mutex) == 0);
93 pa_bool_t pa_mutex_try_lock(pa_mutex *m) {
97 if ((r = pthread_mutex_trylock(&m->mutex)) != 0) {
98 pa_assert(r == EBUSY);
105 void pa_mutex_unlock(pa_mutex *m) {
108 pa_assert_se(pthread_mutex_unlock(&m->mutex) == 0);
111 pa_cond *pa_cond_new(void) {
114 c = pa_xnew(pa_cond, 1);
115 pa_assert_se(pthread_cond_init(&c->cond, NULL) == 0);
119 void pa_cond_free(pa_cond *c) {
122 pa_assert_se(pthread_cond_destroy(&c->cond) == 0);
126 void pa_cond_signal(pa_cond *c, int broadcast) {
130 pa_assert_se(pthread_cond_broadcast(&c->cond) == 0);
132 pa_assert_se(pthread_cond_signal(&c->cond) == 0);
135 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
139 return pthread_cond_wait(&c->cond, &m->mutex);
142 pa_mutex* pa_static_mutex_get(pa_static_mutex *s, pa_bool_t recursive, pa_bool_t inherit_priority) {
147 /* First, check if already initialized and short cut */
148 if ((m = pa_atomic_ptr_load(&s->ptr)))
151 /* OK, not initialized, so let's allocate, and fill in */
152 m = pa_mutex_new(recursive, inherit_priority);
153 if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
158 /* Him, filling in failed, so someone else must have filled in
160 pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));