]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-posix.c
Add copyright notices to all relevant files. (based on svn log)
[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 <assert.h>
30 #include <pthread.h>
31 #include <sched.h>
32 #include <errno.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/mutex.h>
36 #include <pulsecore/once.h>
37 #include <pulsecore/atomic.h>
38
39 #include "thread.h"
40
41 #define ASSERT_SUCCESS(x) do { \
42 int _r = (x); \
43 assert(_r == 0); \
44 } while(0)
45
46 struct pa_thread {
47 pthread_t id;
48 pa_thread_func_t thread_func;
49 void *userdata;
50 pa_atomic_int_t running;
51 };
52
53 struct pa_tls {
54 pthread_key_t key;
55 };
56
57 static pa_tls *thread_tls;
58 static pa_once_t thread_tls_once = PA_ONCE_INIT;
59
60 static void tls_free_cb(void *p) {
61 pa_thread *t = p;
62
63 assert(t);
64
65 if (!t->thread_func)
66 /* This is a foreign thread, we need to free the struct */
67 pa_xfree(t);
68 }
69
70 static void thread_tls_once_func(void) {
71 thread_tls = pa_tls_new(tls_free_cb);
72 assert(thread_tls);
73 }
74
75 static void* internal_thread_func(void *userdata) {
76 pa_thread *t = userdata;
77 assert(t);
78
79 t->id = pthread_self();
80
81 pa_once(&thread_tls_once, thread_tls_once_func);
82
83 pa_tls_set(thread_tls, t);
84
85 pa_atomic_inc(&t->running);
86 t->thread_func(t->userdata);
87 pa_atomic_add(&t->running, -2);
88
89 return NULL;
90 }
91
92 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
93 pa_thread *t;
94
95 assert(thread_func);
96
97 t = pa_xnew(pa_thread, 1);
98 t->thread_func = thread_func;
99 t->userdata = userdata;
100 pa_atomic_store(&t->running, 0);
101
102 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
103 pa_xfree(t);
104 return NULL;
105 }
106
107 pa_atomic_inc(&t->running);
108
109 return t;
110 }
111
112 int pa_thread_is_running(pa_thread *t) {
113 assert(t);
114
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. */
119
120 int policy;
121 struct sched_param param;
122
123 return pthread_getschedparam(t->id, &policy, &param) >= 0 || errno != ESRCH;
124 }
125
126 return pa_atomic_load(&t->running) > 0;
127 }
128
129 void pa_thread_free(pa_thread *t) {
130 assert(t);
131
132 pa_thread_join(t);
133 pa_xfree(t);
134 }
135
136 int pa_thread_join(pa_thread *t) {
137 assert(t);
138
139 return pthread_join(t->id, NULL);
140 }
141
142 pa_thread* pa_thread_self(void) {
143 pa_thread *t;
144
145 pa_once(&thread_tls_once, thread_tls_once_func);
146
147 if ((t = pa_tls_get(thread_tls)))
148 return t;
149
150 /* This is a foreign thread, let's create a pthread structure to
151 * make sure that we can always return a sensible pointer */
152
153 t = pa_xnew(pa_thread, 1);
154 t->id = pthread_self();
155 t->thread_func = NULL;
156 t->userdata = NULL;
157 pa_atomic_store(&t->running, 2);
158
159 pa_tls_set(thread_tls, t);
160
161 return t;
162 }
163
164 void* pa_thread_get_data(pa_thread *t) {
165 assert(t);
166
167 return t->userdata;
168 }
169
170 void pa_thread_set_data(pa_thread *t, void *userdata) {
171 assert(t);
172
173 t->userdata = userdata;
174 }
175
176 void pa_thread_yield(void) {
177 #ifdef HAVE_PTHREAD_YIELD
178 pthread_yield();
179 #else
180 ASSERT_SUCCESS(sched_yield());
181 #endif
182 }
183
184 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
185 pa_tls *t;
186
187 t = pa_xnew(pa_tls, 1);
188
189 if (pthread_key_create(&t->key, free_cb) < 0) {
190 pa_xfree(t);
191 return NULL;
192 }
193
194 return t;
195 }
196
197 void pa_tls_free(pa_tls *t) {
198 assert(t);
199
200 ASSERT_SUCCESS(pthread_key_delete(t->key));
201 pa_xfree(t);
202 }
203
204 void *pa_tls_get(pa_tls *t) {
205 assert(t);
206
207 return pthread_getspecific(t->key);
208 }
209
210 void *pa_tls_set(pa_tls *t, void *userdata) {
211 void *r;
212
213 r = pthread_getspecific(t->key);
214 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
215 return r;
216 }
217