get rid of svn $ keywords
[pulseaudio-mirror.git] / src / pulsecore / thread-win32.c
blobc40d33428e3f2f17073c104e608abc2df1ff6abb
1 /***
2 This file is part of PulseAudio.
4 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
28 #include <windows.h>
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/once.h>
34 #include "thread.h"
36 struct pa_thread {
37 HANDLE thread;
38 pa_thread_func_t thread_func;
39 void *userdata;
42 struct pa_tls {
43 DWORD index;
44 pa_free_cb_t free_func;
47 struct pa_tls_monitor {
48 HANDLE thread;
49 pa_free_cb_t free_func;
50 void *data;
53 static pa_tls *thread_tls;
54 static pa_once thread_tls_once = PA_ONCE_INIT;
55 static pa_tls *monitor_tls;
57 static void thread_tls_once_func(void) {
58 thread_tls = pa_tls_new(NULL);
59 assert(thread_tls);
62 static DWORD WINAPI internal_thread_func(LPVOID param) {
63 pa_thread *t = param;
64 assert(t);
66 pa_run_once(&thread_tls_once, thread_tls_once_func);
67 pa_tls_set(thread_tls, t);
69 t->thread_func(t->userdata);
71 return 0;
74 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
75 pa_thread *t;
77 assert(thread_func);
79 t = pa_xnew(pa_thread, 1);
80 t->thread_func = thread_func;
81 t->userdata = userdata;
83 t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, NULL);
85 if (!t->thread) {
86 pa_xfree(t);
87 return NULL;
90 return t;
93 int pa_thread_is_running(pa_thread *t) {
94 DWORD code;
96 assert(t);
98 if (!GetExitCodeThread(t->thread, &code))
99 return 0;
101 return code == STILL_ACTIVE;
104 void pa_thread_free(pa_thread *t) {
105 assert(t);
107 pa_thread_join(t);
108 CloseHandle(t->thread);
109 pa_xfree(t);
112 int pa_thread_join(pa_thread *t) {
113 assert(t);
115 if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
116 return -1;
118 return 0;
121 pa_thread* pa_thread_self(void) {
122 pa_run_once(&thread_tls_once, thread_tls_once_func);
123 return pa_tls_get(thread_tls);
126 void pa_thread_yield(void) {
127 Sleep(0);
130 static DWORD WINAPI monitor_thread_func(LPVOID param) {
131 struct pa_tls_monitor *m = param;
132 assert(m);
134 WaitForSingleObject(m->thread, INFINITE);
136 CloseHandle(m->thread);
138 m->free_func(m->data);
140 pa_xfree(m);
142 return 0;
145 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
146 pa_tls *t;
148 t = pa_xnew(pa_tls, 1);
149 t->index = TlsAlloc();
150 t->free_func = free_cb;
152 if (t->index == TLS_OUT_OF_INDEXES) {
153 pa_xfree(t);
154 return NULL;
157 return t;
160 void pa_tls_free(pa_tls *t) {
161 assert(t);
163 TlsFree(t->index);
164 pa_xfree(t);
167 void *pa_tls_get(pa_tls *t) {
168 assert(t);
170 return TlsGetValue(t->index);
173 void *pa_tls_set(pa_tls *t, void *userdata) {
174 void *r;
176 assert(t);
178 r = TlsGetValue(t->index);
180 TlsSetValue(t->index, userdata);
182 if (t->free_func) {
183 struct pa_tls_monitor *m;
185 PA_ONCE_BEGIN {
186 monitor_tls = pa_tls_new(NULL);
187 assert(monitor_tls);
188 pa_tls_set(monitor_tls, NULL);
189 } PA_ONCE_END;
191 m = pa_tls_get(monitor_tls);
192 if (!m) {
193 HANDLE thread;
195 m = pa_xnew(struct pa_tls_monitor, 1);
197 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
198 GetCurrentProcess(), &m->thread, 0, FALSE,
199 DUPLICATE_SAME_ACCESS);
201 m->free_func = t->free_func;
203 pa_tls_set(monitor_tls, m);
205 thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);
206 assert(thread);
207 CloseHandle(thread);
210 m->data = userdata;
213 return r;