]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-posix.c
54f21b75a7d404a830498e0046a06ef12941fed4
[pulseaudio] / src / pulsecore / thread-posix.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <pthread.h>
28
29 #define AO_REQUIRE_CAS
30 #include <atomic_ops.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include "thread.h"
35
36 #define ASSERT_SUCCESS(x) do { \
37 int _r = (x); \
38 assert(_r == 0); \
39 } while(0)
40
41 struct pa_thread {
42 pthread_t id;
43 pa_thread_func_t thread_func;
44 void *userdata;
45 AO_t running;
46 };
47
48 struct pa_tls {
49 pthread_key_t key;
50 };
51
52 static pa_tls *thread_tls;
53 static pthread_once_t thread_tls_once = PTHREAD_ONCE_INIT;
54
55 static void thread_tls_once_func(void) {
56 thread_tls = pa_tls_new(NULL);
57 assert(thread_tls);
58 }
59
60 static void* internal_thread_func(void *userdata) {
61 pa_thread *t = userdata;
62 assert(t);
63
64 t->id = pthread_self();
65
66 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
67 pa_tls_set(thread_tls, t);
68
69 AO_store_release_write(&t->running, 1);
70 t->thread_func(t->userdata);
71 AO_store_release_write(&t->running, 0);
72
73 return NULL;
74 }
75
76 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
77 pa_thread *t;
78
79 t = pa_xnew(pa_thread, 1);
80 t->thread_func = thread_func;
81 t->userdata = userdata;
82
83 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
84 pa_xfree(t);
85 return NULL;
86 }
87
88 return t;
89 }
90
91 int pa_thread_is_running(pa_thread *t) {
92 assert(t);
93
94 return !!AO_load_acquire_read(&t->running);
95 }
96
97 void pa_thread_free(pa_thread *t) {
98 assert(t);
99
100 pa_thread_join(t);
101 pa_xfree(t);
102 }
103
104 int pa_thread_join(pa_thread *t) {
105 assert(t);
106
107 return pthread_join(t->id, NULL);
108 }
109
110 pa_thread* pa_thread_self(void) {
111 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
112 return pa_tls_get(thread_tls);
113 }
114
115 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
116 pa_tls *t;
117
118 t = pa_xnew(pa_tls, 1);
119
120 if (pthread_key_create(&t->key, free_cb) < 0) {
121 pa_xfree(t);
122 return NULL;
123 }
124
125 return t;
126 }
127
128 void pa_tls_free(pa_tls *t) {
129 assert(t);
130
131 ASSERT_SUCCESS(pthread_key_delete(t->key));
132 pa_xfree(t);
133 }
134
135 void *pa_tls_get(pa_tls *t) {
136 assert(t);
137
138 return pthread_getspecific(t->key);
139 }
140
141 void *pa_tls_set(pa_tls *t, void *userdata) {
142 void *r;
143
144 r = pthread_getspecific(t->key);
145 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
146 return r;
147 }
148