]> code.delx.au - pulseaudio/blob - src/pulsecore/mutex-posix.c
If PTHREAD_PRIO_INHERIT mutexes are not available fall back to normal mutexes
[pulseaudio] / src / pulsecore / mutex-posix.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pthread.h>
29 #include <errno.h>
30
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/core-error.h>
35
36 #include "mutex.h"
37
38 struct pa_mutex {
39 pthread_mutex_t mutex;
40 };
41
42 struct pa_cond {
43 pthread_cond_t cond;
44 };
45
46 pa_mutex* pa_mutex_new(pa_bool_t recursive, pa_bool_t inherit_priority) {
47 pa_mutex *m;
48 pthread_mutexattr_t attr;
49 int r;
50
51 pa_assert_se(pthread_mutexattr_init(&attr) == 0);
52
53 if (recursive)
54 pa_assert_se(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
55
56 #ifdef HAVE_PTHREAD_PRIO_INHERIT
57 if (inherit_priority)
58 pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) == 0);
59 #endif
60
61 m = pa_xnew(pa_mutex, 1);
62
63 #ifndef HAVE_PTHREAD_PRIO_INHERIT
64 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
65
66 #else
67 if ((r = pthread_mutex_init(&m->mutex, &attr))) {
68
69 /* If this failed, then this was probably due to non-available
70 * priority inheritance. In which case we fall back to normal
71 * mutexes. */
72 pa_assert(r == ENOTSUP && inherit_priority);
73
74 pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE) == 0);
75 pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
76 }
77 #endif
78
79 return m;
80 }
81
82 void pa_mutex_free(pa_mutex *m) {
83 pa_assert(m);
84
85 pa_assert_se(pthread_mutex_destroy(&m->mutex) == 0);
86 pa_xfree(m);
87 }
88
89 void pa_mutex_lock(pa_mutex *m) {
90 pa_assert(m);
91
92 pa_assert_se(pthread_mutex_lock(&m->mutex) == 0);
93 }
94
95 void pa_mutex_unlock(pa_mutex *m) {
96 pa_assert(m);
97
98 pa_assert_se(pthread_mutex_unlock(&m->mutex) == 0);
99 }
100
101 pa_cond *pa_cond_new(void) {
102 pa_cond *c;
103
104 c = pa_xnew(pa_cond, 1);
105 pa_assert_se(pthread_cond_init(&c->cond, NULL) == 0);
106 return c;
107 }
108
109 void pa_cond_free(pa_cond *c) {
110 pa_assert(c);
111
112 pa_assert_se(pthread_cond_destroy(&c->cond) == 0);
113 pa_xfree(c);
114 }
115
116 void pa_cond_signal(pa_cond *c, int broadcast) {
117 pa_assert(c);
118
119 if (broadcast)
120 pa_assert_se(pthread_cond_broadcast(&c->cond) == 0);
121 else
122 pa_assert_se(pthread_cond_signal(&c->cond) == 0);
123 }
124
125 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
126 pa_assert(c);
127 pa_assert(m);
128
129 return pthread_cond_wait(&c->cond, &m->mutex);
130 }