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/.
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer
);
55 g_win32_error_message (gint error
)
61 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
62 |FORMAT_MESSAGE_IGNORE_INSERTS
63 |FORMAT_MESSAGE_FROM_SYSTEM
,
65 (LPWSTR
) &msg
, 0, 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')
73 retval
= g_utf16_to_utf8 (msg
, -1, NULL
, NULL
, NULL
);
78 retval
= g_strdup ("");
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
;
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
);
113 g_mutex_free_posix_impl (GMutex
* mutex
)
115 pthread_mutex_destroy ((pthread_mutex_t
*) 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. */
127 g_mutex_trylock_posix_impl (GMutex
* mutex
)
131 result
= pthread_mutex_trylock ((pthread_mutex_t
*) mutex
);
136 if (result
) ERR("pthread_mutex_trylock %x\n",result
);
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
);
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
153 #define G_NSEC_PER_SEC 1000000000
156 g_cond_timed_wait_posix_impl (GCond
* cond
,
157 GMutex
* entered_mutex
,
161 struct timespec end_time
;
164 g_return_val_if_fail (cond
!= NULL
, FALSE
);
165 g_return_val_if_fail (entered_mutex
!= NULL
, FALSE
);
169 result
= pthread_cond_wait ((pthread_cond_t
*)cond
,
170 (pthread_mutex_t
*) entered_mutex
);
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
,
183 timed_out
= (result
== ETIMEDOUT
);
187 if (result
) ERR("pthread_cond_timedwait %x\n",result
);
193 g_cond_free_posix_impl (GCond
* cond
)
195 pthread_cond_destroy ((pthread_cond_t
*) cond
);
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
);
207 /* NOTE: the functions g_private_get and g_private_set may not use
208 functions from gmem.c and gmessages.c */
211 g_private_set_posix_impl (GPrivate
* private_key
, gpointer value
)
215 pthread_setspecific (*(pthread_key_t
*) private_key
, value
);
219 g_private_get_posix_impl (GPrivate
* private_key
)
223 return pthread_getspecific (*(pthread_key_t
*) private_key
);
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
]);
238 g_thread_self_win32_impl (gpointer thread
)
240 GThreadData
*self
= TlsGetValue (g_thread_self_tls
);
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
);
253 self
->joinable
= FALSE
;
256 *(GThreadData
**)thread
= self
;
260 g_thread_exit_win32_impl (void)
262 GThreadData
*self
= TlsGetValue (g_thread_self_tls
);
268 CloseHandle (self
->thread
);
271 TlsSetValue (g_thread_self_tls
, NULL
);
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 ();
294 g_thread_create_win32_impl (GThreadFunc func
,
299 GThreadPriority priority
,
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);
314 retval
->joinable
= joinable
;
316 retval
->thread
= (HANDLE
) CreateThread (NULL
, stack_size
, g_thread_proxy
,
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
);
329 *(GThreadData
**)thread
= retval
;
331 g_thread_set_priority_win32_impl (thread
, priority
);
335 g_thread_yield_win32_impl (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
);
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
;
389 g_thread_self_tls
= TlsAlloc ();
390 g_thread_init(&g_thread_functions_for_glib_use_default
);
395 void g_thread_impl_init (void)
397 static gboolean beenhere
= FALSE
;