• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   Copyright 2006 Lennart Poettering
5 
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.
10 
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.
15 
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/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <pthread.h>
25 #include <errno.h>
26 
27 #include <pulse/xmalloc.h>
28 
29 #include <pulsecore/core-error.h>
30 #include <pulsecore/macro.h>
31 
32 #include "mutex.h"
33 
34 #include "log/audio_log.h"
35 
36 struct pa_mutex {
37     pthread_mutex_t mutex;
38 };
39 
40 struct pa_cond {
41     pthread_cond_t cond;
42 };
43 
pa_mutex_new(bool recursive,bool inherit_priority)44 pa_mutex* pa_mutex_new(bool recursive, bool inherit_priority) {
45     pa_mutex *m;
46     pthread_mutexattr_t attr;
47 #ifdef HAVE_PTHREAD_PRIO_INHERIT
48     int r;
49 #endif
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         r = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
59         pa_assert(r == 0 || r == ENOTSUP);
60     }
61 #endif
62 
63     m = pa_xnew(pa_mutex, 1);
64 
65 #ifndef HAVE_PTHREAD_PRIO_INHERIT
66     pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
67 
68 #else
69     if ((r = pthread_mutex_init(&m->mutex, &attr))) {
70 
71         /* If this failed, then this was probably due to non-available
72          * priority inheritance. In which case we fall back to normal
73          * mutexes. */
74         pa_assert(r == ENOTSUP && inherit_priority);
75 
76         pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE) == 0);
77         pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
78     }
79 #endif
80 
81     return m;
82 }
83 
pa_mutex_free(pa_mutex * m)84 void pa_mutex_free(pa_mutex *m) {
85     pa_assert(m);
86 
87     pa_assert_se(pthread_mutex_destroy(&m->mutex) == 0);
88     pa_xfree(m);
89 }
90 
pa_mutex_lock(pa_mutex * m)91 void pa_mutex_lock(pa_mutex *m) {
92     pa_assert(m);
93 
94     pa_assert_se(pthread_mutex_lock(&m->mutex) == 0);
95 }
96 
pa_mutex_try_lock(pa_mutex * m)97 bool pa_mutex_try_lock(pa_mutex *m) {
98     int r;
99     pa_assert(m);
100 
101     if ((r = pthread_mutex_trylock(&m->mutex)) != 0) {
102         pa_assert(r == EBUSY);
103         return false;
104     }
105 
106     return true;
107 }
108 
pa_mutex_unlock(pa_mutex * m)109 void pa_mutex_unlock(pa_mutex *m) {
110     int err;
111 
112     pa_assert(m);
113 
114     if ((err = pthread_mutex_unlock(&m->mutex)) != 0) {
115         AUDIO_ERR_LOG("pthread_mutex_unlock() failed: %{public}s", pa_cstrerror(err));
116         pa_assert_not_reached();
117     }
118 }
119 
pa_cond_new(void)120 pa_cond *pa_cond_new(void) {
121     pa_cond *c;
122 
123     c = pa_xnew(pa_cond, 1);
124     pa_assert_se(pthread_cond_init(&c->cond, NULL) == 0);
125     return c;
126 }
127 
pa_cond_free(pa_cond * c)128 void pa_cond_free(pa_cond *c) {
129     pa_assert(c);
130 
131     pa_assert_se(pthread_cond_destroy(&c->cond) == 0);
132     pa_xfree(c);
133 }
134 
pa_cond_signal(pa_cond * c,int broadcast)135 void pa_cond_signal(pa_cond *c, int broadcast) {
136     pa_assert(c);
137 
138     if (broadcast)
139         pa_assert_se(pthread_cond_broadcast(&c->cond) == 0);
140     else
141         pa_assert_se(pthread_cond_signal(&c->cond) == 0);
142 }
143 
pa_cond_wait(pa_cond * c,pa_mutex * m)144 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
145     pa_assert(c);
146     pa_assert(m);
147 
148     return pthread_cond_wait(&c->cond, &m->mutex);
149 }
150 
pa_static_mutex_get(pa_static_mutex * s,bool recursive,bool inherit_priority)151 pa_mutex* pa_static_mutex_get(pa_static_mutex *s, bool recursive, bool inherit_priority) {
152     pa_mutex *m;
153 
154     pa_assert(s);
155 
156     /* First, check if already initialized and short cut */
157     if ((m = pa_atomic_ptr_load(&s->ptr)))
158         return m;
159 
160     /* OK, not initialized, so let's allocate, and fill in */
161     m = pa_mutex_new(recursive, inherit_priority);
162     if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
163         return m;
164 
165     pa_mutex_free(m);
166 
167     /* Him, filling in failed, so someone else must have filled in
168      * already */
169     pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));
170     return m;
171 }
172