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 <pulse/xmalloc.h>
32 #include <pulsecore/mutex.h>
33 #include <pulsecore/once.h>
34 #include <pulsecore/atomic.h>
38 #define ASSERT_SUCCESS(x) do { \
45 pa_thread_func_t thread_func
;
47 pa_atomic_int_t running
;
54 static pa_tls
*thread_tls
;
55 static pa_once_t thread_tls_once
= PA_ONCE_INIT
;
57 static void tls_free_cb(void *p
) {
63 /* This is a foreign thread, we need to free the struct */
67 static void thread_tls_once_func(void) {
68 thread_tls
= pa_tls_new(tls_free_cb
);
72 static void* internal_thread_func(void *userdata
) {
73 pa_thread
*t
= userdata
;
76 t
->id
= pthread_self();
78 pa_once(&thread_tls_once
, thread_tls_once_func
);
80 pa_tls_set(thread_tls
, t
);
82 pa_atomic_inc(&t
->running
);
83 t
->thread_func(t
->userdata
);
84 pa_atomic_add(&t
->running
, -2);
89 pa_thread
* pa_thread_new(pa_thread_func_t thread_func
, void *userdata
) {
94 t
= pa_xnew(pa_thread
, 1);
95 t
->thread_func
= thread_func
;
96 t
->userdata
= userdata
;
97 pa_atomic_store(&t
->running
, 0);
99 if (pthread_create(&t
->id
, NULL
, internal_thread_func
, t
) < 0) {
104 pa_atomic_inc(&t
->running
);
109 int pa_thread_is_running(pa_thread
*t
) {
112 if (!t
->thread_func
) {
113 /* Mhmm, this is a foreign thread, t->running is not
114 * necessarily valid. We misuse pthread_getschedparam() to
115 * check if the thread is valid. This might not be portable. */
118 struct sched_param param
;
120 return pthread_getschedparam(t
->id
, &policy
, ¶m
) >= 0 || errno
!= ESRCH
;
123 return pa_atomic_load(&t
->running
) > 0;
126 void pa_thread_free(pa_thread
*t
) {
133 int pa_thread_join(pa_thread
*t
) {
136 return pthread_join(t
->id
, NULL
);
139 pa_thread
* pa_thread_self(void) {
142 pa_once(&thread_tls_once
, thread_tls_once_func
);
144 if ((t
= pa_tls_get(thread_tls
)))
147 /* This is a foreign thread, let's create a pthread structure to
148 * make sure that we can always return a sensible pointer */
150 t
= pa_xnew(pa_thread
, 1);
151 t
->id
= pthread_self();
152 t
->thread_func
= NULL
;
154 pa_atomic_store(&t
->running
, 2);
156 pa_tls_set(thread_tls
, t
);
161 void* pa_thread_get_data(pa_thread
*t
) {
167 void pa_thread_set_data(pa_thread
*t
, void *userdata
) {
170 t
->userdata
= userdata
;
173 void pa_thread_yield(void) {
174 #ifdef HAVE_PTHREAD_YIELD
177 ASSERT_SUCCESS(sched_yield());
181 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
184 t
= pa_xnew(pa_tls
, 1);
186 if (pthread_key_create(&t
->key
, free_cb
) < 0) {
194 void pa_tls_free(pa_tls
*t
) {
197 ASSERT_SUCCESS(pthread_key_delete(t
->key
));
201 void *pa_tls_get(pa_tls
*t
) {
204 return pthread_getspecific(t
->key
);
207 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
210 r
= pthread_getspecific(t
->key
);
211 ASSERT_SUCCESS(pthread_setspecific(t
->key
, userdata
));