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, see <http://www.gnu.org/licenses/>.
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/macro.h>
40 pthread_mutex_t mutex;
45 pthread_mutex_t mutex;
53 pa_mutex* pa_mutex_new(bool recursive, bool inherit_priority) {
55 pthread_mutexattr_t attr;
56 #ifdef HAVE_PTHREAD_PRIO_INHERIT
60 pa_assert_se(pthread_mutexattr_init(&attr) == 0);
63 pa_assert_se(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
65 #ifdef HAVE_PTHREAD_PRIO_INHERIT
66 if (inherit_priority) {
67 r = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
68 pa_assert(r == 0 || r == ENOTSUP);
72 m = pa_xnew(pa_mutex, 1);
74 #ifndef HAVE_PTHREAD_PRIO_INHERIT
75 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
78 if ((r = pthread_mutex_init(&m->mutex, &attr))) {
80 /* If this failed, then this was probably due to non-available
81 * priority inheritance. In which case we fall back to normal
83 pa_assert(r == ENOTSUP && inherit_priority);
85 pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE) == 0);
86 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
98 void pa_mutex_free(pa_mutex *m) {
105 ret = pthread_mutex_destroy(&m->mutex);
107 pa_log_error("pthread_mutex_destroy [%p] error [%d], (%d/%d)", &m->mutex, ret, m->lock, m->unlock);
108 pa_assert_se(ret == 0);
110 pa_assert_se(pthread_mutex_destroy(&m->mutex) == 0);
115 void pa_mutex_lock(pa_mutex *m) {
122 ret = pthread_mutex_lock(&m->mutex);
124 pa_log_error("pthread_mutex_lock [%p] error [%d], (%d/%d)", &m->mutex, ret, m->lock, m->unlock);
125 pa_assert_se(ret == 0);
128 pa_assert_se(pthread_mutex_lock(&m->mutex) == 0);
132 bool pa_mutex_try_lock(pa_mutex *m) {
136 if ((r = pthread_mutex_trylock(&m->mutex)) != 0) {
137 pa_assert(r == EBUSY);
144 void pa_mutex_unlock(pa_mutex *m) {
149 if ((err = pthread_mutex_unlock(&m->mutex)) != 0) {
151 pa_log_error("pthread_mutex_unlock [%p] error [%d], (%d/%d)", &m->mutex, err, m->lock, m->unlock);
153 pa_log("pthread_mutex_unlock() failed: %s", pa_cstrerror(err));
155 pa_assert_not_reached();
162 pa_cond *pa_cond_new(void) {
165 c = pa_xnew(pa_cond, 1);
166 pa_assert_se(pthread_cond_init(&c->cond, NULL) == 0);
170 void pa_cond_free(pa_cond *c) {
173 pa_assert_se(pthread_cond_destroy(&c->cond) == 0);
177 void pa_cond_signal(pa_cond *c, int broadcast) {
181 pa_assert_se(pthread_cond_broadcast(&c->cond) == 0);
183 pa_assert_se(pthread_cond_signal(&c->cond) == 0);
186 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
190 return pthread_cond_wait(&c->cond, &m->mutex);
194 int pa_cond_timedwait(pa_cond *c, pa_mutex *m, int ms_to_wait) {
197 struct timeval wait_until;
198 struct timespec wait_until_ts;
203 gettimeofday(&now, NULL);
205 wait.tv_sec = ms_to_wait / 1000L;
206 wait.tv_usec = (ms_to_wait % 1000L) * 1000L;
208 timeradd(&now, &wait, &wait_until);
210 wait_until_ts.tv_sec = wait_until.tv_sec;
211 wait_until_ts.tv_nsec = wait_until.tv_usec * 1000L;
213 return pthread_cond_timedwait(&c->cond, &m->mutex, &wait_until_ts);
216 pa_mutex* pa_static_mutex_get(pa_static_mutex *s, bool recursive, bool inherit_priority) {
221 /* First, check if already initialized and short cut */
222 if ((m = pa_atomic_ptr_load(&s->ptr)))
225 /* OK, not initialized, so let's allocate, and fill in */
226 m = pa_mutex_new(recursive, inherit_priority);
227 if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
232 /* Him, filling in failed, so someone else must have filled in
234 pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));