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
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/mutex.h>
35 #include <pulsecore/once.h>
36 #include <pulsecore/atomic.h>
37 #include <pulsecore/macro.h>
43 pa_thread_func_t thread_func
;
52 static void thread_free_cb(void *p
) {
58 /* This is a foreign thread, we need to free the struct */
62 PA_STATIC_TLS_DECLARE(current_thread
, thread_free_cb
);
64 static void* internal_thread_func(void *userdata
) {
65 pa_thread
*t
= userdata
;
68 t
->id
= pthread_self();
70 PA_STATIC_TLS_SET(current_thread
, t
);
72 pa_atomic_inc(&t
->running
);
73 t
->thread_func(t
->userdata
);
74 pa_atomic_sub(&t
->running
, 2);
79 pa_thread
* pa_thread_new(pa_thread_func_t thread_func
, void *userdata
) {
82 pa_assert(thread_func
);
84 t
= pa_xnew(pa_thread
, 1);
85 t
->thread_func
= thread_func
;
86 t
->userdata
= userdata
;
87 pa_atomic_store(&t
->running
, 0);
89 if (pthread_create(&t
->id
, NULL
, internal_thread_func
, t
) < 0) {
94 pa_atomic_inc(&t
->running
);
99 int pa_thread_is_running(pa_thread
*t
) {
102 /* Unfortunately there is no way to tell whether a "foreign"
103 * thread is still running. See
104 * http://udrepper.livejournal.com/16844.html for more
106 pa_assert(t
->thread_func
);
108 return pa_atomic_load(&t
->running
) > 0;
111 void pa_thread_free(pa_thread
*t
) {
118 int pa_thread_join(pa_thread
*t
) {
121 return pthread_join(t
->id
, NULL
);
124 pa_thread
* pa_thread_self(void) {
127 if ((t
= PA_STATIC_TLS_GET(current_thread
)))
130 /* This is a foreign thread, let's create a pthread structure to
131 * make sure that we can always return a sensible pointer */
133 t
= pa_xnew(pa_thread
, 1);
134 t
->id
= pthread_self();
135 t
->thread_func
= NULL
;
137 pa_atomic_store(&t
->running
, 2);
139 PA_STATIC_TLS_SET(current_thread
, t
);
144 void* pa_thread_get_data(pa_thread
*t
) {
150 void pa_thread_set_data(pa_thread
*t
, void *userdata
) {
153 t
->userdata
= userdata
;
156 void pa_thread_yield(void) {
157 #ifdef HAVE_PTHREAD_YIELD
160 pa_assert_se(sched_yield() == 0);
164 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
167 t
= pa_xnew(pa_tls
, 1);
169 if (pthread_key_create(&t
->key
, free_cb
) < 0) {
177 void pa_tls_free(pa_tls
*t
) {
180 pa_assert_se(pthread_key_delete(t
->key
) == 0);
184 void *pa_tls_get(pa_tls
*t
) {
187 return pthread_getspecific(t
->key
);
190 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
193 r
= pthread_getspecific(t
->key
);
194 pa_assert_se(pthread_setspecific(t
->key
, userdata
) == 0);