add accessor functions for the userdata attached to a pa_thread object
[pulseaudio.git] / src / pulsecore / thread-posix.c
bloba2cb9b56c8969648a13eb4d2d18b4e4f835d8013
1 /* $Id$ */
3 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <assert.h>
27 #include <pthread.h>
28 #include <sched.h>
29 #include <errno.h>
31 #include <atomic_ops.h>
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/mutex.h>
36 #include "thread.h"
38 #define ASSERT_SUCCESS(x) do { \
39 int _r = (x); \
40 assert(_r == 0); \
41 } while(0)
43 struct pa_thread {
44 pthread_t id;
45 pa_thread_func_t thread_func;
46 void *userdata;
47 AO_t running;
50 struct pa_tls {
51 pthread_key_t key;
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) {
61 pa_thread *t = p;
63 assert(t);
65 if (!t->thread_func)
66 /* This is a foreign thread, we need to free the struct */
67 pa_xfree(t);
70 static void thread_tls_once_func(void) {
71 thread_tls = pa_tls_new(tls_free_cb);
72 assert(thread_tls);
75 static void* internal_thread_func(void *userdata) {
76 pa_thread *t = userdata;
77 assert(t);
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);
88 return NULL;
91 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
92 pa_thread *t;
94 assert(thread_func);
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) {
102 pa_xfree(t);
103 return NULL;
106 AO_fetch_and_add1_full(&t->running);
108 return t;
111 int pa_thread_is_running(pa_thread *t) {
112 AO_t r;
113 assert(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. */
120 int policy;
121 struct sched_param param;
123 return pthread_getschedparam(t->id, &policy, &param) >= 0 || errno != ESRCH;
126 r = AO_load_full(&t->running);
127 return r == 1 || r == 2;
130 void pa_thread_free(pa_thread *t) {
131 assert(t);
133 pa_thread_join(t);
134 pa_xfree(t);
137 int pa_thread_join(pa_thread *t) {
138 assert(t);
140 return pthread_join(t->id, NULL);
143 pa_thread* pa_thread_self(void) {
144 pa_thread *t;
146 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
148 if ((t = pa_tls_get(thread_tls)))
149 return t;
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;
157 t->userdata = NULL;
158 AO_store_full(&t->running, 1);
160 pa_tls_set(thread_tls, t);
162 return t;
165 void* pa_thread_get_data(pa_thread *t) {
166 assert(t);
168 return t->userdata;
171 void pa_thread_set_data(pa_thread *t, void *userdata) {
172 assert(t);
174 t->userdata = userdata;
177 void pa_thread_yield(void) {
178 #ifdef HAVE_PTHREAD_YIELD
179 pthread_yield();
180 #else
181 ASSERT_SUCCESS(sched_yield());
182 #endif
185 static void thread_once_once_func(void) {
186 once_mutex = pa_mutex_new(0);
187 assert(once_mutex);
190 void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func) {
191 assert(control);
192 assert(once_func);
194 ASSERT_SUCCESS(pthread_once(&thread_once_once, thread_once_once_func));
196 pa_mutex_lock(once_mutex);
198 if (*control == PA_THREAD_ONCE_INIT) {
199 *control = ~PA_THREAD_ONCE_INIT;
200 pa_mutex_unlock(once_mutex);
201 once_func();
202 } else
203 pa_mutex_unlock(once_mutex);
206 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
207 pa_tls *t;
209 t = pa_xnew(pa_tls, 1);
211 if (pthread_key_create(&t->key, free_cb) < 0) {
212 pa_xfree(t);
213 return NULL;
216 return t;
219 void pa_tls_free(pa_tls *t) {
220 assert(t);
222 ASSERT_SUCCESS(pthread_key_delete(t->key));
223 pa_xfree(t);
226 void *pa_tls_get(pa_tls *t) {
227 assert(t);
229 return pthread_getspecific(t->key);
232 void *pa_tls_set(pa_tls *t, void *userdata) {
233 void *r;
235 r = pthread_getspecific(t->key);
236 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
237 return r;