]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-win32.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / thread-win32.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.1 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 <stdio.h>
27
28 #include <windows.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/once.h>
32
33 #include "thread.h"
34
35 struct pa_thread {
36 HANDLE thread;
37 pa_thread_func_t thread_func;
38 void *userdata;
39 };
40
41 struct pa_tls {
42 DWORD index;
43 pa_free_cb_t free_func;
44 };
45
46 struct pa_tls_monitor {
47 HANDLE thread;
48 pa_free_cb_t free_func;
49 void *data;
50 };
51
52 static pa_tls *thread_tls;
53 static pa_once thread_tls_once = PA_ONCE_INIT;
54 static pa_tls *monitor_tls;
55
56 static void thread_tls_once_func(void) {
57 thread_tls = pa_tls_new(NULL);
58 assert(thread_tls);
59 }
60
61 static DWORD WINAPI internal_thread_func(LPVOID param) {
62 pa_thread *t = param;
63 assert(t);
64
65 pa_run_once(&thread_tls_once, thread_tls_once_func);
66 pa_tls_set(thread_tls, t);
67
68 t->thread_func(t->userdata);
69
70 return 0;
71 }
72
73 pa_thread* pa_thread_new(const char *name, pa_thread_func_t thread_func, void *userdata) {
74 pa_thread *t;
75 DWORD thread_id;
76
77 assert(thread_func);
78
79 t = pa_xnew(pa_thread, 1);
80 t->thread_func = thread_func;
81 t->userdata = userdata;
82
83 t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, &thread_id);
84
85 if (!t->thread) {
86 pa_xfree(t);
87 return NULL;
88 }
89
90 return t;
91 }
92
93 int pa_thread_is_running(pa_thread *t) {
94 DWORD code;
95
96 assert(t);
97
98 if (!GetExitCodeThread(t->thread, &code))
99 return 0;
100
101 return code == STILL_ACTIVE;
102 }
103
104 void pa_thread_free(pa_thread *t) {
105 assert(t);
106
107 pa_thread_join(t);
108 CloseHandle(t->thread);
109 pa_xfree(t);
110 }
111
112 void pa_thread_free_nojoin(pa_thread *t) {
113 pa_assert(t);
114
115 CloseHandle(t->thread);
116 pa_xfree(t);
117 }
118
119 int pa_thread_join(pa_thread *t) {
120 assert(t);
121
122 if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
123 return -1;
124
125 return 0;
126 }
127
128 pa_thread* pa_thread_self(void) {
129 pa_run_once(&thread_tls_once, thread_tls_once_func);
130 return pa_tls_get(thread_tls);
131 }
132
133 void* pa_thread_get_data(pa_thread *t) {
134 pa_assert(t);
135
136 return t->userdata;
137 }
138
139 void pa_thread_set_data(pa_thread *t, void *userdata) {
140 pa_assert(t);
141
142 t->userdata = userdata;
143 }
144
145 void pa_thread_set_name(pa_thread *t, const char *name) {
146 /* Not implemented */
147 }
148
149 const char *pa_thread_get_name(pa_thread *t) {
150 /* Not implemented */
151 return NULL;
152 }
153
154 void pa_thread_yield(void) {
155 Sleep(0);
156 }
157
158 static DWORD WINAPI monitor_thread_func(LPVOID param) {
159 struct pa_tls_monitor *m = param;
160 assert(m);
161
162 WaitForSingleObject(m->thread, INFINITE);
163
164 CloseHandle(m->thread);
165
166 m->free_func(m->data);
167
168 pa_xfree(m);
169
170 return 0;
171 }
172
173 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
174 pa_tls *t;
175
176 t = pa_xnew(pa_tls, 1);
177 t->index = TlsAlloc();
178 t->free_func = free_cb;
179
180 if (t->index == TLS_OUT_OF_INDEXES) {
181 pa_xfree(t);
182 return NULL;
183 }
184
185 return t;
186 }
187
188 void pa_tls_free(pa_tls *t) {
189 assert(t);
190
191 TlsFree(t->index);
192 pa_xfree(t);
193 }
194
195 void *pa_tls_get(pa_tls *t) {
196 assert(t);
197
198 return TlsGetValue(t->index);
199 }
200
201 void *pa_tls_set(pa_tls *t, void *userdata) {
202 void *r;
203
204 assert(t);
205
206 r = TlsGetValue(t->index);
207
208 TlsSetValue(t->index, userdata);
209
210 if (t->free_func) {
211 struct pa_tls_monitor *m;
212
213 PA_ONCE_BEGIN {
214 monitor_tls = pa_tls_new(NULL);
215 assert(monitor_tls);
216 pa_tls_set(monitor_tls, NULL);
217 } PA_ONCE_END;
218
219 m = pa_tls_get(monitor_tls);
220 if (!m) {
221 HANDLE thread;
222
223 m = pa_xnew(struct pa_tls_monitor, 1);
224
225 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
226 GetCurrentProcess(), &m->thread, 0, FALSE,
227 DUPLICATE_SAME_ACCESS);
228
229 m->free_func = t->free_func;
230
231 pa_tls_set(monitor_tls, m);
232
233 thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);
234 assert(thread);
235 CloseHandle(thread);
236 }
237
238 m->data = userdata;
239 }
240
241 return r;
242 }