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 pthread_key_t thread_key;
53 static pthread_once_t thread_once = PTHREAD_ONCE_INIT;
55 static void thread_free_cb(void *p) {
61 /* This is a foreign thread, we need to free the struct */
65 static void thread_once_func(void) {
66 pa_assert_se(pthread_key_create(&thread_key, thread_free_cb) == 0);
69 static void* internal_thread_func(void *userdata) {
70 pa_thread *t = userdata;
73 t->id = pthread_self();
75 pthread_once(&thread_once, thread_once_func);
76 pthread_setspecific(thread_key, t);
78 pa_atomic_inc(&t->running);
79 t->thread_func(t->userdata);
80 pa_atomic_sub(&t->running, 2);
85 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
88 pa_assert(thread_func);
90 t = pa_xnew(pa_thread, 1);
91 t->thread_func = thread_func;
92 t->userdata = userdata;
93 pa_atomic_store(&t->running, 0);
95 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
100 pa_atomic_inc(&t->running);
105 int pa_thread_is_running(pa_thread *t) {
108 /* Unfortunately there is no way to tell whether a "foreign"
109 * thread is still running. See
110 * http://udrepper.livejournal.com/16844.html for more
112 pa_assert(t->thread_func);
114 return pa_atomic_load(&t->running) > 0;
117 void pa_thread_free(pa_thread *t) {
124 int pa_thread_join(pa_thread *t) {
127 return pthread_join(t->id, NULL);
130 pa_thread* pa_thread_self(void) {
133 pthread_once(&thread_once, thread_once_func);
135 if ((t = pthread_getspecific(thread_key)))
138 /* This is a foreign thread, let's create a pthread structure to
139 * make sure that we can always return a sensible pointer */
141 t = pa_xnew(pa_thread, 1);
142 t->id = pthread_self();
143 t->thread_func = NULL;
145 pa_atomic_store(&t->running, 2);
147 pthread_setspecific(thread_key, t);
152 void* pa_thread_get_data(pa_thread *t) {
158 void pa_thread_set_data(pa_thread *t, void *userdata) {
161 t->userdata = userdata;
164 void pa_thread_yield(void) {
165 #ifdef HAVE_PTHREAD_YIELD
168 pa_assert_se(sched_yield() == 0);
172 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
175 t = pa_xnew(pa_tls, 1);
177 if (pthread_key_create(&t->key, free_cb) < 0) {
185 void pa_tls_free(pa_tls *t) {
188 pa_assert_se(pthread_key_delete(t->key) == 0);
192 void *pa_tls_get(pa_tls *t) {
195 return pthread_getspecific(t->key);
198 void *pa_tls_set(pa_tls *t, void *userdata) {
201 r = pthread_getspecific(t->key);
202 pa_assert_se(pthread_setspecific(t->key, userdata) == 0);