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 thread_tls_once
= PA_ONCE_INIT
;
57 static pa_tls
*monitor_tls
;
59 static void thread_tls_once_func(void) {
60 thread_tls
= pa_tls_new(NULL
);
64 static DWORD WINAPI
internal_thread_func(LPVOID param
) {
68 pa_run_once(&thread_tls_once
, thread_tls_once_func
);
69 pa_tls_set(thread_tls
, t
);
71 t
->thread_func(t
->userdata
);
76 pa_thread
* pa_thread_new(pa_thread_func_t thread_func
, void *userdata
) {
81 t
= pa_xnew(pa_thread
, 1);
82 t
->thread_func
= thread_func
;
83 t
->userdata
= userdata
;
85 t
->thread
= CreateThread(NULL
, 0, internal_thread_func
, t
, 0, NULL
);
95 int pa_thread_is_running(pa_thread
*t
) {
100 if (!GetExitCodeThread(t
->thread
, &code
))
103 return code
== STILL_ACTIVE
;
106 void pa_thread_free(pa_thread
*t
) {
110 CloseHandle(t
->thread
);
114 int pa_thread_join(pa_thread
*t
) {
117 if (WaitForSingleObject(t
->thread
, INFINITE
) == WAIT_FAILED
)
123 pa_thread
* pa_thread_self(void) {
124 pa_run_once(&thread_tls_once
, thread_tls_once_func
);
125 return pa_tls_get(thread_tls
);
128 void pa_thread_yield(void) {
132 static DWORD WINAPI
monitor_thread_func(LPVOID param
) {
133 struct pa_tls_monitor
*m
= param
;
136 WaitForSingleObject(m
->thread
, INFINITE
);
138 CloseHandle(m
->thread
);
140 m
->free_func(m
->data
);
147 pa_tls
* pa_tls_new(pa_free_cb_t free_cb
) {
150 t
= pa_xnew(pa_tls
, 1);
151 t
->index
= TlsAlloc();
152 t
->free_func
= free_cb
;
154 if (t
->index
== TLS_OUT_OF_INDEXES
) {
162 void pa_tls_free(pa_tls
*t
) {
169 void *pa_tls_get(pa_tls
*t
) {
172 return TlsGetValue(t
->index
);
175 void *pa_tls_set(pa_tls
*t
, void *userdata
) {
180 r
= TlsGetValue(t
->index
);
182 TlsSetValue(t
->index
, userdata
);
185 struct pa_tls_monitor
*m
;
188 monitor_tls
= pa_tls_new(NULL
);
190 pa_tls_set(monitor_tls
, NULL
);
193 m
= pa_tls_get(monitor_tls
);
197 m
= pa_xnew(struct pa_tls_monitor
, 1);
199 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
200 GetCurrentProcess(), &m
->thread
, 0, FALSE
,
201 DUPLICATE_SAME_ACCESS
);
203 m
->free_func
= t
->free_func
;
205 pa_tls_set(monitor_tls
, m
);
207 thread
= CreateThread(NULL
, 0, monitor_thread_func
, m
, 0, NULL
);