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 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. */
121 struct sched_param param;
123 return pthread_getschedparam(t->id, &policy, ¶m) >= 0 || errno != ESRCH;
126 return pa_atomic_load(&t->running) > 0;
129 void pa_thread_free(pa_thread *t) {
136 int pa_thread_join(pa_thread *t) {
139 return pthread_join(t->id, NULL);
142 pa_thread* pa_thread_self(void) {
145 pa_once(&thread_tls_once, thread_tls_once_func);
147 if ((t = pa_tls_get(thread_tls)))
150 /* This is a foreign thread, let's create a pthread structure to
151 * make sure that we can always return a sensible pointer */
153 t = pa_xnew(pa_thread, 1);
154 t->id = pthread_self();
155 t->thread_func = NULL;
157 pa_atomic_store(&t->running, 2);
159 pa_tls_set(thread_tls, t);
164 void* pa_thread_get_data(pa_thread *t) {
170 void pa_thread_set_data(pa_thread *t, void *userdata) {
173 t->userdata = userdata;
176 void pa_thread_yield(void) {
177 #ifdef HAVE_PTHREAD_YIELD
180 ASSERT_SUCCESS(sched_yield());
184 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
187 t = pa_xnew(pa_tls, 1);
189 if (pthread_key_create(&t->key, free_cb) < 0) {
197 void pa_tls_free(pa_tls *t) {
200 ASSERT_SUCCESS(pthread_key_delete(t->key));
204 void *pa_tls_get(pa_tls *t) {
207 return pthread_getspecific(t->key);
210 void *pa_tls_set(pa_tls *t, void *userdata) {
213 r = pthread_getspecific(t->key);
214 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));