4 This file is part of PulseAudio.
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 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
39 #include <pulse/utf8.h>
40 #include <pulse/xmalloc.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/native-common.h>
45 #include "core-error.h"
49 static pthread_once_t cstrerror_once
= PTHREAD_ONCE_INIT
;
50 static pthread_key_t tlsstr_key
;
52 static void inittls(void) {
55 ret
= pthread_key_create(&tlsstr_key
, pa_xfree
);
57 fprintf(stderr
, __FILE__
": CRITICAL: Unable to allocate TLS key (%d)\n", errno
);
64 static DWORD tlsstr_key
= TLS_OUT_OF_INDEXES
;
65 static DWORD monitor_key
= TLS_OUT_OF_INDEXES
;
67 static void inittls(void) {
71 sprintf(name
, "pulse%d", (int)GetCurrentProcessId());
73 mutex
= CreateMutex(NULL
, FALSE
, name
);
75 fprintf(stderr
, __FILE__
": CRITICAL: Unable to create named mutex (%d)\n", (int)GetLastError());
79 WaitForSingleObject(mutex
, INFINITE
);
81 if (tlsstr_key
== TLS_OUT_OF_INDEXES
) {
82 tlsstr_key
= TlsAlloc();
83 monitor_key
= TlsAlloc();
84 if ((tlsstr_key
== TLS_OUT_OF_INDEXES
) || (monitor_key
== TLS_OUT_OF_INDEXES
)) {
85 fprintf(stderr
, __FILE__
": CRITICAL: Unable to allocate TLS key (%d)\n", (int)GetLastError());
96 * This is incredibly brain dead, but this is necessary when dealing with
97 * the hell that is Win32.
104 static DWORD WINAPI
monitor_thread(LPVOID param
) {
105 struct monitor_data
*data
;
107 data
= (struct monitor_data
*)param
;
110 WaitForSingleObject(data
->thread
, INFINITE
);
112 CloseHandle(data
->thread
);
113 pa_xfree(data
->data
);
119 static void start_monitor(void) {
121 struct monitor_data
*data
;
123 data
= pa_xnew(struct monitor_data
, 1);
126 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
127 GetCurrentProcess(), &data
->thread
, 0, FALSE
, DUPLICATE_SAME_ACCESS
);
129 thread
= CreateThread(NULL
, 0, monitor_thread
, data
, 0, NULL
);
132 TlsSetValue(monitor_key
, data
);
139 /* Unsafe, but we have no choice */
144 const char* pa_cstrerror(int errnum
) {
147 #ifdef HAVE_STRERROR_R
154 pthread_once(&cstrerror_once
, inittls
);
156 tlsstr
= pthread_getspecific(tlsstr_key
);
157 #elif defined(HAVE_WINDOWS_H)
159 struct monitor_data
*data
;
163 tlsstr
= TlsGetValue(tlsstr_key
);
166 data
= TlsGetValue(monitor_key
);
172 #ifdef HAVE_STRERROR_R
175 origbuf
= strerror_r(errnum
, errbuf
, sizeof(errbuf
));
179 if (strerror_r(errnum
, errbuf
, sizeof(errbuf
)) == 0) {
181 errbuf
[sizeof(errbuf
) - 1] = '\0';
187 /* This might not be thread safe, but we hope for the best */
188 origbuf
= strerror(errnum
);
191 tlsstr
= pa_locale_to_utf8(origbuf
);
193 fprintf(stderr
, "Unable to convert, filtering\n");
194 tlsstr
= pa_utf8_filter(origbuf
);
198 pthread_setspecific(tlsstr_key
, tlsstr
);
199 #elif defined(HAVE_WINDOWS_H)
200 TlsSetValue(tlsstr_key
, tlsstr
);