unfortunately we cannot detect if a foreign thread is still running. Thus sucks....
[pulseaudio.git] / src / pulsecore / thread-posix.c
blobb3274426192847774cf8bb6541f4ac7768943818
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <assert.h>
30 #include <pthread.h>
31 #include <sched.h>
32 #include <errno.h>
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/mutex.h>
36 #include <pulsecore/once.h>
37 #include <pulsecore/atomic.h>
39 #include "thread.h"
41 #define ASSERT_SUCCESS(x) do { \
42 int _r = (x); \
43 assert(_r == 0); \
44 } while(0)
46 struct pa_thread {
47 pthread_t id;
48 pa_thread_func_t thread_func;
49 void *userdata;
50 pa_atomic_int_t running;
53 struct pa_tls {
54 pthread_key_t key;
57 static pa_tls *thread_tls;
58 static pa_once_t thread_tls_once = PA_ONCE_INIT;
60 static void tls_free_cb(void *p) {
61 pa_thread *t = p;
63 assert(t);
65 if (!t->thread_func)
66 /* This is a foreign thread, we need to free the struct */
67 pa_xfree(t);
70 static void thread_tls_once_func(void) {
71 thread_tls = pa_tls_new(tls_free_cb);
72 assert(thread_tls);
75 static void* internal_thread_func(void *userdata) {
76 pa_thread *t = userdata;
77 assert(t);
79 t->id = pthread_self();
81 pa_once(&thread_tls_once, thread_tls_once_func);
83 pa_tls_set(thread_tls, t);
85 pa_atomic_inc(&t->running);
86 t->thread_func(t->userdata);
87 pa_atomic_add(&t->running, -2);
89 return NULL;
92 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
93 pa_thread *t;
95 assert(thread_func);
97 t = pa_xnew(pa_thread, 1);
98 t->thread_func = thread_func;
99 t->userdata = userdata;
100 pa_atomic_store(&t->running, 0);
102 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
103 pa_xfree(t);
104 return NULL;
107 pa_atomic_inc(&t->running);
109 return t;
112 int pa_thread_is_running(pa_thread *t) {
113 assert(t);
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);
121 return pa_atomic_load(&t->running) > 0;
124 void pa_thread_free(pa_thread *t) {
125 assert(t);
127 pa_thread_join(t);
128 pa_xfree(t);
131 int pa_thread_join(pa_thread *t) {
132 assert(t);
134 return pthread_join(t->id, NULL);
137 pa_thread* pa_thread_self(void) {
138 pa_thread *t;
140 pa_once(&thread_tls_once, thread_tls_once_func);
142 if ((t = pa_tls_get(thread_tls)))
143 return t;
145 /* This is a foreign thread, let's create a pthread structure to
146 * make sure that we can always return a sensible pointer */
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);
154 pa_tls_set(thread_tls, t);
156 return t;
159 void* pa_thread_get_data(pa_thread *t) {
160 assert(t);
162 return t->userdata;
165 void pa_thread_set_data(pa_thread *t, void *userdata) {
166 assert(t);
168 t->userdata = userdata;
171 void pa_thread_yield(void) {
172 #ifdef HAVE_PTHREAD_YIELD
173 pthread_yield();
174 #else
175 ASSERT_SUCCESS(sched_yield());
176 #endif
179 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
180 pa_tls *t;
182 t = pa_xnew(pa_tls, 1);
184 if (pthread_key_create(&t->key, free_cb) < 0) {
185 pa_xfree(t);
186 return NULL;
189 return t;
192 void pa_tls_free(pa_tls *t) {
193 assert(t);
195 ASSERT_SUCCESS(pthread_key_delete(t->key));
196 pa_xfree(t);
199 void *pa_tls_get(pa_tls *t) {
200 assert(t);
202 return pthread_getspecific(t->key);
205 void *pa_tls_set(pa_tls *t, void *userdata) {
206 void *r;
208 r = pthread_getspecific(t->key);
209 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
210 return r;