Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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 <assert.h>
29 #include <pthread.h>
30
31 #include <atomic_ops.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include "mutex.h"
36
37 #define ASSERT_SUCCESS(x) do { \
38     int _r = (x); \
39     assert(_r == 0); \
40 } while(0)
41
42 struct pa_mutex {
43     pthread_mutex_t mutex;
44 };
45
46 struct pa_cond {
47     pthread_cond_t cond;
48 };
49
50 pa_mutex* pa_mutex_new(int recursive) {
51     pa_mutex *m;
52     pthread_mutexattr_t attr;
53
54     pthread_mutexattr_init(&attr);
55
56     if (recursive)
57         ASSERT_SUCCESS(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
58
59     m = pa_xnew(pa_mutex, 1);
60
61     ASSERT_SUCCESS(pthread_mutex_init(&m->mutex, &attr));
62     return m;
63 }
64
65 void pa_mutex_free(pa_mutex *m) {
66     assert(m);
67
68     ASSERT_SUCCESS(pthread_mutex_destroy(&m->mutex));
69     pa_xfree(m);
70 }
71
72 void pa_mutex_lock(pa_mutex *m) {
73     assert(m);
74
75     ASSERT_SUCCESS(pthread_mutex_lock(&m->mutex));
76 }
77
78 void pa_mutex_unlock(pa_mutex *m) {
79     assert(m);
80
81     ASSERT_SUCCESS(pthread_mutex_unlock(&m->mutex));
82 }
83
84 pa_cond *pa_cond_new(void) {
85     pa_cond *c;
86
87     c = pa_xnew(pa_cond, 1);
88
89     ASSERT_SUCCESS(pthread_cond_init(&c->cond, NULL));
90     return c;
91 }
92
93 void pa_cond_free(pa_cond *c) {
94     assert(c);
95
96     ASSERT_SUCCESS(pthread_cond_destroy(&c->cond));
97     pa_xfree(c);
98 }
99
100 void pa_cond_signal(pa_cond *c, int broadcast) {
101     assert(c);
102
103     if (broadcast)
104         ASSERT_SUCCESS(pthread_cond_broadcast(&c->cond));
105     else
106         ASSERT_SUCCESS(pthread_cond_signal(&c->cond));
107 }
108
109 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
110     assert(c);
111     assert(m);
112
113     return pthread_cond_wait(&c->cond, &m->mutex);
114 }