[polypaudio-commits] r947 - /trunk/src/polyp/error.c
svnmailer-noreply at 0pointer.de
svnmailer-noreply at 0pointer.de
Mon May 22 09:47:27 PDT 2006
Author: ossman
Date: Mon May 22 18:47:26 2006
New Revision: 947
URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=947&root=polypaudio&view=rev
Log:
Fix TLS on Win32 to something a bit more safe and portable (compiler-wise).
Modified:
trunk/src/polyp/error.c
Modified: trunk/src/polyp/error.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/polyp/error.c?rev=947&root=polypaudio&r1=946&r2=947&view=diff
==============================================================================
--- trunk/src/polyp/error.c (original)
+++ trunk/src/polyp/error.c Mon May 22 18:47:26 2006
@@ -90,7 +90,78 @@
#elif HAVE_WINDOWS_H
-static __declspec(thread) char *tlsstr;
+static DWORD tlsstr_key = TLS_OUT_OF_INDEXES;
+static DWORD monitor_key = TLS_OUT_OF_INDEXES;
+
+static void inittls(void) {
+ HANDLE mutex;
+ char name[64];
+
+ sprintf(name, "polypaudio%d", (int)GetCurrentProcessId());
+
+ mutex = CreateMutex(NULL, FALSE, name);
+ if (!mutex) {
+ fprintf(stderr, __FILE__ ": CRITICAL: Unable to create named mutex (%d)\n", (int)GetLastError());
+ exit(-1);
+ }
+
+ WaitForSingleObject(mutex, INFINITE);
+
+ if (tlsstr_key == TLS_OUT_OF_INDEXES) {
+ tlsstr_key = TlsAlloc();
+ monitor_key = TlsAlloc();
+ if ((tlsstr_key == TLS_OUT_OF_INDEXES) || (monitor_key == TLS_OUT_OF_INDEXES)) {
+ fprintf(stderr, __FILE__ ": CRITICAL: Unable to allocate TLS key (%d)\n", (int)GetLastError());
+ exit(-1);
+ }
+ }
+
+ ReleaseMutex(mutex);
+
+ CloseHandle(mutex);
+}
+
+/*
+ * This is incredibly brain dead, but this is necessary when dealing with
+ * the hell that is Win32.
+ */
+struct monitor_data {
+ HANDLE thread;
+ void *data;
+};
+
+static DWORD WINAPI monitor_thread(LPVOID param) {
+ struct monitor_data *data;
+
+ data = (struct monitor_data*)param;
+ assert(data);
+
+ WaitForSingleObject(data->thread, INFINITE);
+
+ CloseHandle(data->thread);
+ pa_xfree(data->data);
+ pa_xfree(data);
+
+ return 0;
+}
+
+static void start_monitor(void) {
+ HANDLE thread;
+ struct monitor_data *data;
+
+ data = pa_xnew(struct monitor_data, 1);
+ assert(data);
+
+ DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
+ GetCurrentProcess(), &data->thread, 0, FALSE, DUPLICATE_SAME_ACCESS);
+
+ thread = CreateThread(NULL, 0, monitor_thread, data, 0, NULL);
+ assert(thread);
+
+ TlsSetValue(monitor_key, data);
+
+ CloseHandle(thread);
+}
#else
@@ -112,6 +183,16 @@
pthread_once(&cstrerror_once, inittls);
tlsstr = pthread_getspecific(tlsstr_key);
+#elif defined(HAVE_WINDOWS_H)
+ char *tlsstr;
+ struct monitor_data *data;
+
+ inittls();
+
+ tlsstr = TlsGetValue(tlsstr_key);
+ if (!tlsstr)
+ start_monitor();
+ data = TlsGetValue(monitor_key);
#endif
if (tlsstr)
@@ -144,6 +225,9 @@
#ifdef HAVE_PTHREAD
pthread_setspecific(tlsstr_key, tlsstr);
+#elif defined(HAVE_WINDOWS_H)
+ TlsSetValue(tlsstr_key, tlsstr);
+ data->data = tlsstr;
#endif
return tlsstr;
More information about the pulseaudio-commits
mailing list