Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulsecore / thread-win32.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29
30 #include <windows.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/once.h>
35
36 #include "thread.h"
37
38 struct pa_thread {
39     HANDLE thread;
40     pa_thread_func_t thread_func;
41     void *userdata;
42 };
43
44 struct pa_tls {
45     DWORD index;
46     pa_free_cb_t free_func;
47 };
48
49 struct pa_tls_monitor {
50     HANDLE thread;
51     pa_free_cb_t free_func;
52     void *data;
53 };
54
55 static pa_tls *thread_tls;
56 static pa_once_t thread_tls_once = PA_ONCE_INIT;
57 static pa_tls *monitor_tls;
58 static pa_once_t monitor_tls_once = PA_ONCE_INIT;
59
60 static void thread_tls_once_func(void) {
61     thread_tls = pa_tls_new(NULL);
62     assert(thread_tls);
63 }
64
65 static DWORD WINAPI internal_thread_func(LPVOID param) {
66     pa_thread *t = param;
67     assert(t);
68
69     pa_once(&thread_tls_once, thread_tls_once_func);
70     pa_tls_set(thread_tls, t);
71
72     t->thread_func(t->userdata);
73
74     return 0;
75 }
76
77 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
78     pa_thread *t;
79
80     assert(thread_func);
81
82     t = pa_xnew(pa_thread, 1);
83     t->thread_func = thread_func;
84     t->userdata = userdata;
85
86     t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, NULL);
87
88     if (!t->thread) {
89         pa_xfree(t);
90         return NULL;
91     }
92
93     return t;
94 }
95
96 int pa_thread_is_running(pa_thread *t) {
97     DWORD code;
98
99     assert(t);
100
101     if (!GetExitCodeThread(t->thread, &code))
102         return 0;
103
104     return code == STILL_ACTIVE;
105 }
106
107 void pa_thread_free(pa_thread *t) {
108     assert(t);
109
110     pa_thread_join(t);
111     CloseHandle(t->thread);
112     pa_xfree(t);
113 }
114
115 int pa_thread_join(pa_thread *t) {
116     assert(t);
117
118     if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
119         return -1;
120
121     return 0;
122 }
123
124 pa_thread* pa_thread_self(void) {
125     pa_once(&thread_tls_once, thread_tls_once_func);
126     return pa_tls_get(thread_tls);
127 }
128
129 void pa_thread_yield(void) {
130     Sleep(0);
131 }
132
133 static void monitor_tls_once_func(void) {
134     monitor_tls = pa_tls_new(NULL);
135     assert(monitor_tls);
136     pa_tls_set(monitor_tls, NULL);
137 }
138
139 static DWORD WINAPI monitor_thread_func(LPVOID param) {
140     struct pa_tls_monitor *m = param;
141     assert(m);
142
143     WaitForSingleObject(m->thread, INFINITE);
144
145     CloseHandle(m->thread);
146
147     m->free_func(m->data);
148
149     pa_xfree(m);
150
151     return 0;
152 }
153
154 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
155     pa_tls *t;
156
157     t = pa_xnew(pa_tls, 1);
158     t->index = TlsAlloc();
159     t->free_func = free_cb;
160
161     if (t->index == TLS_OUT_OF_INDEXES) {
162         pa_xfree(t);
163         return NULL;
164     }
165
166     return t;
167 }
168
169 void pa_tls_free(pa_tls *t) {
170     assert(t);
171
172     TlsFree(t->index);
173     pa_xfree(t);
174 }
175
176 void *pa_tls_get(pa_tls *t) {
177     assert(t);
178
179     return TlsGetValue(t->index);
180 }
181
182 void *pa_tls_set(pa_tls *t, void *userdata) {
183     void *r;
184
185     assert(t);
186
187     r = TlsGetValue(t->index);
188
189     TlsSetValue(t->index, userdata);
190
191     if (t->free_func) {
192         struct pa_tls_monitor *m;
193
194         pa_once(&monitor_tls_once, monitor_tls_once_func);
195
196         m = pa_tls_get(monitor_tls);
197         if (!m) {
198             HANDLE thread;
199
200             m = pa_xnew(struct pa_tls_monitor, 1);
201
202             DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
203                 GetCurrentProcess(), &m->thread, 0, FALSE,
204                 DUPLICATE_SAME_ACCESS);
205
206             m->free_func = t->free_func;
207
208             pa_tls_set(monitor_tls, m);
209
210             thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);
211             assert(thread);
212             CloseHandle(thread);
213         }
214
215         m->data = userdata;
216     }
217
218     return r;
219 }