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.1 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
32 #include <sys/prctl.h>
35 #include <pulse/xmalloc.h>
36 #include <pulsecore/atomic.h>
37 #include <pulsecore/macro.h>
43 pa_thread_func_t thread_func
;
54 static void thread_free_cb(void *p
) {
59 if (!t
->thread_func
) {
60 /* This is a foreign thread, we need to free the struct */
66 PA_STATIC_TLS_DECLARE(current_thread
, thread_free_cb
);
68 static void* internal_thread_func(void *userdata
) {
69 pa_thread
*t
= userdata
;
73 prctl(PR_SET_NAME
, t
->name
);
74 #elif defined(HAVE_PTHREAD_SETNAME_NP) && defined(OS_IS_DARWIN)
75 pthread_setname_np(t
->name
);
78 t
->id
= pthread_self();
80 PA_STATIC_TLS_SET(current_thread
, t
);
82 pa_atomic_inc(&t
->running
);
83 t
->thread_func(t
->userdata
);
84 pa_atomic_sub(&t
->running
, 2);
89 pa_thread
* pa_thread_new(const char *name
, pa_thread_func_t thread_func
, void *userdata
) {
92 pa_assert(thread_func
);
94 t
= pa_xnew0(pa_thread
, 1);
95 t
->name
= pa_xstrdup(name
);
96 t
->thread_func
= thread_func
;
97 t
->userdata
= userdata
;
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 /* Unfortunately there is no way to tell whether a "foreign"
113 * thread is still running. See
114 * http://udrepper.livejournal.com/16844.html for more
116 pa_assert(t
->thread_func
);
118 return pa_atomic_load(&t
->running
) > 0;
121 void pa_thread_free(pa_thread
*t
) {
130 void pa_thread_free_nojoin(pa_thread
*t
) {
137 int pa_thread_join(pa_thread
*t
) {
139 pa_assert(t
->thread_func
);
145 return pthread_join(t
->id
, NULL
);
148 pa_thread
* pa_thread_self(void) {
151 if ((t
= PA_STATIC_TLS_GET(current_thread
)))
154 /* This is a foreign thread, let's create a pthread structure to
155 * make sure that we can always return a sensible pointer */
157 t
= pa_xnew0(pa_thread
, 1);
158 t
->id
= pthread_self();
160 pa_atomic_store(&t
->running
, 2);
162 PA_STATIC_TLS_SET(current_thread
, t
);
167 void* pa_thread_get_data(pa_thread
*t
) {
173 void pa_thread_set_data(pa_thread
*t
, void *userdata
) {
176 t
->userdata
= userdata
;
179 void pa_thread_set_name(pa_thread
*t
, const char *name
) {
183 t
->name
= pa_xstrdup(name
);
186 prctl(PR_SET_NAME
, name
);
187 #elif defined(HAVE_PTHREAD_SETNAME_NP) && defined(OS_IS_DARWIN)
188 pthread_setname_np(name
);
192 const char *pa_thread_get_name(pa_thread
*t
) {
197 t
->name
= pa_xmalloc(17);
199 if (prctl(PR_GET_NAME
, t
->name
) >= 0)
206 #elif defined(HAVE_PTHREAD_GETNAME_NP) && defined(OS_IS_DARWIN)
208 t
->name
= pa_xmalloc0(17);
209 pthread_getname_np(t
->id
, t
->name
, 16);
216 void pa_thread_yield(void) {
217 #ifdef HAVE_PTHREAD_YIELD
220 pa_assert_se(sched_yield() == 0);
224 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
227 t
= pa_xnew(pa_tls
, 1);
229 if (pthread_key_create(&t
->key
, free_cb
) < 0) {
237 void pa_tls_free(pa_tls
*t
) {
240 pa_assert_se(pthread_key_delete(t
->key
) == 0);
244 void *pa_tls_get(pa_tls
*t
) {
247 return pthread_getspecific(t
->key
);
250 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
253 r
= pthread_getspecific(t
->key
);
254 pa_assert_se(pthread_setspecific(t
->key
, userdata
) == 0);