default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / mutex-posix.c
blob634087d9ecebff2b7ea8b0fe234e25261b713613
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <pthread.h>
27 #include <errno.h>
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/macro.h>
32 #include "mutex.h"
34 struct pa_mutex {
35 pthread_mutex_t mutex;
38 struct pa_cond {
39 pthread_cond_t cond;
42 pa_mutex* pa_mutex_new(pa_bool_t recursive, pa_bool_t inherit_priority) {
43 pa_mutex *m;
44 pthread_mutexattr_t attr;
45 int r;
47 pa_assert_se(pthread_mutexattr_init(&attr) == 0);
49 if (recursive)
50 pa_assert_se(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
52 #ifdef HAVE_PTHREAD_PRIO_INHERIT
53 if (inherit_priority)
54 pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) == 0);
55 #endif
57 m = pa_xnew(pa_mutex, 1);
59 #ifndef HAVE_PTHREAD_PRIO_INHERIT
60 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
62 #else
63 if ((r = pthread_mutex_init(&m->mutex, &attr))) {
65 /* If this failed, then this was probably due to non-available
66 * priority inheritance. In which case we fall back to normal
67 * mutexes. */
68 pa_assert(r == ENOTSUP && inherit_priority);
70 pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE) == 0);
71 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
73 #endif
75 return m;
78 void pa_mutex_free(pa_mutex *m) {
79 pa_assert(m);
81 pa_assert_se(pthread_mutex_destroy(&m->mutex) == 0);
82 pa_xfree(m);
85 void pa_mutex_lock(pa_mutex *m) {
86 pa_assert(m);
88 pa_assert_se(pthread_mutex_lock(&m->mutex) == 0);
91 pa_bool_t pa_mutex_try_lock(pa_mutex *m) {
92 int r;
93 pa_assert(m);
95 if ((r = pthread_mutex_trylock(&m->mutex)) != 0) {
96 pa_assert(r == EBUSY);
97 return FALSE;
100 return TRUE;
103 void pa_mutex_unlock(pa_mutex *m) {
104 pa_assert(m);
106 pa_assert_se(pthread_mutex_unlock(&m->mutex) == 0);
109 pa_cond *pa_cond_new(void) {
110 pa_cond *c;
112 c = pa_xnew(pa_cond, 1);
113 pa_assert_se(pthread_cond_init(&c->cond, NULL) == 0);
114 return c;
117 void pa_cond_free(pa_cond *c) {
118 pa_assert(c);
120 pa_assert_se(pthread_cond_destroy(&c->cond) == 0);
121 pa_xfree(c);
124 void pa_cond_signal(pa_cond *c, int broadcast) {
125 pa_assert(c);
127 if (broadcast)
128 pa_assert_se(pthread_cond_broadcast(&c->cond) == 0);
129 else
130 pa_assert_se(pthread_cond_signal(&c->cond) == 0);
133 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
134 pa_assert(c);
135 pa_assert(m);
137 return pthread_cond_wait(&c->cond, &m->mutex);
140 pa_mutex* pa_static_mutex_get(pa_static_mutex *s, pa_bool_t recursive, pa_bool_t inherit_priority) {
141 pa_mutex *m;
143 pa_assert(s);
145 /* First, check if already initialized and short cut */
146 if ((m = pa_atomic_ptr_load(&s->ptr)))
147 return m;
149 /* OK, not initialized, so let's allocate, and fill in */
150 m = pa_mutex_new(recursive, inherit_priority);
151 if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
152 return m;
154 pa_mutex_free(m);
156 /* Him, filling in failed, so someone else must have filled in
157 * already */
158 pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));
159 return m;