4271fa42f06a84692d44a9f1a35097ea4ec5570e
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / thread-posix.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <pthread.h>
31 #include <sched.h>
32 #include <errno.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/mutex.h>
36 #include <pulsecore/once.h>
37 #include <pulsecore/atomic.h>
38
39 #include "thread.h"
40
41 #define ASSERT_SUCCESS(x) do { \
42     int _r = (x); \
43     assert(_r == 0); \
44 } while(0)
45
46 struct pa_thread {
47     pthread_t id;
48     pa_thread_func_t thread_func;
49     void *userdata;
50     pa_atomic_int_t running;
51 };
52
53 struct pa_tls {
54     pthread_key_t key;
55 };
56
57 static pa_tls *thread_tls;
58 static pa_once_t thread_tls_once = PA_ONCE_INIT;
59
60 static void tls_free_cb(void *p) {
61     pa_thread *t = p;
62
63     assert(t);
64
65     if (!t->thread_func)
66         /* This is a foreign thread, we need to free the struct */
67         pa_xfree(t);
68 }
69
70 static void thread_tls_once_func(void) {
71     thread_tls = pa_tls_new(tls_free_cb);
72     assert(thread_tls);
73 }
74
75 static void* internal_thread_func(void *userdata) {
76     pa_thread *t = userdata;
77     assert(t);
78
79     t->id = pthread_self();
80
81     pa_once(&thread_tls_once, thread_tls_once_func);
82
83     pa_tls_set(thread_tls, t);
84
85     pa_atomic_inc(&t->running);
86     t->thread_func(t->userdata);
87     pa_atomic_add(&t->running, -2);
88
89     return NULL;
90 }
91
92 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
93     pa_thread *t;
94
95     assert(thread_func);
96
97     t = pa_xnew(pa_thread, 1);
98     t->thread_func = thread_func;
99     t->userdata = userdata;
100     pa_atomic_store(&t->running, 0);
101
102     if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
103         pa_xfree(t);
104         return NULL;
105     }
106
107     pa_atomic_inc(&t->running);
108
109     return t;
110 }
111
112 int pa_thread_is_running(pa_thread *t) {
113     assert(t);
114
115     /* Unfortunately there is no way to tell whether a "foreign"
116      * thread is still running. See
117      * http://udrepper.livejournal.com/16844.html for more
118      * information */
119     assert(t->thread_func);
120
121     return pa_atomic_load(&t->running) > 0;
122 }
123
124 void pa_thread_free(pa_thread *t) {
125     assert(t);
126
127     pa_thread_join(t);
128     pa_xfree(t);
129 }
130
131 int pa_thread_join(pa_thread *t) {
132     assert(t);
133
134     return pthread_join(t->id, NULL);
135 }
136
137 pa_thread* pa_thread_self(void) {
138     pa_thread *t;
139
140     pa_once(&thread_tls_once, thread_tls_once_func);
141
142     if ((t = pa_tls_get(thread_tls)))
143         return t;
144
145     /* This is a foreign thread, let's create a pthread structure to
146      * make sure that we can always return a sensible pointer */
147
148     t = pa_xnew(pa_thread, 1);
149     t->id = pthread_self();
150     t->thread_func = NULL;
151     t->userdata = NULL;
152     pa_atomic_store(&t->running, 2);
153
154     pa_tls_set(thread_tls, t);
155
156     return t;
157 }
158
159 void* pa_thread_get_data(pa_thread *t) {
160     assert(t);
161
162     return t->userdata;
163 }
164
165 void pa_thread_set_data(pa_thread *t, void *userdata) {
166     assert(t);
167
168     t->userdata = userdata;
169 }
170
171 void pa_thread_yield(void) {
172 #ifdef HAVE_PTHREAD_YIELD
173     pthread_yield();
174 #else
175     ASSERT_SUCCESS(sched_yield());
176 #endif
177 }
178
179 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
180     pa_tls *t;
181
182     t = pa_xnew(pa_tls, 1);
183
184     if (pthread_key_create(&t->key, free_cb) < 0) {
185         pa_xfree(t);
186         return NULL;
187     }
188
189     return t;
190 }
191
192 void pa_tls_free(pa_tls *t) {
193     assert(t);
194
195     ASSERT_SUCCESS(pthread_key_delete(t->key));
196     pa_xfree(t);
197 }
198
199 void *pa_tls_get(pa_tls *t) {
200     assert(t);
201
202     return pthread_getspecific(t->key);
203 }
204
205 void *pa_tls_set(pa_tls *t, void *userdata) {
206     void *r;
207
208     r = pthread_getspecific(t->key);
209     ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
210     return r;
211 }
212