2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/mutex.h>
33 #include <pulsecore/once.h>
34 #include <pulsecore/atomic.h>
35 #include <pulsecore/macro.h>
41 pa_thread_func_t thread_func
;
50 static void thread_free_cb(void *p
) {
56 /* This is a foreign thread, we need to free the struct */
60 PA_STATIC_TLS_DECLARE(current_thread
, thread_free_cb
);
62 static void* internal_thread_func(void *userdata
) {
63 pa_thread
*t
= userdata
;
66 t
->id
= pthread_self();
68 PA_STATIC_TLS_SET(current_thread
, t
);
70 pa_atomic_inc(&t
->running
);
71 t
->thread_func(t
->userdata
);
72 pa_atomic_sub(&t
->running
, 2);
77 pa_thread
* pa_thread_new(pa_thread_func_t thread_func
, void *userdata
) {
80 pa_assert(thread_func
);
82 t
= pa_xnew(pa_thread
, 1);
83 t
->thread_func
= thread_func
;
84 t
->userdata
= userdata
;
85 pa_atomic_store(&t
->running
, 0);
87 if (pthread_create(&t
->id
, NULL
, internal_thread_func
, t
) < 0) {
92 pa_atomic_inc(&t
->running
);
97 int pa_thread_is_running(pa_thread
*t
) {
100 /* Unfortunately there is no way to tell whether a "foreign"
101 * thread is still running. See
102 * http://udrepper.livejournal.com/16844.html for more
104 pa_assert(t
->thread_func
);
106 return pa_atomic_load(&t
->running
) > 0;
109 void pa_thread_free(pa_thread
*t
) {
116 int pa_thread_join(pa_thread
*t
) {
119 return pthread_join(t
->id
, NULL
);
122 pa_thread
* pa_thread_self(void) {
125 if ((t
= PA_STATIC_TLS_GET(current_thread
)))
128 /* This is a foreign thread, let's create a pthread structure to
129 * make sure that we can always return a sensible pointer */
131 t
= pa_xnew(pa_thread
, 1);
132 t
->id
= pthread_self();
133 t
->thread_func
= NULL
;
135 pa_atomic_store(&t
->running
, 2);
137 PA_STATIC_TLS_SET(current_thread
, t
);
142 void* pa_thread_get_data(pa_thread
*t
) {
148 void pa_thread_set_data(pa_thread
*t
, void *userdata
) {
151 t
->userdata
= userdata
;
154 void pa_thread_yield(void) {
155 #ifdef HAVE_PTHREAD_YIELD
158 pa_assert_se(sched_yield() == 0);
162 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
165 t
= pa_xnew(pa_tls
, 1);
167 if (pthread_key_create(&t
->key
, free_cb
) < 0) {
175 void pa_tls_free(pa_tls
*t
) {
178 pa_assert_se(pthread_key_delete(t
->key
) == 0);
182 void *pa_tls_get(pa_tls
*t
) {
185 return pthread_getspecific(t
->key
);
188 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
191 r
= pthread_getspecific(t
->key
);
192 pa_assert_se(pthread_setspecific(t
->key
, userdata
) == 0);