atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[wine] / dlls / winegstreamer / glibthread.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * Wine Gstramer integration
5  * Copyright 2010 Aric Stewart, CodeWeavers
6  *
7  * gthread.c: solaris thread system implementation
8  * Copyright 1998-2001 Sebastian Wilhelmi; University of Karlsruhe
9  * Copyright 2001 Hans Breuer
10  *
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.
15  *
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.
20  *
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
24  */
25
26 /*
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/.
31  */
32
33 #include "config.h"
34
35 #ifdef HAVE_PTHREAD_H
36 #include <pthread.h>
37 #endif
38
39 #include <glib.h>
40
41 #include <errno.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winnls.h"
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
52
53 static gchar *
54 g_win32_error_message (gint error)
55 {
56   gchar *retval;
57   WCHAR *msg = NULL;
58   int nchars;
59
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)
66     {
67       nchars = WideCharToMultiByte(CP_UTF8, 0, msg, -1, NULL, 0, NULL, NULL);
68
69       if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
70           msg[nchars-2] = '\0';
71
72       retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
73
74       LocalFree (msg);
75     }
76   else
77     retval = g_strdup ("");
78
79   return retval;
80 }
81
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
87 };
88
89 static DWORD g_thread_self_tls;
90
91 /* A "forward" declaration of this structure */
92 static GThreadFunctions g_thread_functions_for_glib_use_default;
93
94 typedef struct _GThreadData GThreadData;
95 struct _GThreadData
96 {
97   GThreadFunc func;
98   gpointer data;
99   HANDLE thread;
100   gboolean joinable;
101 };
102
103 static GMutex *
104 g_mutex_new_posix_impl (void)
105 {
106   GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
107   pthread_mutex_init ((pthread_mutex_t *) result, NULL);
108   return result;
109 }
110
111 static void
112 g_mutex_free_posix_impl (GMutex * mutex)
113 {
114   pthread_mutex_destroy ((pthread_mutex_t *) mutex);
115   g_free (mutex);
116 }
117
118 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
119    functions from gmem.c and gmessages.c; */
120
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. */
124
125 static gboolean
126 g_mutex_trylock_posix_impl (GMutex * mutex)
127 {
128   int result;
129
130   result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
131
132   if (result == EBUSY)
133     return FALSE;
134
135   if (result) ERR("pthread_mutex_trylock %x\n",result);
136   return TRUE;
137 }
138
139 static GCond *
140 g_cond_new_posix_impl (void)
141 {
142   GCond *result = (GCond *) g_new (pthread_cond_t, 1);
143   pthread_cond_init ((pthread_cond_t *) result, NULL);
144   return result;
145 }
146
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. */
151
152 #define G_NSEC_PER_SEC 1000000000
153
154 static gboolean
155 g_cond_timed_wait_posix_impl (GCond * cond,
156                               GMutex * entered_mutex,
157                               GTimeVal * abs_time)
158 {
159   int result;
160   struct timespec end_time;
161   gboolean timed_out;
162
163   g_return_val_if_fail (cond != NULL, FALSE);
164   g_return_val_if_fail (entered_mutex != NULL, FALSE);
165
166   if (!abs_time)
167     {
168       result = pthread_cond_wait ((pthread_cond_t *)cond,
169                                   (pthread_mutex_t *) entered_mutex);
170       timed_out = FALSE;
171     }
172   else
173     {
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);
176
177       g_return_val_if_fail (end_time.tv_nsec < G_NSEC_PER_SEC, TRUE);
178
179       result = pthread_cond_timedwait ((pthread_cond_t *) cond,
180                                        (pthread_mutex_t *) entered_mutex,
181                                        &end_time);
182       timed_out = (result == ETIMEDOUT);
183     }
184
185   if (!timed_out)
186     if (result) ERR("pthread_cond_timedwait %x\n",result);
187
188   return !timed_out;
189 }
190
191 static void
192 g_cond_free_posix_impl (GCond * cond)
193 {
194   pthread_cond_destroy ((pthread_cond_t *) cond);
195   g_free (cond);
196 }
197
198 static GPrivate *
199 g_private_new_posix_impl (GDestroyNotify destructor)
200 {
201   GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
202   pthread_key_create ((pthread_key_t *) result, destructor);
203   return result;
204 }
205
206 /* NOTE: the functions g_private_get and g_private_set may not use
207    functions from gmem.c and gmessages.c */
208
209 static void
210 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
211 {
212   if (!private_key)
213     return;
214   pthread_setspecific (*(pthread_key_t *) private_key, value);
215 }
216
217 static gpointer
218 g_private_get_posix_impl (GPrivate * private_key)
219 {
220   if (!private_key)
221     return NULL;
222   return pthread_getspecific (*(pthread_key_t *) private_key);
223 }
224
225 static void
226 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
227 {
228   GThreadData *target = *(GThreadData **)thread;
229
230   g_return_if_fail ((int)priority >= G_THREAD_PRIORITY_LOW);
231   g_return_if_fail ((int)priority <= G_THREAD_PRIORITY_URGENT);
232
233   SetThreadPriority (target->thread, g_thread_priority_map [priority]);
234 }
235
236 static void
237 g_thread_self_win32_impl (gpointer thread)
238 {
239   GThreadData *self = TlsGetValue (g_thread_self_tls);
240
241   if (!self)
242     {
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;
253     }
254
255   *(GThreadData **)thread = self;
256 }
257
258 static void
259 g_thread_exit_win32_impl (void)
260 {
261   GThreadData *self = TlsGetValue (g_thread_self_tls);
262
263   if (self)
264     {
265       if (!self->joinable)
266       {
267         CloseHandle (self->thread);
268         g_free (self);
269       }
270       TlsSetValue (g_thread_self_tls, NULL);
271     }
272
273   ExitThread (0);
274 }
275
276 static guint __stdcall
277 g_thread_proxy (gpointer data)
278 {
279   GThreadData *self = (GThreadData*) data;
280
281   TlsSetValue (g_thread_self_tls, self);
282
283   self->func (self->data);
284
285   g_thread_exit_win32_impl ();
286
287   g_assert_not_reached ();
288
289   return 0;
290 }
291
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)
301 {
302   guint ignore;
303   GThreadData *retval;
304
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);
308
309   retval = g_new(GThreadData, 1);
310   retval->func = func;
311   retval->data = data;
312
313   retval->joinable = joinable;
314
315   retval->thread = (HANDLE) CreateThread (NULL, stack_size, g_thread_proxy,
316                                           retval, 0, &ignore);
317
318   if (retval->thread == NULL)
319     {
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;
326     }
327
328   *(GThreadData **)thread = retval;
329
330   g_thread_set_priority_win32_impl (thread, priority);
331 }
332
333 static void
334 g_thread_yield_win32_impl (void)
335 {
336   Sleep(0);
337 }
338
339 static void
340 g_thread_join_win32_impl (gpointer thread)
341 {
342   GThreadData *target = *(GThreadData **)thread;
343
344   g_return_if_fail (target->joinable);
345
346   WaitForSingleObject (target->thread, INFINITE);
347
348   CloseHandle (target->thread);
349   g_free (target);
350 }
351
352 static GThreadFunctions g_thread_functions_for_glib_use_default =
353 {
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 */
377 };
378
379 void g_thread_impl_init (void)
380 {
381   static gboolean beenhere = FALSE;
382
383   if (beenhere)
384     return;
385
386   beenhere = TRUE;
387
388   g_thread_self_tls = TlsAlloc ();
389   g_thread_init(&g_thread_functions_for_glib_use_default);
390 }