]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-posix.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / thread-posix.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <pthread.h>
30 #include <sched.h>
31 #include <errno.h>
32
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>
38
39 #include "thread.h"
40
41 struct pa_thread {
42 pthread_t id;
43 pa_thread_func_t thread_func;
44 void *userdata;
45 pa_atomic_t running;
46 };
47
48 struct pa_tls {
49 pthread_key_t key;
50 };
51
52 static void thread_free_cb(void *p) {
53 pa_thread *t = p;
54
55 pa_assert(t);
56
57 if (!t->thread_func)
58 /* This is a foreign thread, we need to free the struct */
59 pa_xfree(t);
60 }
61
62 PA_STATIC_TLS_DECLARE(current_thread, thread_free_cb);
63
64 static void* internal_thread_func(void *userdata) {
65 pa_thread *t = userdata;
66 pa_assert(t);
67
68 t->id = pthread_self();
69
70 PA_STATIC_TLS_SET(current_thread, t);
71
72 pa_atomic_inc(&t->running);
73 t->thread_func(t->userdata);
74 pa_atomic_sub(&t->running, 2);
75
76 return NULL;
77 }
78
79 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
80 pa_thread *t;
81
82 pa_assert(thread_func);
83
84 t = pa_xnew(pa_thread, 1);
85 t->thread_func = thread_func;
86 t->userdata = userdata;
87 pa_atomic_store(&t->running, 0);
88
89 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
90 pa_xfree(t);
91 return NULL;
92 }
93
94 pa_atomic_inc(&t->running);
95
96 return t;
97 }
98
99 int pa_thread_is_running(pa_thread *t) {
100 pa_assert(t);
101
102 /* Unfortunately there is no way to tell whether a "foreign"
103 * thread is still running. See
104 * http://udrepper.livejournal.com/16844.html for more
105 * information */
106 pa_assert(t->thread_func);
107
108 return pa_atomic_load(&t->running) > 0;
109 }
110
111 void pa_thread_free(pa_thread *t) {
112 pa_assert(t);
113
114 pa_thread_join(t);
115 pa_xfree(t);
116 }
117
118 int pa_thread_join(pa_thread *t) {
119 pa_assert(t);
120
121 return pthread_join(t->id, NULL);
122 }
123
124 pa_thread* pa_thread_self(void) {
125 pa_thread *t;
126
127 if ((t = PA_STATIC_TLS_GET(current_thread)))
128 return t;
129
130 /* This is a foreign thread, let's create a pthread structure to
131 * make sure that we can always return a sensible pointer */
132
133 t = pa_xnew(pa_thread, 1);
134 t->id = pthread_self();
135 t->thread_func = NULL;
136 t->userdata = NULL;
137 pa_atomic_store(&t->running, 2);
138
139 PA_STATIC_TLS_SET(current_thread, t);
140
141 return t;
142 }
143
144 void* pa_thread_get_data(pa_thread *t) {
145 pa_assert(t);
146
147 return t->userdata;
148 }
149
150 void pa_thread_set_data(pa_thread *t, void *userdata) {
151 pa_assert(t);
152
153 t->userdata = userdata;
154 }
155
156 void pa_thread_yield(void) {
157 #ifdef HAVE_PTHREAD_YIELD
158 pthread_yield();
159 #else
160 pa_assert_se(sched_yield() == 0);
161 #endif
162 }
163
164 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
165 pa_tls *t;
166
167 t = pa_xnew(pa_tls, 1);
168
169 if (pthread_key_create(&t->key, free_cb) < 0) {
170 pa_xfree(t);
171 return NULL;
172 }
173
174 return t;
175 }
176
177 void pa_tls_free(pa_tls *t) {
178 pa_assert(t);
179
180 pa_assert_se(pthread_key_delete(t->key) == 0);
181 pa_xfree(t);
182 }
183
184 void *pa_tls_get(pa_tls *t) {
185 pa_assert(t);
186
187 return pthread_getspecific(t->key);
188 }
189
190 void *pa_tls_set(pa_tls *t, void *userdata) {
191 void *r;
192
193 r = pthread_getspecific(t->key);
194 pa_assert_se(pthread_setspecific(t->key, userdata) == 0);
195 return r;
196 }
197