]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-win32.c
Add pthread_once() equivalent support.
[pulseaudio] / src / pulsecore / thread-win32.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 <stdio.h>
27
28 #include <windows.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/log.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_thread_once_t thread_tls_once = PA_THREAD_ONCE_INIT;
54 static pa_tls *monitor_tls;
55 static pa_thread_once_t monitor_tls_once = PA_THREAD_ONCE_INIT;
56
57 static void thread_tls_once_func(void) {
58 thread_tls = pa_tls_new(NULL);
59 assert(thread_tls);
60 }
61
62 static DWORD WINAPI internal_thread_func(LPVOID param) {
63 pa_thread *t = param;
64 assert(t);
65
66 pa_thread_once(&thread_tls_once, thread_tls_once_func);
67 pa_tls_set(thread_tls, t);
68
69 t->thread_func(t->userdata);
70
71 return 0;
72 }
73
74 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
75 pa_thread *t;
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, NULL);
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 int pa_thread_join(pa_thread *t) {
113 assert(t);
114
115 if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
116 return -1;
117
118 return 0;
119 }
120
121 pa_thread* pa_thread_self(void) {
122 pa_thread_once(&thread_tls_once, thread_tls_once_func);
123 return pa_tls_get(thread_tls);
124 }
125
126 void pa_thread_yield(void) {
127 Sleep(0);
128 }
129
130 void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func) {
131 HANDLE mutex;
132 char name[64];
133
134 assert(control);
135 assert(once_func);
136
137 sprintf(name, "pulse%d", (int)GetCurrentProcessId());
138
139 mutex = CreateMutex(NULL, FALSE, name);
140 assert(mutex);
141
142 WaitForSingleObject(mutex, INFINITE);
143
144 if (*control == PA_THREAD_ONCE_INIT) {
145 *control = ~PA_THREAD_ONCE_INIT;
146 ReleaseMutex(mutex);
147 once_func();
148 } else
149 ReleaseMutex(mutex);
150
151 CloseHandle(mutex);
152 }
153
154 static void monitor_tls_once_func(void) {
155 monitor_tls = pa_tls_new(NULL);
156 assert(monitor_tls);
157 pa_tls_set(monitor_tls, NULL);
158 }
159
160 static DWORD WINAPI monitor_thread_func(LPVOID param) {
161 struct pa_tls_monitor *m = param;
162 assert(m);
163
164 WaitForSingleObject(m->thread, INFINITE);
165
166 CloseHandle(m->thread);
167
168 m->free_func(m->data);
169
170 pa_xfree(m);
171
172 return 0;
173 }
174
175 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
176 pa_tls *t;
177
178 t = pa_xnew(pa_tls, 1);
179 t->index = TlsAlloc();
180 t->free_func = free_cb;
181
182 if (t->index == TLS_OUT_OF_INDEXES) {
183 pa_xfree(t);
184 return NULL;
185 }
186
187 return t;
188 }
189
190 void pa_tls_free(pa_tls *t) {
191 assert(t);
192
193 TlsFree(t->index);
194 pa_xfree(t);
195 }
196
197 void *pa_tls_get(pa_tls *t) {
198 assert(t);
199
200 return TlsGetValue(t->index);
201 }
202
203 void *pa_tls_set(pa_tls *t, void *userdata) {
204 void *r;
205
206 assert(t);
207
208 r = TlsGetValue(t->index);
209
210 TlsSetValue(t->index, userdata);
211
212 if (t->free_func) {
213 struct pa_tls_monitor *m;
214
215 pa_thread_once(&monitor_tls_once, monitor_tls_once_func);
216
217 m = pa_tls_get(monitor_tls);
218 if (!m) {
219 HANDLE thread;
220
221 m = pa_xnew(struct pa_tls_monitor, 1);
222
223 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
224 GetCurrentProcess(), &m->thread, 0, FALSE,
225 DUPLICATE_SAME_ACCESS);
226
227 m->free_func = t->free_func;
228
229 pa_tls_set(monitor_tls, m);
230
231 thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);
232 assert(thread);
233 CloseHandle(thread);
234 }
235
236 m->data = userdata;
237 }
238
239 return r;
240 }