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/.
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer
);
54 g_win32_error_message (gint error
)
60 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
61 |FORMAT_MESSAGE_IGNORE_INSERTS
62 |FORMAT_MESSAGE_FROM_SYSTEM
,
64 (LPWSTR
) &msg
, 0, 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')
72 retval
= g_utf16_to_utf8 (msg
, -1, NULL
, NULL
, NULL
);
77 retval
= g_strdup ("");
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
;
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
);
112 g_mutex_free_posix_impl (GMutex
* mutex
)
114 pthread_mutex_destroy ((pthread_mutex_t
*) 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. */
126 g_mutex_trylock_posix_impl (GMutex
* mutex
)
130 result
= pthread_mutex_trylock ((pthread_mutex_t
*) mutex
);
135 if (result
) ERR("pthread_mutex_trylock %x\n",result
);
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
);
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
152 #define G_NSEC_PER_SEC 1000000000
155 g_cond_timed_wait_posix_impl (GCond
* cond
,
156 GMutex
* entered_mutex
,
160 struct timespec end_time
;
163 g_return_val_if_fail (cond
!= NULL
, FALSE
);
164 g_return_val_if_fail (entered_mutex
!= NULL
, FALSE
);
168 result
= pthread_cond_wait ((pthread_cond_t
*)cond
,
169 (pthread_mutex_t
*) entered_mutex
);
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
,
182 timed_out
= (result
== ETIMEDOUT
);
186 if (result
) ERR("pthread_cond_timedwait %x\n",result
);
192 g_cond_free_posix_impl (GCond
* cond
)
194 pthread_cond_destroy ((pthread_cond_t
*) cond
);
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
);
206 /* NOTE: the functions g_private_get and g_private_set may not use
207 functions from gmem.c and gmessages.c */
210 g_private_set_posix_impl (GPrivate
* private_key
, gpointer value
)
214 pthread_setspecific (*(pthread_key_t
*) private_key
, value
);
218 g_private_get_posix_impl (GPrivate
* private_key
)
222 return pthread_getspecific (*(pthread_key_t
*) private_key
);
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
]);
237 g_thread_self_win32_impl (gpointer thread
)
239 GThreadData
*self
= TlsGetValue (g_thread_self_tls
);
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
);
252 self
->joinable
= FALSE
;
255 *(GThreadData
**)thread
= self
;
259 g_thread_exit_win32_impl (void)
261 GThreadData
*self
= TlsGetValue (g_thread_self_tls
);
267 CloseHandle (self
->thread
);
270 TlsSetValue (g_thread_self_tls
, NULL
);
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 ();
293 g_thread_create_win32_impl (GThreadFunc func
,
298 GThreadPriority priority
,
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);
313 retval
->joinable
= joinable
;
315 retval
->thread
= (HANDLE
) CreateThread (NULL
, stack_size
, g_thread_proxy
,
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
);
328 *(GThreadData
**)thread
= retval
;
330 g_thread_set_priority_win32_impl (thread
, priority
);
334 g_thread_yield_win32_impl (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
);
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
;
388 g_thread_self_tls
= TlsAlloc ();
389 g_thread_init(&g_thread_functions_for_glib_use_default
);