sink-input: Check flat volume with pa_sink_flat_volume_enabled().
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / pulsecore / thread-win32.c
blob7d458b977987347da31327765b429e8cb84c1dad
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.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
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(const char *name, pa_thread_func_t thread_func, void *userdata) {
75 pa_thread *t;
76 DWORD thread_id;
78 assert(thread_func);
80 t = pa_xnew(pa_thread, 1);
81 t->thread_func = thread_func;
82 t->userdata = userdata;
84 t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, &thread_id);
86 if (!t->thread) {
87 pa_xfree(t);
88 return NULL;
91 return t;
94 int pa_thread_is_running(pa_thread *t) {
95 DWORD code;
97 assert(t);
99 if (!GetExitCodeThread(t->thread, &code))
100 return 0;
102 return code == STILL_ACTIVE;
105 void pa_thread_free(pa_thread *t) {
106 assert(t);
108 pa_thread_join(t);
109 CloseHandle(t->thread);
110 pa_xfree(t);
113 int pa_thread_join(pa_thread *t) {
114 assert(t);
116 if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
117 return -1;
119 return 0;
122 pa_thread* pa_thread_self(void) {
123 pa_run_once(&thread_tls_once, thread_tls_once_func);
124 return pa_tls_get(thread_tls);
127 void pa_thread_yield(void) {
128 Sleep(0);
131 static DWORD WINAPI monitor_thread_func(LPVOID param) {
132 struct pa_tls_monitor *m = param;
133 assert(m);
135 WaitForSingleObject(m->thread, INFINITE);
137 CloseHandle(m->thread);
139 m->free_func(m->data);
141 pa_xfree(m);
143 return 0;
146 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
147 pa_tls *t;
149 t = pa_xnew(pa_tls, 1);
150 t->index = TlsAlloc();
151 t->free_func = free_cb;
153 if (t->index == TLS_OUT_OF_INDEXES) {
154 pa_xfree(t);
155 return NULL;
158 return t;
161 void pa_tls_free(pa_tls *t) {
162 assert(t);
164 TlsFree(t->index);
165 pa_xfree(t);
168 void *pa_tls_get(pa_tls *t) {
169 assert(t);
171 return TlsGetValue(t->index);
174 void *pa_tls_set(pa_tls *t, void *userdata) {
175 void *r;
177 assert(t);
179 r = TlsGetValue(t->index);
181 TlsSetValue(t->index, userdata);
183 if (t->free_func) {
184 struct pa_tls_monitor *m;
186 PA_ONCE_BEGIN {
187 monitor_tls = pa_tls_new(NULL);
188 assert(monitor_tls);
189 pa_tls_set(monitor_tls, NULL);
190 } PA_ONCE_END;
192 m = pa_tls_get(monitor_tls);
193 if (!m) {
194 HANDLE thread;
196 m = pa_xnew(struct pa_tls_monitor, 1);
198 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
199 GetCurrentProcess(), &m->thread, 0, FALSE,
200 DUPLICATE_SAME_ACCESS);
202 m->free_func = t->free_func;
204 pa_tls_set(monitor_tls, m);
206 thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);
207 assert(thread);
208 CloseHandle(thread);
211 m->data = userdata;
214 return r;