4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <atomic_ops.h>
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/mutex.h>
38 #define ASSERT_SUCCESS(x) do { \
45 pa_thread_func_t thread_func
;
54 static pa_tls
*thread_tls
;
55 static pthread_once_t thread_tls_once
= PTHREAD_ONCE_INIT
;
57 static pa_mutex
*once_mutex
;
58 static pthread_once_t thread_once_once
= PTHREAD_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 ASSERT_SUCCESS(pthread_once(&thread_tls_once
, thread_tls_once_func
));
82 pa_tls_set(thread_tls
, t
);
84 AO_fetch_and_add1_full(&t
->running
);
85 t
->thread_func(t
->userdata
);
86 AO_fetch_and_add_full(&t
->running
, (AO_t
) -2);
91 pa_thread
* pa_thread_new(pa_thread_func_t thread_func
, void *userdata
) {
96 t
= pa_xnew(pa_thread
, 1);
97 t
->thread_func
= thread_func
;
98 t
->userdata
= userdata
;
99 AO_store_full(&t
->running
, 0);
101 if (pthread_create(&t
->id
, NULL
, internal_thread_func
, t
) < 0) {
106 AO_fetch_and_add1_full(&t
->running
);
111 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 r
= AO_load_full(&t
->running
);
127 return r
== 1 || r
== 2;
130 void pa_thread_free(pa_thread
*t
) {
137 int pa_thread_join(pa_thread
*t
) {
140 return pthread_join(t
->id
, NULL
);
143 pa_thread
* pa_thread_self(void) {
146 ASSERT_SUCCESS(pthread_once(&thread_tls_once
, thread_tls_once_func
));
148 if ((t
= pa_tls_get(thread_tls
)))
151 /* This is a foreign thread, let's create a pthread structure to
152 * make sure that we can always return a sensible pointer */
154 t
= pa_xnew(pa_thread
, 1);
155 t
->id
= pthread_self();
156 t
->thread_func
= NULL
;
158 AO_store_full(&t
->running
, 1);
160 pa_tls_set(thread_tls
, t
);
165 void pa_thread_yield(void) {
166 #ifdef HAVE_PTHREAD_YIELD
169 ASSERT_SUCCESS(sched_yield());
173 static void thread_once_once_func(void) {
174 once_mutex
= pa_mutex_new(0);
178 void pa_thread_once(pa_thread_once_t
*control
, pa_thread_once_func_t once_func
) {
182 ASSERT_SUCCESS(pthread_once(&thread_once_once
, thread_once_once_func
));
184 pa_mutex_lock(once_mutex
);
186 if (*control
== PA_THREAD_ONCE_INIT
) {
187 *control
= ~PA_THREAD_ONCE_INIT
;
188 pa_mutex_unlock(once_mutex
);
191 pa_mutex_unlock(once_mutex
);
194 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
197 t
= pa_xnew(pa_tls
, 1);
199 if (pthread_key_create(&t
->key
, free_cb
) < 0) {
207 void pa_tls_free(pa_tls
*t
) {
210 ASSERT_SUCCESS(pthread_key_delete(t
->key
));
214 void *pa_tls_get(pa_tls
*t
) {
217 return pthread_getspecific(t
->key
);
220 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
223 r
= pthread_getspecific(t
->key
);
224 ASSERT_SUCCESS(pthread_setspecific(t
->key
, userdata
));