From 3571bf1699d1fa42b5d24fcf62eea867f0fe9903 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 1 Sep 2006 18:16:55 +0000 Subject: Thread implementation for Win32. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1356 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-win32.c | 246 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 src/pulsecore/thread-win32.c (limited to 'src/pulsecore/thread-win32.c') diff --git a/src/pulsecore/thread-win32.c b/src/pulsecore/thread-win32.c new file mode 100644 index 00000000..bdcc5b2c --- /dev/null +++ b/src/pulsecore/thread-win32.c @@ -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 +#endif + +#include + +#include + +#include +#include + +#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; +} -- cgit From f84c65ed86ae59aae6e9a48e62aca31eaa30e2e3 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 1 Sep 2006 18:39:55 +0000 Subject: Add pthread_once() equivalent support. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1357 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-win32.c | 56 ++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 31 deletions(-) (limited to 'src/pulsecore/thread-win32.c') diff --git a/src/pulsecore/thread-win32.c b/src/pulsecore/thread-win32.c index bdcc5b2c..ee61d85a 100644 --- a/src/pulsecore/thread-win32.c +++ b/src/pulsecore/thread-win32.c @@ -49,35 +49,21 @@ struct pa_tls_monitor { void *data; }; -static pa_tls *thread_tls = NULL; -static pa_tls *monitor_tls = NULL; +static pa_tls *thread_tls; +static pa_thread_once_t thread_tls_once = PA_THREAD_ONCE_INIT; +static pa_tls *monitor_tls; +static pa_thread_once_t monitor_tls_once = PA_THREAD_ONCE_INIT; 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); + thread_tls = pa_tls_new(NULL); + assert(thread_tls); } static DWORD WINAPI internal_thread_func(LPVOID param) { pa_thread *t = param; assert(t); - thread_tls_once_func(); + pa_thread_once(&thread_tls_once, thread_tls_once_func); pa_tls_set(thread_tls, t); t->thread_func(t->userdata); @@ -133,7 +119,7 @@ int pa_thread_join(pa_thread *t) { } pa_thread* pa_thread_self(void) { - thread_tls_once_func(); + pa_thread_once(&thread_tls_once, thread_tls_once_func); return pa_tls_get(thread_tls); } @@ -141,10 +127,13 @@ void pa_thread_yield(void) { Sleep(0); } -static void monitor_tls_once_func(void) { +void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func) { HANDLE mutex; char name[64]; + assert(control); + assert(once_func); + sprintf(name, "pulse%d", (int)GetCurrentProcessId()); mutex = CreateMutex(NULL, FALSE, name); @@ -152,17 +141,22 @@ static void monitor_tls_once_func(void) { WaitForSingleObject(mutex, INFINITE); - if (monitor_tls == NULL) { - monitor_tls = pa_tls_new(NULL); - assert(monitor_tls); - pa_tls_set(monitor_tls, NULL); - } - - ReleaseMutex(mutex); + if (*control == PA_THREAD_ONCE_INIT) { + *control = ~PA_THREAD_ONCE_INIT; + ReleaseMutex(mutex); + once_func(); + } else + ReleaseMutex(mutex); CloseHandle(mutex); } +static void monitor_tls_once_func(void) { + monitor_tls = pa_tls_new(NULL); + assert(monitor_tls); + pa_tls_set(monitor_tls, NULL); +} + static DWORD WINAPI monitor_thread_func(LPVOID param) { struct pa_tls_monitor *m = param; assert(m); @@ -218,7 +212,7 @@ void *pa_tls_set(pa_tls *t, void *userdata) { if (t->free_func) { struct pa_tls_monitor *m; - monitor_tls_once_func(); + pa_thread_once(&monitor_tls_once, monitor_tls_once_func); m = pa_tls_get(monitor_tls); if (!m) { -- cgit From 772645922a450f5d366bd6e077a44582e7a7b79f Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 11 Sep 2006 07:56:03 +0000 Subject: Fix up build structure for platform dependent modules. Also add implementation on Win32 for pa_once(). git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1395 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-win32.c | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) (limited to 'src/pulsecore/thread-win32.c') diff --git a/src/pulsecore/thread-win32.c b/src/pulsecore/thread-win32.c index ee61d85a..98ea0691 100644 --- a/src/pulsecore/thread-win32.c +++ b/src/pulsecore/thread-win32.c @@ -29,6 +29,7 @@ #include #include +#include #include "thread.h" @@ -50,9 +51,9 @@ struct pa_tls_monitor { }; static pa_tls *thread_tls; -static pa_thread_once_t thread_tls_once = PA_THREAD_ONCE_INIT; +static pa_once_t thread_tls_once = PA_ONCE_INIT; static pa_tls *monitor_tls; -static pa_thread_once_t monitor_tls_once = PA_THREAD_ONCE_INIT; +static pa_once_t monitor_tls_once = PA_ONCE_INIT; static void thread_tls_once_func(void) { thread_tls = pa_tls_new(NULL); @@ -63,7 +64,7 @@ static DWORD WINAPI internal_thread_func(LPVOID param) { pa_thread *t = param; assert(t); - pa_thread_once(&thread_tls_once, thread_tls_once_func); + pa_once(&thread_tls_once, thread_tls_once_func); pa_tls_set(thread_tls, t); t->thread_func(t->userdata); @@ -119,7 +120,7 @@ int pa_thread_join(pa_thread *t) { } pa_thread* pa_thread_self(void) { - pa_thread_once(&thread_tls_once, thread_tls_once_func); + pa_once(&thread_tls_once, thread_tls_once_func); return pa_tls_get(thread_tls); } @@ -127,30 +128,6 @@ void pa_thread_yield(void) { Sleep(0); } -void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func) { - HANDLE mutex; - char name[64]; - - assert(control); - assert(once_func); - - sprintf(name, "pulse%d", (int)GetCurrentProcessId()); - - mutex = CreateMutex(NULL, FALSE, name); - assert(mutex); - - WaitForSingleObject(mutex, INFINITE); - - if (*control == PA_THREAD_ONCE_INIT) { - *control = ~PA_THREAD_ONCE_INIT; - ReleaseMutex(mutex); - once_func(); - } else - ReleaseMutex(mutex); - - CloseHandle(mutex); -} - static void monitor_tls_once_func(void) { monitor_tls = pa_tls_new(NULL); assert(monitor_tls); @@ -212,7 +189,7 @@ void *pa_tls_set(pa_tls *t, void *userdata) { if (t->free_func) { struct pa_tls_monitor *m; - pa_thread_once(&monitor_tls_once, monitor_tls_once_func); + pa_once(&monitor_tls_once, monitor_tls_once_func); m = pa_tls_get(monitor_tls); if (!m) { -- cgit From 521daf6f0ac4fa6a2fbfb5d523c0c743342dca2b Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 4 Jan 2007 13:43:45 +0000 Subject: Huge trailing whitespace cleanup. Let's keep the tree pure from here on, mmmkay? git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1418 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-win32.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/pulsecore/thread-win32.c') diff --git a/src/pulsecore/thread-win32.c b/src/pulsecore/thread-win32.c index 98ea0691..38dd4dd6 100644 --- a/src/pulsecore/thread-win32.c +++ b/src/pulsecore/thread-win32.c @@ -2,17 +2,17 @@ /*** 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 -- cgit From 06211b7c8fd329137ae9003818543912a87d9898 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 13 Feb 2007 15:35:19 +0000 Subject: Add copyright notices to all relevant files. (based on svn log) git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1426 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-win32.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/pulsecore/thread-win32.c') diff --git a/src/pulsecore/thread-win32.c b/src/pulsecore/thread-win32.c index 38dd4dd6..46d273b4 100644 --- a/src/pulsecore/thread-win32.c +++ b/src/pulsecore/thread-win32.c @@ -3,6 +3,8 @@ /*** This file is part of PulseAudio. + Copyright 2006 Pierre Ossman for Cendio AB + 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, -- cgit From a67c21f093202f142438689d3f7cfbdf4ea82eea Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Oct 2007 19:13:50 +0000 Subject: merge 'lennart' branch back into trunk. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1971 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-win32.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/pulsecore/thread-win32.c') diff --git a/src/pulsecore/thread-win32.c b/src/pulsecore/thread-win32.c index 46d273b4..cad1420a 100644 --- a/src/pulsecore/thread-win32.c +++ b/src/pulsecore/thread-win32.c @@ -53,9 +53,8 @@ struct pa_tls_monitor { }; static pa_tls *thread_tls; -static pa_once_t thread_tls_once = PA_ONCE_INIT; +static pa_once thread_tls_once = PA_ONCE_INIT; static pa_tls *monitor_tls; -static pa_once_t monitor_tls_once = PA_ONCE_INIT; static void thread_tls_once_func(void) { thread_tls = pa_tls_new(NULL); @@ -66,7 +65,7 @@ static DWORD WINAPI internal_thread_func(LPVOID param) { pa_thread *t = param; assert(t); - pa_once(&thread_tls_once, thread_tls_once_func); + pa_run_once(&thread_tls_once, thread_tls_once_func); pa_tls_set(thread_tls, t); t->thread_func(t->userdata); @@ -122,7 +121,7 @@ int pa_thread_join(pa_thread *t) { } pa_thread* pa_thread_self(void) { - pa_once(&thread_tls_once, thread_tls_once_func); + pa_run_once(&thread_tls_once, thread_tls_once_func); return pa_tls_get(thread_tls); } @@ -130,12 +129,6 @@ void pa_thread_yield(void) { Sleep(0); } -static void monitor_tls_once_func(void) { - monitor_tls = pa_tls_new(NULL); - assert(monitor_tls); - pa_tls_set(monitor_tls, NULL); -} - static DWORD WINAPI monitor_thread_func(LPVOID param) { struct pa_tls_monitor *m = param; assert(m); @@ -191,7 +184,11 @@ void *pa_tls_set(pa_tls *t, void *userdata) { if (t->free_func) { struct pa_tls_monitor *m; - pa_once(&monitor_tls_once, monitor_tls_once_func); + PA_ONCE_BEGIN { + monitor_tls = pa_tls_new(NULL); + assert(monitor_tls); + pa_tls_set(monitor_tls, NULL); + } PA_ONCE_END; m = pa_tls_get(monitor_tls); if (!m) { -- cgit