empty G_ATOMIC_MEMORY_BARRIER() definition for !G_THREADS_ENABLED
[platform/upstream/glib.git] / gthread / gthread-win32.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gthread.c: solaris thread system implementation
5  * Copyright 1998-2001 Sebastian Wilhelmi; University of Karlsruhe
6  * Copyright 2001 Hans Breuer
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GLib Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
29  */
30
31 /* 
32  * MT safe
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <glib.h>
40
41 #define STRICT
42 #include <windows.h>
43 #undef STRICT
44
45 #include <process.h>
46 #include <stdlib.h>
47
48 #define win32_check_for_error(what) G_STMT_START{                       \
49   if (!(what))                                                          \
50     g_error ("file %s: line %d (%s): error %s during %s",               \
51              __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION,                \
52              g_win32_error_message (GetLastError ()), #what);           \
53   }G_STMT_END
54
55 #define G_MUTEX_SIZE (sizeof (gpointer))
56
57 #define PRIORITY_LOW_VALUE    THREAD_PRIORITY_BELOW_NORMAL
58 #define PRIORITY_NORMAL_VALUE THREAD_PRIORITY_NORMAL
59 #define PRIORITY_HIGH_VALUE   THREAD_PRIORITY_ABOVE_NORMAL
60 #define PRIORITY_URGENT_VALUE THREAD_PRIORITY_HIGHEST
61
62 static DWORD g_thread_self_tls;
63 static DWORD g_private_tls;
64 static DWORD g_cond_event_tls;
65 static CRITICAL_SECTION g_thread_global_spinlock;
66
67 typedef BOOL (__stdcall *GTryEnterCriticalSectionFunc) (CRITICAL_SECTION *);
68
69 static GTryEnterCriticalSectionFunc try_enter_critical_section = NULL;
70
71 /* As noted in the docs, GPrivate is a limited resource, here we take
72  * a rather low maximum to save memory, use GStaticPrivate instead. */
73 #define G_PRIVATE_MAX 16
74
75 static GDestroyNotify g_private_destructors[G_PRIVATE_MAX];
76
77 static guint g_private_next = 0;
78
79 typedef struct _GThreadData GThreadData;
80 struct _GThreadData
81 {
82   GThreadFunc func;
83   gpointer data;
84   HANDLE thread;
85   gboolean joinable;
86 };
87
88 struct _GCond 
89 {
90   GPtrArray *array;
91   CRITICAL_SECTION lock;
92 };
93
94 static GMutex *
95 g_mutex_new_win32_cs_impl (void)
96 {
97   CRITICAL_SECTION *cs = g_new (CRITICAL_SECTION, 1);
98   gpointer *retval = g_new (gpointer, 1);
99
100   InitializeCriticalSection (cs);
101   *retval = cs;
102   return (GMutex *) retval;
103 }
104
105 static void
106 g_mutex_free_win32_cs_impl (GMutex *mutex)
107 {
108   gpointer *ptr = (gpointer *) mutex;
109   CRITICAL_SECTION *cs = (CRITICAL_SECTION *) *ptr;
110
111   DeleteCriticalSection (cs);
112   g_free (cs);
113   g_free (mutex);
114 }
115
116 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
117    functions from gmem.c and gmessages.c; */
118
119 static void
120 g_mutex_lock_win32_cs_impl (GMutex *mutex)
121 {
122   EnterCriticalSection (*(CRITICAL_SECTION **)mutex);
123 }
124
125 static gboolean
126 g_mutex_trylock_win32_cs_impl (GMutex * mutex)
127 {
128   return try_enter_critical_section (*(CRITICAL_SECTION **)mutex);
129 }
130
131 static void
132 g_mutex_unlock_win32_cs_impl (GMutex *mutex)
133 {
134   LeaveCriticalSection (*(CRITICAL_SECTION **)mutex);
135 }
136
137 static GMutex *
138 g_mutex_new_win32_impl (void)
139 {
140   HANDLE handle;
141   HANDLE *retval;
142   win32_check_for_error (handle = CreateMutex (NULL, FALSE, NULL));
143   retval = g_new (HANDLE, 1);
144   *retval = handle;
145   return (GMutex *) retval;
146 }
147
148 static void
149 g_mutex_free_win32_impl (GMutex *mutex)
150 {
151   win32_check_for_error (CloseHandle (*(HANDLE *) mutex));
152   g_free (mutex);
153 }
154
155 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
156    functions from gmem.c and gmessages.c; */
157
158 static void
159 g_mutex_lock_win32_impl (GMutex *mutex)
160 {
161   WaitForSingleObject (*(HANDLE *) mutex, INFINITE);
162 }
163
164 static gboolean
165 g_mutex_trylock_win32_impl (GMutex * mutex)
166 {
167   DWORD result;
168   win32_check_for_error (WAIT_FAILED != 
169                          (result = WaitForSingleObject (*(HANDLE *)mutex, 0)));
170   return result != WAIT_TIMEOUT;
171 }
172
173 static void
174 g_mutex_unlock_win32_impl (GMutex *mutex)
175 {
176   ReleaseMutex (*(HANDLE *) mutex);
177 }
178
179 static GCond *
180 g_cond_new_win32_impl (void)
181 {
182   GCond *retval = g_new (GCond, 1);
183
184   retval->array = g_ptr_array_new ();
185   InitializeCriticalSection (&retval->lock);
186
187   return retval;
188 }
189
190 static void
191 g_cond_signal_win32_impl (GCond * cond)
192 {
193   EnterCriticalSection (&cond->lock);
194
195   if (cond->array->len > 0)
196     {
197       SetEvent (g_ptr_array_index (cond->array, 0));
198       g_ptr_array_remove_index (cond->array, 0);
199     }
200
201   LeaveCriticalSection (&cond->lock);
202 }
203
204 static void
205 g_cond_broadcast_win32_impl (GCond * cond)
206 {
207   guint i;
208   EnterCriticalSection (&cond->lock);
209
210   for (i = 0; i < cond->array->len; i++)
211     SetEvent (g_ptr_array_index (cond->array, i));
212
213   g_ptr_array_set_size (cond->array, 0);
214   LeaveCriticalSection (&cond->lock);
215 }
216
217 static gboolean
218 g_cond_wait_internal (GCond *cond,
219                       GMutex *entered_mutex,
220                       gulong milliseconds)
221 {
222   gulong retval;
223   HANDLE event = TlsGetValue (g_cond_event_tls);
224
225   if (!event)
226     {
227       win32_check_for_error (event = CreateEvent (0, FALSE, FALSE, NULL));
228       TlsSetValue (g_cond_event_tls, event);
229     }
230
231   EnterCriticalSection (&cond->lock);
232
233   /* The event must not be signaled. Check this */
234   g_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
235
236   g_ptr_array_add (cond->array, event);
237   LeaveCriticalSection (&cond->lock);
238
239   g_mutex_unlock (entered_mutex);
240
241   win32_check_for_error (WAIT_FAILED !=
242                          (retval = WaitForSingleObject (event, milliseconds)));
243
244   g_mutex_lock (entered_mutex);
245
246   if (retval == WAIT_TIMEOUT)
247     {
248       EnterCriticalSection (&cond->lock);
249       g_ptr_array_remove (cond->array, event);
250
251       /* In the meantime we could have been signaled, so we must again
252        * wait for the signal, this time with no timeout, to reset
253        * it. retval is set again to honour the late arrival of the
254        * signal */
255       win32_check_for_error (WAIT_FAILED != 
256                              (retval = WaitForSingleObject (event, 0)));
257
258       LeaveCriticalSection (&cond->lock);
259     }
260
261 #ifndef G_DISABLE_ASSERT
262   EnterCriticalSection (&cond->lock);
263
264   /* Now event must not be inside the array, check this */
265   g_assert (g_ptr_array_remove (cond->array, event) == FALSE);
266
267   LeaveCriticalSection (&cond->lock);
268 #endif /* !G_DISABLE_ASSERT */
269
270   return retval != WAIT_TIMEOUT;
271 }
272
273 static void     
274 g_cond_wait_win32_impl (GCond *cond,
275                         GMutex *entered_mutex)
276 {
277   g_return_if_fail (cond != NULL);
278   g_return_if_fail (entered_mutex != NULL);
279
280   g_cond_wait_internal (cond, entered_mutex, INFINITE);
281 }
282
283 static gboolean
284 g_cond_timed_wait_win32_impl (GCond *cond, 
285                               GMutex *entered_mutex,
286                               GTimeVal *abs_time)
287 {
288   GTimeVal current_time;
289   gulong to_wait;
290
291   g_return_val_if_fail (cond != NULL, FALSE);
292   g_return_val_if_fail (entered_mutex != NULL, FALSE);
293
294   if (!abs_time)
295     to_wait = INFINITE;
296   else
297     {
298       g_get_current_time (&current_time);
299       if (abs_time->tv_sec < current_time.tv_sec ||
300           (abs_time->tv_sec == current_time.tv_sec &&
301            abs_time->tv_usec <= current_time.tv_usec))
302         to_wait = 0;
303       else
304         to_wait = (abs_time->tv_sec - current_time.tv_sec) * 1000 +
305           (abs_time->tv_usec - current_time.tv_usec) / 1000;      
306     }
307   
308   return g_cond_wait_internal (cond, entered_mutex, to_wait);
309 }
310
311 static void
312 g_cond_free_win32_impl (GCond * cond)
313 {
314   DeleteCriticalSection (&cond->lock);
315   g_ptr_array_free (cond->array, TRUE);
316   g_free (cond);
317 }
318
319 static GPrivate *
320 g_private_new_win32_impl (GDestroyNotify destructor)
321 {
322   GPrivate *result;
323   EnterCriticalSection (&g_thread_global_spinlock);
324   if (g_private_next >= G_PRIVATE_MAX)
325     g_error ("Too many GPrivate allocated. Their number is limited to %d.\n"
326              "Use GStaticPrivate instead.\n", G_PRIVATE_MAX);
327   g_private_destructors[g_private_next] = destructor;
328   result = GUINT_TO_POINTER (g_private_next);
329   g_private_next++;
330   LeaveCriticalSection (&g_thread_global_spinlock);
331
332   return result;
333 }
334
335 /* NOTE: the functions g_private_get and g_private_set may not use
336    functions from gmem.c and gmessages.c */
337
338 static void
339 g_private_set_win32_impl (GPrivate * private_key, gpointer value)
340 {
341   gpointer* array = TlsGetValue (g_private_tls);
342   guint index = GPOINTER_TO_UINT (private_key);
343
344   if (index >= G_PRIVATE_MAX)
345       return;
346
347   if (!array)
348     {
349       array = (gpointer*) calloc (G_PRIVATE_MAX, sizeof (gpointer));
350       TlsSetValue (g_private_tls, array);
351     }
352
353   array[index] = value;
354 }
355
356 static gpointer
357 g_private_get_win32_impl (GPrivate * private_key)
358 {
359   gpointer* array = TlsGetValue (g_private_tls);
360   guint index = GPOINTER_TO_UINT (private_key);
361
362   if (index >= G_PRIVATE_MAX || !array)
363     return NULL;
364
365   return array[index];
366 }
367
368 static void
369 g_thread_set_priority_win32_impl (gpointer thread, GThreadPriority priority)
370 {
371   GThreadData *target = *(GThreadData **)thread;
372
373   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
374   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
375
376   win32_check_for_error (SetThreadPriority (target->thread,  
377                                             g_thread_priority_map [priority]));
378 }
379
380 static void
381 g_thread_self_win32_impl (gpointer thread)
382 {  
383   GThreadData *self = TlsGetValue (g_thread_self_tls);
384
385   if (!self)
386     {
387       /* This should only happen for the main thread! */
388       HANDLE handle = GetCurrentThread ();
389       HANDLE process = GetCurrentProcess ();
390       self = g_new (GThreadData, 1);
391       win32_check_for_error (DuplicateHandle (process, handle, process, 
392                                               &self->thread, 0, FALSE, 
393                                               DUPLICATE_SAME_ACCESS));
394       win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
395       self->func = NULL;
396       self->data = NULL;      
397       self->joinable = FALSE;
398     }
399
400   *(GThreadData **)thread = self;
401 }
402
403 static void 
404 g_thread_exit_win32_impl (void)
405 {
406   GThreadData *self = TlsGetValue (g_thread_self_tls);
407   guint i, private_max;
408   gpointer *array = TlsGetValue (g_private_tls);
409   HANDLE event = TlsGetValue (g_cond_event_tls);
410
411   EnterCriticalSection (&g_thread_global_spinlock);
412   private_max = g_private_next;
413   LeaveCriticalSection (&g_thread_global_spinlock);
414
415   if (array)
416     {
417       for (i = 0; i < private_max; i++)
418         {
419           GDestroyNotify destructor = g_private_destructors[i];
420           GDestroyNotify data = array[i];
421           if (destructor && data)
422             destructor (data);
423         }
424
425       g_free (array);
426
427       win32_check_for_error (TlsSetValue (g_private_tls, NULL));
428     }
429   
430   if (self)
431     {
432       if (!self->joinable)
433         {
434           win32_check_for_error (CloseHandle (self->thread));
435           g_free (self);
436         }
437       win32_check_for_error (TlsSetValue (g_thread_self_tls, NULL));
438     }
439
440   if (event)
441     {
442       CloseHandle (event);
443       win32_check_for_error (TlsSetValue (g_cond_event_tls, NULL));
444     }
445
446   _endthreadex (0);
447 }
448
449 static guint __stdcall
450 g_thread_proxy (gpointer data)
451 {
452   GThreadData *self = (GThreadData*) data;
453
454   win32_check_for_error (TlsSetValue (g_thread_self_tls, self));
455   
456   self->func (self->data);
457
458   g_thread_exit_win32_impl ();
459
460   g_assert_not_reached ();
461
462   return 0;
463 }
464
465 static void
466 g_thread_create_win32_impl (GThreadFunc func, 
467                             gpointer data, 
468                             gulong stack_size,
469                             gboolean joinable,
470                             gboolean bound,
471                             GThreadPriority priority,
472                             gpointer thread,
473                             GError **error)
474 {     
475   guint ignore;
476   GThreadData *retval;
477
478   g_return_if_fail (func);
479   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
480   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
481   
482   retval = g_new(GThreadData, 1);
483   retval->func = func;
484   retval->data = data;
485   
486   retval->joinable = joinable;
487
488   retval->thread = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_proxy, 
489                                             retval, 0, &ignore);
490
491   if (retval->thread == NULL)
492     {
493       gchar *win_error = g_win32_error_message (GetLastError ());
494       g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, 
495                    "Error creating thread: %s", win_error);
496       g_free (retval);
497       g_free (win_error);
498       return;
499     }
500
501   *(GThreadData **)thread = retval;
502
503   g_thread_set_priority_win32_impl (thread, priority);
504 }
505
506 static void 
507 g_thread_yield_win32_impl (void)
508 {
509   Sleep(0);
510 }
511
512 static void
513 g_thread_join_win32_impl (gpointer thread)
514 {
515   GThreadData *target = *(GThreadData **)thread;
516
517   g_return_if_fail (target->joinable);
518
519   win32_check_for_error (WAIT_FAILED != 
520                          WaitForSingleObject (target->thread, INFINITE));
521
522   win32_check_for_error (CloseHandle (target->thread));
523   g_free (target);
524 }
525
526 static GThreadFunctions g_thread_functions_for_glib_use_default =
527 {
528   g_mutex_new_win32_impl,           /* mutex */
529   g_mutex_lock_win32_impl,
530   g_mutex_trylock_win32_impl,
531   g_mutex_unlock_win32_impl,
532   g_mutex_free_win32_impl,
533   g_cond_new_win32_impl,            /* condition */
534   g_cond_signal_win32_impl,
535   g_cond_broadcast_win32_impl,
536   g_cond_wait_win32_impl,
537   g_cond_timed_wait_win32_impl,
538   g_cond_free_win32_impl,
539   g_private_new_win32_impl,         /* private thread data */
540   g_private_get_win32_impl,
541   g_private_set_win32_impl,
542   g_thread_create_win32_impl,       /* thread */
543   g_thread_yield_win32_impl,
544   g_thread_join_win32_impl,
545   g_thread_exit_win32_impl,
546   g_thread_set_priority_win32_impl,
547   g_thread_self_win32_impl,
548   NULL                              /* no equal function necessary */
549 };
550
551 #define HAVE_G_THREAD_IMPL_INIT
552 static void
553 g_thread_impl_init ()
554 {
555   static gboolean beenhere = FALSE;
556   HMODULE kernel32;
557
558   if (beenhere)
559     return;
560
561   beenhere = TRUE;
562   
563   win32_check_for_error (TLS_OUT_OF_INDEXES != 
564                          (g_thread_self_tls = TlsAlloc ()));
565   win32_check_for_error (TLS_OUT_OF_INDEXES != 
566                          (g_private_tls = TlsAlloc ()));
567   win32_check_for_error (TLS_OUT_OF_INDEXES != 
568                          (g_cond_event_tls = TlsAlloc ()));
569   InitializeCriticalSection (&g_thread_global_spinlock);
570
571   /* Here we are looking for TryEnterCriticalSection in KERNEL32.DLL,
572    * if it is found, we can use the faster critical sections instead
573    * of mutexes. Note however that
574    * http://www2.awl.com/cseng/titles/0-201-63465-1/csmutx.htm indicates,
575    * that critical sections might not be ideal after all on SMP machines */
576   kernel32 = GetModuleHandle ("KERNEL32.DLL");
577   if (kernel32)
578     {
579       try_enter_critical_section = (GTryEnterCriticalSectionFunc)
580         GetProcAddress(kernel32, "TryEnterCriticalSection");
581       
582       /* Even if TryEnterCriticalSection is found, it is not
583        * necessarily working..., we have to check it */
584       if (try_enter_critical_section && 
585           try_enter_critical_section (&g_thread_global_spinlock))
586         {
587           LeaveCriticalSection (&g_thread_global_spinlock);
588
589           g_thread_functions_for_glib_use_default.mutex_new =
590             g_mutex_new_win32_cs_impl;
591           g_thread_functions_for_glib_use_default.mutex_lock =
592             g_mutex_lock_win32_cs_impl;
593           g_thread_functions_for_glib_use_default.mutex_trylock =
594             g_mutex_trylock_win32_cs_impl;
595           g_thread_functions_for_glib_use_default.mutex_unlock =
596             g_mutex_unlock_win32_cs_impl;
597           g_thread_functions_for_glib_use_default.mutex_free =
598             g_mutex_free_win32_cs_impl;
599         }
600     }
601 }