• 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 #include <pulsecore/macro.h>
29 
30 #include "mutex.h"
31 
32 struct pa_mutex {
33     pthread_mutex_t mutex;
34 };
35 
36 struct pa_cond {
37     pthread_cond_t cond;
38 };
39 
pa_mutex_new(bool recursive,bool inherit_priority)40 pa_mutex* pa_mutex_new(bool recursive, bool inherit_priority) {
41     pa_mutex *m;
42     pthread_mutexattr_t attr;
43 #ifdef HAVE_PTHREAD_PRIO_INHERIT
44     int r;
45 #endif
46 
47     pa_assert_se(pthread_mutexattr_init(&attr) == 0);
48 
49     if (recursive)
50         pa_assert_se(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
51 
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);
56     }
57 #endif
58 
59     m = pa_xnew(pa_mutex, 1);
60 
61 #ifndef HAVE_PTHREAD_PRIO_INHERIT
62     pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
63 
64 #else
65     if ((r = pthread_mutex_init(&m->mutex, &attr))) {
66 
67         /* If this failed, then this was probably due to non-available
68          * priority inheritance. In which case we fall back to normal
69          * mutexes. */
70         pa_assert(r == ENOTSUP && inherit_priority);
71 
72         pa_assert_se(pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE) == 0);
73         pa_assert_se(pthread_mutex_init(&m->mutex, &attr) == 0);
74     }
75 #endif
76 
77     return m;
78 }
79 
pa_mutex_free(pa_mutex * m)80 void pa_mutex_free(pa_mutex *m) {
81     pa_assert(m);
82 
83     pa_assert_se(pthread_mutex_destroy(&m->mutex) == 0);
84     pa_xfree(m);
85 }
86 
pa_mutex_lock(pa_mutex * m)87 void pa_mutex_lock(pa_mutex *m) {
88     pa_assert(m);
89 
90     pa_assert_se(pthread_mutex_lock(&m->mutex) == 0);
91 }
92 
pa_mutex_try_lock(pa_mutex * m)93 bool pa_mutex_try_lock(pa_mutex *m) {
94     int r;
95     pa_assert(m);
96 
97     if ((r = pthread_mutex_trylock(&m->mutex)) != 0) {
98         pa_assert(r == EBUSY);
99         return false;
100     }
101 
102     return true;
103 }
104 
pa_mutex_unlock(pa_mutex * m)105 void pa_mutex_unlock(pa_mutex *m) {
106     pa_assert(m);
107 
108     pa_assert_se(pthread_mutex_unlock(&m->mutex) == 0);
109 }
110 
pa_cond_new(void)111 pa_cond *pa_cond_new(void) {
112     pa_cond *c;
113 
114     c = pa_xnew(pa_cond, 1);
115     pa_assert_se(pthread_cond_init(&c->cond, NULL) == 0);
116     return c;
117 }
118 
pa_cond_free(pa_cond * c)119 void pa_cond_free(pa_cond *c) {
120     pa_assert(c);
121 
122     pa_assert_se(pthread_cond_destroy(&c->cond) == 0);
123     pa_xfree(c);
124 }
125 
pa_cond_signal(pa_cond * c,int broadcast)126 void pa_cond_signal(pa_cond *c, int broadcast) {
127     pa_assert(c);
128 
129     if (broadcast)
130         pa_assert_se(pthread_cond_broadcast(&c->cond) == 0);
131     else
132         pa_assert_se(pthread_cond_signal(&c->cond) == 0);
133 }
134 
pa_cond_wait(pa_cond * c,pa_mutex * m)135 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
136     pa_assert(c);
137     pa_assert(m);
138 
139     return pthread_cond_wait(&c->cond, &m->mutex);
140 }
141 
pa_static_mutex_get(pa_static_mutex * s,bool recursive,bool inherit_priority)142 pa_mutex* pa_static_mutex_get(pa_static_mutex *s, bool recursive, bool inherit_priority) {
143     pa_mutex *m;
144 
145     pa_assert(s);
146 
147     /* First, check if already initialized and short cut */
148     if ((m = pa_atomic_ptr_load(&s->ptr)))
149         return m;
150 
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)))
154         return m;
155 
156     pa_mutex_free(m);
157 
158     /* Him, filling in failed, so someone else must have filled in
159      * already */
160     pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));
161     return m;
162 }
163