Add a missing pa_xfree.
[pulseaudio.git] / src / pulsecore / thread-posix.c
blob7f43f43e75288949bf5ee73f4402a9bdb6067f46
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <pthread.h>
30 #include <sched.h>
31 #include <errno.h>
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>
39 #include "thread.h"
41 struct pa_thread {
42 pthread_t id;
43 pa_thread_func_t thread_func;
44 void *userdata;
45 pa_atomic_t running;
48 struct pa_tls {
49 pthread_key_t key;
52 static void thread_free_cb(void *p) {
53 pa_thread *t = p;
55 pa_assert(t);
57 if (!t->thread_func)
58 /* This is a foreign thread, we need to free the struct */
59 pa_xfree(t);
62 PA_STATIC_TLS_DECLARE(current_thread, thread_free_cb);
64 static void* internal_thread_func(void *userdata) {
65 pa_thread *t = userdata;
66 pa_assert(t);
68 t->id = pthread_self();
70 PA_STATIC_TLS_SET(current_thread, t);
72 pa_atomic_inc(&t->running);
73 t->thread_func(t->userdata);
74 pa_atomic_sub(&t->running, 2);
76 return NULL;
79 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
80 pa_thread *t;
82 pa_assert(thread_func);
84 t = pa_xnew(pa_thread, 1);
85 t->thread_func = thread_func;
86 t->userdata = userdata;
87 pa_atomic_store(&t->running, 0);
89 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
90 pa_xfree(t);
91 return NULL;
94 pa_atomic_inc(&t->running);
96 return t;
99 int pa_thread_is_running(pa_thread *t) {
100 pa_assert(t);
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);
108 return pa_atomic_load(&t->running) > 0;
111 void pa_thread_free(pa_thread *t) {
112 pa_assert(t);
114 pa_thread_join(t);
115 pa_xfree(t);
118 int pa_thread_join(pa_thread *t) {
119 pa_assert(t);
121 return pthread_join(t->id, NULL);
124 pa_thread* pa_thread_self(void) {
125 pa_thread *t;
127 if ((t = PA_STATIC_TLS_GET(current_thread)))
128 return t;
130 /* This is a foreign thread, let's create a pthread structure to
131 * make sure that we can always return a sensible pointer */
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);
139 PA_STATIC_TLS_SET(current_thread, t);
141 return t;
144 void* pa_thread_get_data(pa_thread *t) {
145 pa_assert(t);
147 return t->userdata;
150 void pa_thread_set_data(pa_thread *t, void *userdata) {
151 pa_assert(t);
153 t->userdata = userdata;
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
164 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
165 pa_tls *t;
167 t = pa_xnew(pa_tls, 1);
169 if (pthread_key_create(&t->key, free_cb) < 0) {
170 pa_xfree(t);
171 return NULL;
174 return t;
177 void pa_tls_free(pa_tls *t) {
178 pa_assert(t);
180 pa_assert_se(pthread_key_delete(t->key) == 0);
181 pa_xfree(t);
184 void *pa_tls_get(pa_tls *t) {
185 pa_assert(t);
187 return pthread_getspecific(t->key);
190 void *pa_tls_set(pa_tls *t, void *userdata) {
191 void *r;
193 r = pthread_getspecific(t->key);
194 pa_assert_se(pthread_setspecific(t->key, userdata) == 0);
195 return r;