4 This file is part of PulseAudio.
6 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
32 #include <pulse/xmalloc.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/once.h>
40 pa_thread_func_t thread_func;
46 pa_free_cb_t free_func;
49 struct pa_tls_monitor {
51 pa_free_cb_t free_func;
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;
60 static void thread_tls_once_func(void) {
61 thread_tls = pa_tls_new(NULL);
65 static DWORD WINAPI internal_thread_func(LPVOID param) {
69 pa_once(&thread_tls_once, thread_tls_once_func);
70 pa_tls_set(thread_tls, t);
72 t->thread_func(t->userdata);
77 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
82 t = pa_xnew(pa_thread, 1);
83 t->thread_func = thread_func;
84 t->userdata = userdata;
86 t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, NULL);
96 int pa_thread_is_running(pa_thread *t) {
101 if (!GetExitCodeThread(t->thread, &code))
104 return code == STILL_ACTIVE;
107 void pa_thread_free(pa_thread *t) {
111 CloseHandle(t->thread);
115 int pa_thread_join(pa_thread *t) {
118 if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
124 pa_thread* pa_thread_self(void) {
125 pa_once(&thread_tls_once, thread_tls_once_func);
126 return pa_tls_get(thread_tls);
129 void pa_thread_yield(void) {
133 static void monitor_tls_once_func(void) {
134 monitor_tls = pa_tls_new(NULL);
136 pa_tls_set(monitor_tls, NULL);
139 static DWORD WINAPI monitor_thread_func(LPVOID param) {
140 struct pa_tls_monitor *m = param;
143 WaitForSingleObject(m->thread, INFINITE);
145 CloseHandle(m->thread);
147 m->free_func(m->data);
154 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
157 t = pa_xnew(pa_tls, 1);
158 t->index = TlsAlloc();
159 t->free_func = free_cb;
161 if (t->index == TLS_OUT_OF_INDEXES) {
169 void pa_tls_free(pa_tls *t) {
176 void *pa_tls_get(pa_tls *t) {
179 return TlsGetValue(t->index);
182 void *pa_tls_set(pa_tls *t, void *userdata) {
187 r = TlsGetValue(t->index);
189 TlsSetValue(t->index, userdata);
192 struct pa_tls_monitor *m;
194 pa_once(&monitor_tls_once, monitor_tls_once_func);
196 m = pa_tls_get(monitor_tls);
200 m = pa_xnew(struct pa_tls_monitor, 1);
202 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
203 GetCurrentProcess(), &m->thread, 0, FALSE,
204 DUPLICATE_SAME_ACCESS);
206 m->free_func = t->free_func;
208 pa_tls_set(monitor_tls, m);
210 thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);