2006-09-01 18:16:55 +00:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-09-01 18:16:55 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2006-09-01 18:16:55 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-09-01 18:16:55 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-09-01 18:16:55 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
along with PulseAudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2006-09-11 07:56:03 +00:00
|
|
|
#include <pulsecore/once.h>
|
2006-09-01 18:16:55 +00:00
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
|
|
struct pa_thread {
|
|
|
|
|
HANDLE thread;
|
|
|
|
|
pa_thread_func_t thread_func;
|
|
|
|
|
void *userdata;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pa_tls {
|
|
|
|
|
DWORD index;
|
|
|
|
|
pa_free_cb_t free_func;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pa_tls_monitor {
|
|
|
|
|
HANDLE thread;
|
|
|
|
|
pa_free_cb_t free_func;
|
|
|
|
|
void *data;
|
|
|
|
|
};
|
|
|
|
|
|
2006-09-01 18:39:55 +00:00
|
|
|
static pa_tls *thread_tls;
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_once thread_tls_once = PA_ONCE_INIT;
|
2006-09-01 18:39:55 +00:00
|
|
|
static pa_tls *monitor_tls;
|
2006-09-01 18:16:55 +00:00
|
|
|
|
|
|
|
|
static void thread_tls_once_func(void) {
|
2006-09-01 18:39:55 +00:00
|
|
|
thread_tls = pa_tls_new(NULL);
|
|
|
|
|
assert(thread_tls);
|
2006-09-01 18:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DWORD WINAPI internal_thread_func(LPVOID param) {
|
|
|
|
|
pa_thread *t = param;
|
|
|
|
|
assert(t);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_run_once(&thread_tls_once, thread_tls_once_func);
|
2006-09-01 18:16:55 +00:00
|
|
|
pa_tls_set(thread_tls, t);
|
|
|
|
|
|
|
|
|
|
t->thread_func(t->userdata);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 17:07:50 +01:00
|
|
|
pa_thread* pa_thread_new(const char *name, pa_thread_func_t thread_func, void *userdata) {
|
2006-09-01 18:16:55 +00:00
|
|
|
pa_thread *t;
|
2011-01-04 17:07:50 +01:00
|
|
|
DWORD thread_id;
|
2006-09-01 18:16:55 +00:00
|
|
|
|
|
|
|
|
assert(thread_func);
|
|
|
|
|
|
|
|
|
|
t = pa_xnew(pa_thread, 1);
|
|
|
|
|
t->thread_func = thread_func;
|
|
|
|
|
t->userdata = userdata;
|
|
|
|
|
|
2011-01-04 17:07:50 +01:00
|
|
|
t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, &thread_id);
|
2006-09-01 18:16:55 +00:00
|
|
|
|
|
|
|
|
if (!t->thread) {
|
|
|
|
|
pa_xfree(t);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_thread_is_running(pa_thread *t) {
|
|
|
|
|
DWORD code;
|
|
|
|
|
|
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
|
|
if (!GetExitCodeThread(t->thread, &code))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return code == STILL_ACTIVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_thread_free(pa_thread *t) {
|
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
|
|
pa_thread_join(t);
|
|
|
|
|
CloseHandle(t->thread);
|
|
|
|
|
pa_xfree(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_thread_join(pa_thread *t) {
|
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
|
|
if (WaitForSingleObject(t->thread, INFINITE) == WAIT_FAILED)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_thread* pa_thread_self(void) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_run_once(&thread_tls_once, thread_tls_once_func);
|
2006-09-01 18:16:55 +00:00
|
|
|
return pa_tls_get(thread_tls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_thread_yield(void) {
|
|
|
|
|
Sleep(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DWORD WINAPI monitor_thread_func(LPVOID param) {
|
|
|
|
|
struct pa_tls_monitor *m = param;
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
|
|
WaitForSingleObject(m->thread, INFINITE);
|
|
|
|
|
|
|
|
|
|
CloseHandle(m->thread);
|
|
|
|
|
|
|
|
|
|
m->free_func(m->data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(m);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
|
|
|
|
|
pa_tls *t;
|
|
|
|
|
|
|
|
|
|
t = pa_xnew(pa_tls, 1);
|
|
|
|
|
t->index = TlsAlloc();
|
|
|
|
|
t->free_func = free_cb;
|
|
|
|
|
|
|
|
|
|
if (t->index == TLS_OUT_OF_INDEXES) {
|
|
|
|
|
pa_xfree(t);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_tls_free(pa_tls *t) {
|
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
|
|
TlsFree(t->index);
|
|
|
|
|
pa_xfree(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *pa_tls_get(pa_tls *t) {
|
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
|
|
return TlsGetValue(t->index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *pa_tls_set(pa_tls *t, void *userdata) {
|
|
|
|
|
void *r;
|
|
|
|
|
|
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
|
|
r = TlsGetValue(t->index);
|
|
|
|
|
|
|
|
|
|
TlsSetValue(t->index, userdata);
|
|
|
|
|
|
|
|
|
|
if (t->free_func) {
|
|
|
|
|
struct pa_tls_monitor *m;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
PA_ONCE_BEGIN {
|
|
|
|
|
monitor_tls = pa_tls_new(NULL);
|
|
|
|
|
assert(monitor_tls);
|
|
|
|
|
pa_tls_set(monitor_tls, NULL);
|
|
|
|
|
} PA_ONCE_END;
|
2006-09-01 18:16:55 +00:00
|
|
|
|
|
|
|
|
m = pa_tls_get(monitor_tls);
|
|
|
|
|
if (!m) {
|
|
|
|
|
HANDLE thread;
|
|
|
|
|
|
|
|
|
|
m = pa_xnew(struct pa_tls_monitor, 1);
|
|
|
|
|
|
|
|
|
|
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
|
|
|
|
GetCurrentProcess(), &m->thread, 0, FALSE,
|
|
|
|
|
DUPLICATE_SAME_ACCESS);
|
|
|
|
|
|
|
|
|
|
m->free_func = t->free_func;
|
|
|
|
|
|
|
|
|
|
pa_tls_set(monitor_tls, m);
|
|
|
|
|
|
|
|
|
|
thread = CreateThread(NULL, 0, monitor_thread_func, m, 0, NULL);
|
|
|
|
|
assert(thread);
|
|
|
|
|
CloseHandle(thread);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m->data = userdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|