riched32: Don't bother to unregister classes at process exit.
[wine/wine-gecko.git] / dlls / winegstreamer / glibthread.c
blob25eceb887b13660b9a76cb6fb8672e3982ed5b65
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Wine Gstramer 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 #include "windef.h"
47 #include "winbase.h"
48 #include "winnls.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
53 static gchar *
54 g_win32_error_message (gint error)
56 gchar *retval;
57 WCHAR *msg = NULL;
58 int nchars;
60 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
61 |FORMAT_MESSAGE_IGNORE_INSERTS
62 |FORMAT_MESSAGE_FROM_SYSTEM,
63 NULL, error, 0,
64 (LPWSTR) &msg, 0, NULL);
65 if (msg != NULL)
67 nchars = WideCharToMultiByte(CP_UTF8, 0, msg, -1, NULL, 0, NULL, NULL);
69 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
70 msg[nchars-2] = '\0';
72 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
74 LocalFree (msg);
76 else
77 retval = g_strdup ("");
79 return retval;
82 static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1] = {
83 THREAD_PRIORITY_BELOW_NORMAL,
84 THREAD_PRIORITY_NORMAL,
85 THREAD_PRIORITY_ABOVE_NORMAL,
86 THREAD_PRIORITY_HIGHEST
89 static DWORD g_thread_self_tls;
91 /* A "forward" declaration of this structure */
92 static GThreadFunctions g_thread_functions_for_glib_use_default;
94 typedef struct _GThreadData GThreadData;
95 struct _GThreadData
97 GThreadFunc func;
98 gpointer data;
99 HANDLE thread;
100 gboolean joinable;
103 static GMutex *
104 g_mutex_new_posix_impl (void)
106 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
107 pthread_mutex_init ((pthread_mutex_t *) result, NULL);
108 return result;
111 static void
112 g_mutex_free_posix_impl (GMutex * mutex)
114 pthread_mutex_destroy ((pthread_mutex_t *) mutex);
115 g_free (mutex);
118 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
119 functions from gmem.c and gmessages.c; */
121 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
122 signature and semantic are right, but without error check then!!!!,
123 we might want to change this therefore. */
125 static gboolean
126 g_mutex_trylock_posix_impl (GMutex * mutex)
128 int result;
130 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
132 if (result == EBUSY)
133 return FALSE;
135 if (result) ERR("pthread_mutex_trylock %x\n",result);
136 return TRUE;
139 static GCond *
140 g_cond_new_posix_impl (void)
142 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
143 pthread_cond_init ((pthread_cond_t *) result, NULL);
144 return result;
147 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
148 can be taken directly, as signature and semantic are right, but
149 without error check then!!!!, we might want to change this
150 therefore. */
152 #define G_NSEC_PER_SEC 1000000000
154 static gboolean
155 g_cond_timed_wait_posix_impl (GCond * cond,
156 GMutex * entered_mutex,
157 GTimeVal * abs_time)
159 int result;
160 struct timespec end_time;
161 gboolean timed_out;
163 g_return_val_if_fail (cond != NULL, FALSE);
164 g_return_val_if_fail (entered_mutex != NULL, FALSE);
166 if (!abs_time)
168 result = pthread_cond_wait ((pthread_cond_t *)cond,
169 (pthread_mutex_t *) entered_mutex);
170 timed_out = FALSE;
172 else
174 end_time.tv_sec = abs_time->tv_sec;
175 end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
177 g_return_val_if_fail (end_time.tv_nsec < G_NSEC_PER_SEC, TRUE);
179 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
180 (pthread_mutex_t *) entered_mutex,
181 &end_time);
182 timed_out = (result == ETIMEDOUT);
185 if (!timed_out)
186 if (result) ERR("pthread_cond_timedwait %x\n",result);
188 return !timed_out;
191 static void
192 g_cond_free_posix_impl (GCond * cond)
194 pthread_cond_destroy ((pthread_cond_t *) cond);
195 g_free (cond);
198 static GPrivate *
199 g_private_new_posix_impl (GDestroyNotify destructor)
201 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
202 pthread_key_create ((pthread_key_t *) result, destructor);
203 return result;
206 /* NOTE: the functions g_private_get and g_private_set may not use
207 functions from gmem.c and gmessages.c */
209 static void
210 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
212 if (!private_key)
213 return;
214 pthread_setspecific (*(pthread_key_t *) private_key, value);
217 static gpointer
218 g_private_get_posix_impl (GPrivate * private_key)
220 if (!private_key)
221 return NULL;
222 return pthread_getspecific (*(pthread_key_t *) private_key);
225 static void
226 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
228 GThreadData *target = *(GThreadData **)thread;
230 g_return_if_fail ((int)priority >= G_THREAD_PRIORITY_LOW);
231 g_return_if_fail ((int)priority <= G_THREAD_PRIORITY_URGENT);
233 SetThreadPriority (target->thread, g_thread_priority_map [priority]);
236 static void
237 g_thread_self_win32_impl (gpointer thread)
239 GThreadData *self = TlsGetValue (g_thread_self_tls);
241 if (!self)
243 /* This should only happen for the main thread! */
244 HANDLE handle = GetCurrentThread ();
245 HANDLE process = GetCurrentProcess ();
246 self = g_new (GThreadData, 1);
247 DuplicateHandle (process, handle, process, &self->thread, 0, FALSE,
248 DUPLICATE_SAME_ACCESS);
249 TlsSetValue (g_thread_self_tls, self);
250 self->func = NULL;
251 self->data = NULL;
252 self->joinable = FALSE;
255 *(GThreadData **)thread = self;
258 static void
259 g_thread_exit_win32_impl (void)
261 GThreadData *self = TlsGetValue (g_thread_self_tls);
263 if (self)
265 if (!self->joinable)
267 CloseHandle (self->thread);
268 g_free (self);
270 TlsSetValue (g_thread_self_tls, NULL);
273 ExitThread (0);
276 static guint __stdcall
277 g_thread_proxy (gpointer data)
279 GThreadData *self = (GThreadData*) data;
281 TlsSetValue (g_thread_self_tls, self);
283 self->func (self->data);
285 g_thread_exit_win32_impl ();
287 g_assert_not_reached ();
289 return 0;
292 static void
293 g_thread_create_win32_impl (GThreadFunc func,
294 gpointer data,
295 gulong stack_size,
296 gboolean joinable,
297 gboolean bound,
298 GThreadPriority priority,
299 gpointer thread,
300 GError **error)
302 guint ignore;
303 GThreadData *retval;
305 g_return_if_fail (func);
306 g_return_if_fail ((int)priority >= G_THREAD_PRIORITY_LOW);
307 g_return_if_fail ((int)priority <= G_THREAD_PRIORITY_URGENT);
309 retval = g_new(GThreadData, 1);
310 retval->func = func;
311 retval->data = data;
313 retval->joinable = joinable;
315 retval->thread = (HANDLE) CreateThread (NULL, stack_size, g_thread_proxy,
316 retval, 0, &ignore);
318 if (retval->thread == NULL)
320 gchar *win_error = g_win32_error_message (GetLastError ());
321 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
322 "Error creating thread: %s", win_error);
323 g_free (retval);
324 g_free (win_error);
325 return;
328 *(GThreadData **)thread = retval;
330 g_thread_set_priority_win32_impl (thread, priority);
333 static void
334 g_thread_yield_win32_impl (void)
336 Sleep(0);
339 static void
340 g_thread_join_win32_impl (gpointer thread)
342 GThreadData *target = *(GThreadData **)thread;
344 g_return_if_fail (target->joinable);
346 WaitForSingleObject (target->thread, INFINITE);
348 CloseHandle (target->thread);
349 g_free (target);
352 static GThreadFunctions g_thread_functions_for_glib_use_default =
354 /* Posix functions here for speed */
355 g_mutex_new_posix_impl,
356 (void (*)(GMutex *)) pthread_mutex_lock,
357 g_mutex_trylock_posix_impl,
358 (void (*)(GMutex *)) pthread_mutex_unlock,
359 g_mutex_free_posix_impl,
360 g_cond_new_posix_impl,
361 (void (*)(GCond *)) pthread_cond_signal,
362 (void (*)(GCond *)) pthread_cond_broadcast,
363 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
364 g_cond_timed_wait_posix_impl,
365 g_cond_free_posix_impl,
366 g_private_new_posix_impl,
367 g_private_get_posix_impl,
368 g_private_set_posix_impl,
369 /* win32 function required here */
370 g_thread_create_win32_impl, /* thread */
371 g_thread_yield_win32_impl,
372 g_thread_join_win32_impl,
373 g_thread_exit_win32_impl,
374 g_thread_set_priority_win32_impl,
375 g_thread_self_win32_impl,
376 NULL /* no equal function necessary */
379 void g_thread_impl_init (void)
381 static gboolean beenhere = FALSE;
383 if (beenhere)
384 return;
386 beenhere = TRUE;
388 g_thread_self_tls = TlsAlloc ();
389 g_thread_init(&g_thread_functions_for_glib_use_default);