4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
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.
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.
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
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/core-error.h>
39 pthread_mutex_t mutex
;
46 pa_mutex
* pa_mutex_new(pa_bool_t recursive
, pa_bool_t inherit_priority
) {
48 pthread_mutexattr_t attr
;
51 pa_assert_se(pthread_mutexattr_init(&attr
) == 0);
54 pa_assert_se(pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
) == 0);
56 #ifdef HAVE_PTHREAD_PRIO_INHERIT
58 pa_assert_se(pthread_mutexattr_setprotocol(&attr
, PTHREAD_PRIO_INHERIT
) == 0);
61 m
= pa_xnew(pa_mutex
, 1);
63 #ifndef HAVE_PTHREAD_PRIO_INHERIT
64 pa_assert_se(pthread_mutex_init(&m
->mutex
, &attr
) == 0);
67 if ((r
= pthread_mutex_init(&m
->mutex
, &attr
))) {
69 /* If this failed, then this was probably due to non-available
70 * priority inheritance. In which case we fall back to normal
72 pa_assert(r
== ENOTSUP
&& inherit_priority
);
74 pa_assert_se(pthread_mutexattr_setprotocol(&attr
, PTHREAD_PRIO_NONE
) == 0);
75 pa_assert_se(pthread_mutex_init(&m
->mutex
, &attr
) == 0);
82 void pa_mutex_free(pa_mutex
*m
) {
85 pa_assert_se(pthread_mutex_destroy(&m
->mutex
) == 0);
89 void pa_mutex_lock(pa_mutex
*m
) {
92 pa_assert_se(pthread_mutex_lock(&m
->mutex
) == 0);
95 pa_bool_t
pa_mutex_try_lock(pa_mutex
*m
) {
99 if ((r
= pthread_mutex_trylock(&m
->mutex
)) != 0) {
100 pa_assert(r
== EBUSY
);
107 void pa_mutex_unlock(pa_mutex
*m
) {
110 pa_assert_se(pthread_mutex_unlock(&m
->mutex
) == 0);
113 pa_cond
*pa_cond_new(void) {
116 c
= pa_xnew(pa_cond
, 1);
117 pa_assert_se(pthread_cond_init(&c
->cond
, NULL
) == 0);
121 void pa_cond_free(pa_cond
*c
) {
124 pa_assert_se(pthread_cond_destroy(&c
->cond
) == 0);
128 void pa_cond_signal(pa_cond
*c
, int broadcast
) {
132 pa_assert_se(pthread_cond_broadcast(&c
->cond
) == 0);
134 pa_assert_se(pthread_cond_signal(&c
->cond
) == 0);
137 int pa_cond_wait(pa_cond
*c
, pa_mutex
*m
) {
141 return pthread_cond_wait(&c
->cond
, &m
->mutex
);