Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[profile/ivi/pulseaudio-panda.git] / 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 pthread_key_t thread_key;
53 static pthread_once_t thread_once = PTHREAD_ONCE_INIT;
54
55 static void thread_free_cb(void *p) {
56     pa_thread *t = p;
57
58     pa_assert(t);
59
60     if (!t->thread_func)
61         /* This is a foreign thread, we need to free the struct */
62         pa_xfree(t);
63 }
64
65 static void thread_once_func(void) {
66     pa_assert_se(pthread_key_create(&thread_key, thread_free_cb) == 0);
67 }
68
69 static void* internal_thread_func(void *userdata) {
70     pa_thread *t = userdata;
71     pa_assert(t);
72
73     t->id = pthread_self();
74
75     pthread_once(&thread_once, thread_once_func);
76     pthread_setspecific(thread_key, t);
77
78     pa_atomic_inc(&t->running);
79     t->thread_func(t->userdata);
80     pa_atomic_sub(&t->running, 2);
81
82     return NULL;
83 }
84
85 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
86     pa_thread *t;
87
88     pa_assert(thread_func);
89
90     t = pa_xnew(pa_thread, 1);
91     t->thread_func = thread_func;
92     t->userdata = userdata;
93     pa_atomic_store(&t->running, 0);
94
95     if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
96         pa_xfree(t);
97         return NULL;
98     }
99
100     pa_atomic_inc(&t->running);
101
102     return t;
103 }
104
105 int pa_thread_is_running(pa_thread *t) {
106     pa_assert(t);
107
108     /* Unfortunately there is no way to tell whether a "foreign"
109      * thread is still running. See
110      * http://udrepper.livejournal.com/16844.html for more
111      * information */
112     pa_assert(t->thread_func);
113
114     return pa_atomic_load(&t->running) > 0;
115 }
116
117 void pa_thread_free(pa_thread *t) {
118     pa_assert(t);
119
120     pa_thread_join(t);
121     pa_xfree(t);
122 }
123
124 int pa_thread_join(pa_thread *t) {
125     pa_assert(t);
126
127     return pthread_join(t->id, NULL);
128 }
129
130 pa_thread* pa_thread_self(void) {
131     pa_thread *t;
132
133     pthread_once(&thread_once, thread_once_func);
134
135     if ((t = pthread_getspecific(thread_key)))
136         return t;
137
138     /* This is a foreign thread, let's create a pthread structure to
139      * make sure that we can always return a sensible pointer */
140
141     t = pa_xnew(pa_thread, 1);
142     t->id = pthread_self();
143     t->thread_func = NULL;
144     t->userdata = NULL;
145     pa_atomic_store(&t->running, 2);
146
147     pthread_setspecific(thread_key, t);
148
149     return t;
150 }
151
152 void* pa_thread_get_data(pa_thread *t) {
153     pa_assert(t);
154
155     return t->userdata;
156 }
157
158 void pa_thread_set_data(pa_thread *t, void *userdata) {
159     pa_assert(t);
160
161     t->userdata = userdata;
162 }
163
164 void pa_thread_yield(void) {
165 #ifdef HAVE_PTHREAD_YIELD
166     pthread_yield();
167 #else
168     pa_assert_se(sched_yield() == 0);
169 #endif
170 }
171
172 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
173     pa_tls *t;
174
175     t = pa_xnew(pa_tls, 1);
176
177     if (pthread_key_create(&t->key, free_cb) < 0) {
178         pa_xfree(t);
179         return NULL;
180     }
181
182     return t;
183 }
184
185 void pa_tls_free(pa_tls *t) {
186     pa_assert(t);
187
188     pa_assert_se(pthread_key_delete(t->key) == 0);
189     pa_xfree(t);
190 }
191
192 void *pa_tls_get(pa_tls *t) {
193     pa_assert(t);
194
195     return pthread_getspecific(t->key);
196 }
197
198 void *pa_tls_set(pa_tls *t, void *userdata) {
199     void *r;
200
201     r = pthread_getspecific(t->key);
202     pa_assert_se(pthread_setspecific(t->key, userdata) == 0);
203     return r;
204 }
205