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.1 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
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/once.h>
37 pa_thread_func_t thread_func;
43 pa_free_cb_t free_func;
46 struct pa_tls_monitor {
48 pa_free_cb_t free_func;
52 static pa_tls *thread_tls;
53 static pa_once thread_tls_once = PA_ONCE_INIT;
54 static pa_tls *monitor_tls;
56 static void thread_tls_once_func(void) {
57 thread_tls = pa_tls_new(NULL);
61 static DWORD WINAPI internal_thread_func(LPVOID param) {
65 pa_run_once(&thread_tls_once, thread_tls_once_func);
66 pa_tls_set(thread_tls, t);
68 t->thread_func(t->userdata);
73 pa_thread* pa_thread_new(const char *name, pa_thread_func_t thread_func, void *userdata) {
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, &thread_id);
93 int pa_thread_is_running(pa_thread *t) {
98 if (!GetExitCodeThread(t->thread, &code))
101 return code == STILL_ACTIVE;
104 void pa_thread_free(pa_thread *t) {
108 CloseHandle(t->thread);
112 int pa_thread_join(pa_thread *t) {
115 if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
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_get_data(pa_thread *t) {
132 void pa_thread_set_data(pa_thread *t, void *userdata) {
135 t->userdata = userdata;
138 void pa_thread_set_name(pa_thread *t, const char *name) {
139 /* Not implemented */
142 const char *pa_thread_get_name(pa_thread *t) {
143 /* Not implemented */
147 void pa_thread_yield(void) {
151 static DWORD WINAPI monitor_thread_func(LPVOID param) {
152 struct pa_tls_monitor *m = param;
155 WaitForSingleObject(m->thread, INFINITE);
157 CloseHandle(m->thread);
159 m->free_func(m->data);
166 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
169 t = pa_xnew(pa_tls, 1);
170 t->index = TlsAlloc();
171 t->free_func = free_cb;
173 if (t->index == TLS_OUT_OF_INDEXES) {
181 void pa_tls_free(pa_tls *t) {
188 void *pa_tls_get(pa_tls *t) {
191 return TlsGetValue(t->index);
194 void *pa_tls_set(pa_tls *t, void *userdata) {
199 r = TlsGetValue(t->index);
201 TlsSetValue(t->index, userdata);
204 struct pa_tls_monitor *m;
207 monitor_tls = pa_tls_new(NULL);
209 pa_tls_set(monitor_tls, NULL);
212 m = pa_tls_get(monitor_tls);
216 m = pa_xnew(struct pa_tls_monitor, 1);
218 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
219 GetCurrentProcess(), &m->thread, 0, FALSE,
220 DUPLICATE_SAME_ACCESS);
222 m->free_func = t->free_func;
224 pa_tls_set(monitor_tls, m);
226 thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);