]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-posix.c
fix pa_thread_is_running() for foreign threads; fix a memory leak for foreign threads
[pulseaudio] / src / pulsecore / thread-posix.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <pthread.h>
28 #include <sched.h>
29 #include <errno.h>
30
31 #include <atomic_ops.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/mutex.h>
35
36 #include "thread.h"
37
38 #define ASSERT_SUCCESS(x) do { \
39 int _r = (x); \
40 assert(_r == 0); \
41 } while(0)
42
43 struct pa_thread {
44 pthread_t id;
45 pa_thread_func_t thread_func;
46 void *userdata;
47 AO_t running;
48 };
49
50 struct pa_tls {
51 pthread_key_t key;
52 };
53
54 static pa_tls *thread_tls;
55 static pthread_once_t thread_tls_once = PTHREAD_ONCE_INIT;
56
57 static pa_mutex *once_mutex;
58 static pthread_once_t thread_once_once = PTHREAD_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 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
82 pa_tls_set(thread_tls, t);
83
84 AO_fetch_and_add1_full(&t->running);
85 t->thread_func(t->userdata);
86 AO_fetch_and_add_full(&t->running, (AO_t) -2);
87
88 return NULL;
89 }
90
91 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
92 pa_thread *t;
93
94 assert(thread_func);
95
96 t = pa_xnew(pa_thread, 1);
97 t->thread_func = thread_func;
98 t->userdata = userdata;
99 AO_store_full(&t->running, 0);
100
101 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
102 pa_xfree(t);
103 return NULL;
104 }
105
106 AO_fetch_and_add1_full(&t->running);
107
108 return t;
109 }
110
111 int pa_thread_is_running(pa_thread *t) {
112 AO_t r;
113 assert(t);
114
115 if (!t->thread_func) {
116 /* Mhmm, this is a foreign thread, t->running is not
117 * necessarily valid. We misuse pthread_getschedparam() to
118 * check if the thread is valid. This might not be portable. */
119
120 int policy;
121 struct sched_param param;
122
123 return pthread_getschedparam(t->id, &policy, &param) >= 0 || errno != ESRCH;
124 }
125
126 r = AO_load_full(&t->running);
127 return r == 1 || r == 2;
128 }
129
130 void pa_thread_free(pa_thread *t) {
131 assert(t);
132
133 pa_thread_join(t);
134 pa_xfree(t);
135 }
136
137 int pa_thread_join(pa_thread *t) {
138 assert(t);
139
140 return pthread_join(t->id, NULL);
141 }
142
143 pa_thread* pa_thread_self(void) {
144 pa_thread *t;
145
146 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
147
148 if ((t = pa_tls_get(thread_tls)))
149 return t;
150
151 /* This is a foreign thread, let's create a pthread structure to
152 * make sure that we can always return a sensible pointer */
153
154 t = pa_xnew(pa_thread, 1);
155 t->id = pthread_self();
156 t->thread_func = NULL;
157 t->userdata = NULL;
158 AO_store_full(&t->running, 1);
159
160 pa_tls_set(thread_tls, t);
161
162 return t;
163 }
164
165 void pa_thread_yield(void) {
166 #ifdef HAVE_PTHREAD_YIELD
167 pthread_yield();
168 #else
169 ASSERT_SUCCESS(sched_yield());
170 #endif
171 }
172
173 static void thread_once_once_func(void) {
174 once_mutex = pa_mutex_new(0);
175 assert(once_mutex);
176 }
177
178 void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func) {
179 assert(control);
180 assert(once_func);
181
182 ASSERT_SUCCESS(pthread_once(&thread_once_once, thread_once_once_func));
183
184 pa_mutex_lock(once_mutex);
185
186 if (*control == PA_THREAD_ONCE_INIT) {
187 *control = ~PA_THREAD_ONCE_INIT;
188 pa_mutex_unlock(once_mutex);
189 once_func();
190 } else
191 pa_mutex_unlock(once_mutex);
192 }
193
194 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
195 pa_tls *t;
196
197 t = pa_xnew(pa_tls, 1);
198
199 if (pthread_key_create(&t->key, free_cb) < 0) {
200 pa_xfree(t);
201 return NULL;
202 }
203
204 return t;
205 }
206
207 void pa_tls_free(pa_tls *t) {
208 assert(t);
209
210 ASSERT_SUCCESS(pthread_key_delete(t->key));
211 pa_xfree(t);
212 }
213
214 void *pa_tls_get(pa_tls *t) {
215 assert(t);
216
217 return pthread_getspecific(t->key);
218 }
219
220 void *pa_tls_set(pa_tls *t, void *userdata) {
221 void *r;
222
223 r = pthread_getspecific(t->key);
224 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
225 return r;
226 }
227