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
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/mutex.h>
36 #include <pulsecore/once.h>
37 #include <pulsecore/atomic.h>
41 #define ASSERT_SUCCESS(x) do { \
48 pa_thread_func_t thread_func
;
50 pa_atomic_int_t running
;
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
) {
66 /* This is a foreign thread, we need to free the struct */
70 static void thread_tls_once_func(void) {
71 thread_tls
= pa_tls_new(tls_free_cb
);
75 static void* internal_thread_func(void *userdata
) {
76 pa_thread
*t
= userdata
;
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);
92 pa_thread
* pa_thread_new(pa_thread_func_t thread_func
, void *userdata
) {
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) {
107 pa_atomic_inc(&t
->running
);
112 int pa_thread_is_running(pa_thread
*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
119 assert(t
->thread_func
);
121 return pa_atomic_load(&t
->running
) > 0;
124 void pa_thread_free(pa_thread
*t
) {
131 int pa_thread_join(pa_thread
*t
) {
134 return pthread_join(t
->id
, NULL
);
137 pa_thread
* pa_thread_self(void) {
140 pa_once(&thread_tls_once
, thread_tls_once_func
);
142 if ((t
= pa_tls_get(thread_tls
)))
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
;
152 pa_atomic_store(&t
->running
, 2);
154 pa_tls_set(thread_tls
, t
);
159 void* pa_thread_get_data(pa_thread
*t
) {
165 void pa_thread_set_data(pa_thread
*t
, void *userdata
) {
168 t
->userdata
= userdata
;
171 void pa_thread_yield(void) {
172 #ifdef HAVE_PTHREAD_YIELD
175 ASSERT_SUCCESS(sched_yield());
179 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
182 t
= pa_xnew(pa_tls
, 1);
184 if (pthread_key_create(&t
->key
, free_cb
) < 0) {
192 void pa_tls_free(pa_tls
*t
) {
195 ASSERT_SUCCESS(pthread_key_delete(t
->key
));
199 void *pa_tls_get(pa_tls
*t
) {
202 return pthread_getspecific(t
->key
);
205 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
208 r
= pthread_getspecific(t
->key
);
209 ASSERT_SUCCESS(pthread_setspecific(t
->key
, userdata
));