mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-04 13:29:59 -05:00
Thread implementation for Win32.
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1356 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
parent
97202d1b2d
commit
3571bf1699
3 changed files with 392 additions and 3 deletions
|
|
@ -580,6 +580,7 @@ libpulsecore_la_SOURCES += \
|
||||||
pulsecore/modargs.c pulsecore/modargs.h \
|
pulsecore/modargs.c pulsecore/modargs.h \
|
||||||
pulsecore/modinfo.c pulsecore/modinfo.h \
|
pulsecore/modinfo.c pulsecore/modinfo.h \
|
||||||
pulsecore/module.c pulsecore/module.h \
|
pulsecore/module.c pulsecore/module.h \
|
||||||
|
pulsecore/mutex.h \
|
||||||
pulsecore/namereg.c pulsecore/namereg.h \
|
pulsecore/namereg.c pulsecore/namereg.h \
|
||||||
pulsecore/pid.c pulsecore/pid.h \
|
pulsecore/pid.c pulsecore/pid.h \
|
||||||
pulsecore/pipe.c pulsecore/pipe.h \
|
pulsecore/pipe.c pulsecore/pipe.h \
|
||||||
|
|
@ -602,19 +603,28 @@ libpulsecore_la_SOURCES += \
|
||||||
pulsecore/source.c pulsecore/source.h \
|
pulsecore/source.c pulsecore/source.h \
|
||||||
pulsecore/source-output.c pulsecore/source-output.h \
|
pulsecore/source-output.c pulsecore/source-output.h \
|
||||||
pulsecore/strbuf.c pulsecore/strbuf.h \
|
pulsecore/strbuf.c pulsecore/strbuf.h \
|
||||||
|
pulsecore/thread.h \
|
||||||
pulsecore/tokenizer.c pulsecore/tokenizer.h \
|
pulsecore/tokenizer.c pulsecore/tokenizer.h \
|
||||||
pulsecore/winsock.h \
|
pulsecore/winsock.h \
|
||||||
pulsecore/core-error.c pulsecore/core-error.h \
|
pulsecore/core-error.c pulsecore/core-error.h \
|
||||||
pulsecore/hook-list.c pulsecore/hook-list.h \
|
pulsecore/hook-list.c pulsecore/hook-list.h \
|
||||||
pulsecore/shm.c pulsecore/shm.h \
|
pulsecore/shm.c pulsecore/shm.h
|
||||||
pulsecore/mutex-posix.c pulsecore/mutex.h \
|
|
||||||
pulsecore/thread-posix.c pulsecore/thread.h
|
|
||||||
|
|
||||||
if OS_IS_WIN32
|
if OS_IS_WIN32
|
||||||
libpulsecore_la_SOURCES += \
|
libpulsecore_la_SOURCES += \
|
||||||
pulsecore/dllmain.c
|
pulsecore/dllmain.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if OS_IS_WIN32
|
||||||
|
libpulsecore_la_SOURCES += \
|
||||||
|
pulsecore/mutex-win32.c \
|
||||||
|
pulsecore/thread-win32.c
|
||||||
|
else
|
||||||
|
libpulsecore_la_SOURCES += \
|
||||||
|
pulsecore/mutex-posix.c \
|
||||||
|
pulsecore/thread-posix.c
|
||||||
|
endif
|
||||||
|
|
||||||
libpulsecore_la_CPPFLAGS = $(AM_CPPFLAGS) $(LIBOIL_CFLAGS)
|
libpulsecore_la_CPPFLAGS = $(AM_CPPFLAGS) $(LIBOIL_CFLAGS)
|
||||||
libpulsecore_la_LDFLAGS = -version-info $(LIBPULSECORE_VERSION_INFO)
|
libpulsecore_la_LDFLAGS = -version-info $(LIBPULSECORE_VERSION_INFO)
|
||||||
libpulsecore_la_LIBADD = $(AM_LIBADD) $(LIBLTDL) $(LIBSAMPLERATE_LIBS) $(LIBSNDFILE_LIBS) $(WINSOCK_LIBS) $(LIBOIL_LIBS) $(LIBICONV)
|
libpulsecore_la_LIBADD = $(AM_LIBADD) $(LIBLTDL) $(LIBSAMPLERATE_LIBS) $(LIBSNDFILE_LIBS) $(WINSOCK_LIBS) $(LIBOIL_LIBS) $(LIBICONV)
|
||||||
|
|
|
||||||
133
src/pulsecore/mutex-win32.c
Normal file
133
src/pulsecore/mutex-win32.c
Normal file
|
|
@ -0,0 +1,133 @@
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
This file is part of PulseAudio.
|
||||||
|
|
||||||
|
PulseAudio is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published
|
||||||
|
by the Free Software Foundation; either version 2 of the License,
|
||||||
|
or (at your option) any later version.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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 <windows.h>
|
||||||
|
|
||||||
|
#include <pulse/xmalloc.h>
|
||||||
|
#include <pulsecore/hashmap.h>
|
||||||
|
|
||||||
|
#include "mutex.h"
|
||||||
|
|
||||||
|
struct pa_mutex {
|
||||||
|
CRITICAL_SECTION mutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct pa_cond {
|
||||||
|
pa_hashmap *wait_events;
|
||||||
|
};
|
||||||
|
|
||||||
|
pa_mutex* pa_mutex_new(int recursive) {
|
||||||
|
pa_mutex *m;
|
||||||
|
|
||||||
|
m = pa_xnew(pa_mutex, 1);
|
||||||
|
|
||||||
|
InitializeCriticalSection(&m->mutex);
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_mutex_free(pa_mutex *m) {
|
||||||
|
assert(m);
|
||||||
|
|
||||||
|
DeleteCriticalSection(&m->mutex);
|
||||||
|
pa_xfree(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_mutex_lock(pa_mutex *m) {
|
||||||
|
assert(m);
|
||||||
|
|
||||||
|
EnterCriticalSection(&m->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_mutex_unlock(pa_mutex *m) {
|
||||||
|
assert(m);
|
||||||
|
|
||||||
|
LeaveCriticalSection(&m->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_cond *pa_cond_new(void) {
|
||||||
|
pa_cond *c;
|
||||||
|
|
||||||
|
c = pa_xnew(pa_cond, 1);
|
||||||
|
c->wait_events = pa_hashmap_new(NULL, NULL);
|
||||||
|
assert(c->wait_events);
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_cond_free(pa_cond *c) {
|
||||||
|
assert(c);
|
||||||
|
|
||||||
|
pa_hashmap_free(c->wait_events, NULL, NULL);
|
||||||
|
pa_xfree(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_cond_signal(pa_cond *c, int broadcast) {
|
||||||
|
assert(c);
|
||||||
|
|
||||||
|
if (pa_hashmap_size(c->wait_events) == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (broadcast)
|
||||||
|
SetEvent(pa_hashmap_get_first(c->wait_events));
|
||||||
|
else {
|
||||||
|
void *iter;
|
||||||
|
const void *key;
|
||||||
|
HANDLE event;
|
||||||
|
|
||||||
|
iter = NULL;
|
||||||
|
while (1) {
|
||||||
|
pa_hashmap_iterate(c->wait_events, &iter, &key);
|
||||||
|
if (key == NULL)
|
||||||
|
break;
|
||||||
|
event = (HANDLE)pa_hashmap_get(c->wait_events, key);
|
||||||
|
SetEvent(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pa_cond_wait(pa_cond *c, pa_mutex *m) {
|
||||||
|
HANDLE event;
|
||||||
|
|
||||||
|
assert(c);
|
||||||
|
assert(m);
|
||||||
|
|
||||||
|
event = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||||
|
assert(event);
|
||||||
|
|
||||||
|
pa_hashmap_put(c->wait_events, event, event);
|
||||||
|
|
||||||
|
pa_mutex_unlock(m);
|
||||||
|
|
||||||
|
WaitForSingleObject(event, INFINITE);
|
||||||
|
|
||||||
|
pa_mutex_lock(m);
|
||||||
|
|
||||||
|
pa_hashmap_remove(c->wait_events, event);
|
||||||
|
|
||||||
|
CloseHandle(event);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
246
src/pulsecore/thread-win32.c
Normal file
246
src/pulsecore/thread-win32.c
Normal file
|
|
@ -0,0 +1,246 @@
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/***
|
||||||
|
This file is part of PulseAudio.
|
||||||
|
|
||||||
|
PulseAudio is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published
|
||||||
|
by the Free Software Foundation; either version 2 of the License,
|
||||||
|
or (at your option) any later version.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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>
|
||||||
|
#include <pulsecore/log.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
};
|
||||||
|
|
||||||
|
static pa_tls *thread_tls = NULL;
|
||||||
|
static pa_tls *monitor_tls = NULL;
|
||||||
|
|
||||||
|
static void thread_tls_once_func(void) {
|
||||||
|
HANDLE mutex;
|
||||||
|
char name[64];
|
||||||
|
|
||||||
|
sprintf(name, "pulse%d", (int)GetCurrentProcessId());
|
||||||
|
|
||||||
|
mutex = CreateMutex(NULL, FALSE, name);
|
||||||
|
assert(mutex);
|
||||||
|
|
||||||
|
WaitForSingleObject(mutex, INFINITE);
|
||||||
|
|
||||||
|
if (thread_tls == NULL) {
|
||||||
|
thread_tls = pa_tls_new(NULL);
|
||||||
|
assert(thread_tls);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReleaseMutex(mutex);
|
||||||
|
|
||||||
|
CloseHandle(mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
static DWORD WINAPI internal_thread_func(LPVOID param) {
|
||||||
|
pa_thread *t = param;
|
||||||
|
assert(t);
|
||||||
|
|
||||||
|
thread_tls_once_func();
|
||||||
|
pa_tls_set(thread_tls, t);
|
||||||
|
|
||||||
|
t->thread_func(t->userdata);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
|
||||||
|
pa_thread *t;
|
||||||
|
|
||||||
|
assert(thread_func);
|
||||||
|
|
||||||
|
t = pa_xnew(pa_thread, 1);
|
||||||
|
t->thread_func = thread_func;
|
||||||
|
t->userdata = userdata;
|
||||||
|
|
||||||
|
t->thread = CreateThread(NULL, 0, internal_thread_func, t, 0, NULL);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
thread_tls_once_func();
|
||||||
|
return pa_tls_get(thread_tls);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_thread_yield(void) {
|
||||||
|
Sleep(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void monitor_tls_once_func(void) {
|
||||||
|
HANDLE mutex;
|
||||||
|
char name[64];
|
||||||
|
|
||||||
|
sprintf(name, "pulse%d", (int)GetCurrentProcessId());
|
||||||
|
|
||||||
|
mutex = CreateMutex(NULL, FALSE, name);
|
||||||
|
assert(mutex);
|
||||||
|
|
||||||
|
WaitForSingleObject(mutex, INFINITE);
|
||||||
|
|
||||||
|
if (monitor_tls == NULL) {
|
||||||
|
monitor_tls = pa_tls_new(NULL);
|
||||||
|
assert(monitor_tls);
|
||||||
|
pa_tls_set(monitor_tls, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReleaseMutex(mutex);
|
||||||
|
|
||||||
|
CloseHandle(mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
monitor_tls_once_func();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue