TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / winegstreamer / glibthread.c
blob46e22f41d996fb2eaaa2bf44dd0ee997273b5603
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Wine GStreamer integration
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * gthread.c: solaris thread system implementation
8 * Copyright 1998-2001 Sebastian Wilhelmi; University of Karlsruhe
9 * Copyright 2001 Hans Breuer
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
28 * file for a list of people on the GLib Team. See the ChangeLog
29 * files for a list of changes. These files are distributed with
30 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 #include "config.h"
35 #ifdef HAVE_PTHREAD_H
36 #include <pthread.h>
37 #endif
39 #include <glib.h>
41 #include <errno.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <stdio.h>
46 #if 0
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winnls.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
54 static gchar *
55 g_win32_error_message (gint error)
57 gchar *retval;
58 WCHAR *msg = NULL;
59 int nchars;
61 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
62 |FORMAT_MESSAGE_IGNORE_INSERTS
63 |FORMAT_MESSAGE_FROM_SYSTEM,
64 NULL, error, 0,
65 (LPWSTR) &msg, 0, NULL);
66 if (msg != NULL)
68 nchars = WideCharToMultiByte(CP_UTF8, 0, msg, -1, NULL, 0, NULL, NULL);
70 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
71 msg[nchars-2] = '\0';
73 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
75 LocalFree (msg);
77 else
78 retval = g_strdup ("");
80 return retval;
83 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1] = {
84 THREAD_PRIORITY_BELOW_NORMAL,
85 THREAD_PRIORITY_NORMAL,
86 THREAD_PRIORITY_ABOVE_NORMAL,
87 THREAD_PRIORITY_HIGHEST
90 static DWORD g_thread_self_tls;
92 /* A "forward" declaration of this structure */
93 static GThreadFunctions g_thread_functions_for_glib_use_default;
95 typedef struct _GThreadData GThreadData;
96 struct _GThreadData
98 GThreadFunc func;
99 gpointer data;
100 HANDLE thread;
101 gboolean joinable;
104 static GMutex *
105 g_mutex_new_posix_impl (void)
107 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
108 pthread_mutex_init ((pthread_mutex_t *) result, NULL);
109 return result;
112 static void
113 g_mutex_free_posix_impl (GMutex * mutex)
115 pthread_mutex_destroy ((pthread_mutex_t *) mutex);
116 g_free (mutex);
119 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
120 functions from gmem.c and gmessages.c; */
122 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
123 signature and semantic are right, but without error check then!!!!,
124 we might want to change this therefore. */
126 static gboolean
127 g_mutex_trylock_posix_impl (GMutex * mutex)
129 int result;
131 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
133 if (result == EBUSY)
134 return FALSE;
136 if (result) ERR("pthread_mutex_trylock %x\n",result);
137 return TRUE;
140 static GCond *
141 g_cond_new_posix_impl (void)
143 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
144 pthread_cond_init ((pthread_cond_t *) result, NULL);
145 return result;
148 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
149 can be taken directly, as signature and semantic are right, but
150 without error check then!!!!, we might want to change this
151 therefore. */
153 #define G_NSEC_PER_SEC 1000000000
155 static gboolean
156 g_cond_timed_wait_posix_impl (GCond * cond,
157 GMutex * entered_mutex,
158 GTimeVal * abs_time)
160 int result;
161 struct timespec end_time;
162 gboolean timed_out;
164 g_return_val_if_fail (cond != NULL, FALSE);
165 g_return_val_if_fail (entered_mutex != NULL, FALSE);
167 if (!abs_time)
169 result = pthread_cond_wait ((pthread_cond_t *)cond,
170 (pthread_mutex_t *) entered_mutex);
171 timed_out = FALSE;
173 else
175 end_time.tv_sec = abs_time->tv_sec;
176 end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
178 g_return_val_if_fail (end_time.tv_nsec < G_NSEC_PER_SEC, TRUE);
180 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
181 (pthread_mutex_t *) entered_mutex,
182 &end_time);
183 timed_out = (result == ETIMEDOUT);
186 if (!timed_out)
187 if (result) ERR("pthread_cond_timedwait %x\n",result);
189 return !timed_out;
192 static void
193 g_cond_free_posix_impl (GCond * cond)
195 pthread_cond_destroy ((pthread_cond_t *) cond);
196 g_free (cond);
199 static GPrivate *
200 g_private_new_posix_impl (GDestroyNotify destructor)
202 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
203 pthread_key_create ((pthread_key_t *) result, destructor);
204 return result;
207 /* NOTE: the functions g_private_get and g_private_set may not use
208 functions from gmem.c and gmessages.c */
210 static void
211 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
213 if (!private_key)
214 return;
215 pthread_setspecific (*(pthread_key_t *) private_key, value);
218 static gpointer
219 g_private_get_posix_impl (GPrivate * private_key)
221 if (!private_key)
222 return NULL;
223 return pthread_getspecific (*(pthread_key_t *) private_key);
226 static void
227 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
229 GThreadData *target = *(GThreadData **)thread;
231 g_return_if_fail ((int)priority >= G_THREAD_PRIORITY_LOW);
232 g_return_if_fail ((int)priority <= G_THREAD_PRIORITY_URGENT);
234 SetThreadPriority (target->thread, g_thread_priority_map [priority]);
237 static void
238 g_thread_self_win32_impl (gpointer thread)
240 GThreadData *self = TlsGetValue (g_thread_self_tls);
242 if (!self)
244 /* This should only happen for the main thread! */
245 HANDLE handle = GetCurrentThread ();
246 HANDLE process = GetCurrentProcess ();
247 self = g_new (GThreadData, 1);
248 DuplicateHandle (process, handle, process, &self->thread, 0, FALSE,
249 DUPLICATE_SAME_ACCESS);
250 TlsSetValue (g_thread_self_tls, self);
251 self->func = NULL;
252 self->data = NULL;
253 self->joinable = FALSE;
256 *(GThreadData **)thread = self;
259 static void
260 g_thread_exit_win32_impl (void)
262 GThreadData *self = TlsGetValue (g_thread_self_tls);
264 if (self)
266 if (!self->joinable)
268 CloseHandle (self->thread);
269 g_free (self);
271 TlsSetValue (g_thread_self_tls, NULL);
274 ExitThread (0);
277 static guint __stdcall
278 g_thread_proxy (gpointer data)
280 GThreadData *self = (GThreadData*) data;
282 TlsSetValue (g_thread_self_tls, self);
284 self->func (self->data);
286 g_thread_exit_win32_impl ();
288 g_assert_not_reached ();
290 return 0;
293 static void
294 g_thread_create_win32_impl (GThreadFunc func,
295 gpointer data,
296 gulong stack_size,
297 gboolean joinable,
298 gboolean bound,
299 GThreadPriority priority,
300 gpointer thread,
301 GError **error)
303 guint ignore;
304 GThreadData *retval;
306 g_return_if_fail (func);
307 g_return_if_fail ((int)priority >= G_THREAD_PRIORITY_LOW);
308 g_return_if_fail ((int)priority <= G_THREAD_PRIORITY_URGENT);
310 retval = g_new(GThreadData, 1);
311 retval->func = func;
312 retval->data = data;
314 retval->joinable = joinable;
316 retval->thread = (HANDLE) CreateThread (NULL, stack_size, g_thread_proxy,
317 retval, 0, &ignore);
319 if (retval->thread == NULL)
321 gchar *win_error = g_win32_error_message (GetLastError ());
322 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
323 "Error creating thread: %s", win_error);
324 g_free (retval);
325 g_free (win_error);
326 return;
329 *(GThreadData **)thread = retval;
331 g_thread_set_priority_win32_impl (thread, priority);
334 static void
335 g_thread_yield_win32_impl (void)
337 Sleep(0);
340 static void
341 g_thread_join_win32_impl (gpointer thread)
343 GThreadData *target = *(GThreadData **)thread;
345 g_return_if_fail (target->joinable);
347 WaitForSingleObject (target->thread, INFINITE);
349 CloseHandle (target->thread);
350 g_free (target);
353 static GThreadFunctions g_thread_functions_for_glib_use_default =
355 /* Posix functions here for speed */
356 g_mutex_new_posix_impl,
357 (void (*)(GMutex *)) pthread_mutex_lock,
358 g_mutex_trylock_posix_impl,
359 (void (*)(GMutex *)) pthread_mutex_unlock,
360 g_mutex_free_posix_impl,
361 g_cond_new_posix_impl,
362 (void (*)(GCond *)) pthread_cond_signal,
363 (void (*)(GCond *)) pthread_cond_broadcast,
364 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
365 g_cond_timed_wait_posix_impl,
366 g_cond_free_posix_impl,
367 g_private_new_posix_impl,
368 g_private_get_posix_impl,
369 g_private_set_posix_impl,
370 /* win32 function required here */
371 g_thread_create_win32_impl, /* thread */
372 g_thread_yield_win32_impl,
373 g_thread_join_win32_impl,
374 g_thread_exit_win32_impl,
375 g_thread_set_priority_win32_impl,
376 g_thread_self_win32_impl,
377 NULL /* no equal function necessary */
380 void g_thread_impl_init (void)
382 static gboolean beenhere = FALSE;
384 if (beenhere)
385 return;
387 beenhere = TRUE;
389 g_thread_self_tls = TlsAlloc ();
390 g_thread_init(&g_thread_functions_for_glib_use_default);
393 #else
395 void g_thread_impl_init (void)
397 static gboolean beenhere = FALSE;
399 if (!beenhere++)
400 g_thread_init(NULL);
403 #endif