1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 #ifndef GCC_GTHR_WIN32_H
31 #define GCC_GTHR_WIN32_H
33 /* Windows32 threads specific definitions. The windows32 threading model
34 does not map well into pthread-inspired gcc's threading model, and so
35 there are caveats one needs to be aware of.
37 1. The destructor supplied to __gthread_key_create is ignored for
38 generic x86-win32 ports. This will certainly cause memory leaks
39 due to unreclaimed eh contexts (sizeof (eh_context) is at least
40 24 bytes for x86 currently).
42 This memory leak may be significant for long-running applications
43 that make heavy use of C++ EH.
45 However, Mingw runtime (version 0.3 or newer) provides a mechanism
46 to emulate pthreads key dtors; the runtime provides a special DLL,
47 linked in if -mthreads option is specified, that runs the dtors in
48 the reverse order of registration when each thread exits. If
49 -mthreads option is not given, a stub is linked in instead of the
50 DLL, which results in memory leak. Other x86-win32 ports can use
51 the same technique of course to avoid the leak.
53 2. The error codes returned are non-POSIX like, and cast into ints.
54 This may cause incorrect error return due to truncation values on
55 hw where sizeof (DWORD) > sizeof (int).
57 3. We are currently using a special mutex instead of the Critical
58 Sections, since Win9x does not support TryEnterCriticalSection
61 The basic framework should work well enough. In the long term, GCC
62 needs to use Structured Exception Handling on Windows32. */
73 /* This is necessary to prevent windef.h (included from windows.h) from
74 defining it's own BOOL as a typedef. */
79 /* Now undef the windows BOOL. */
82 /* Key structure for maintaining thread specific storage */
83 static DWORD __gthread_objc_data_tls
= (DWORD
) -1;
85 /* Backend initialization functions */
87 /* Initialize the threads subsystem. */
89 __gthread_objc_init_thread_system (void)
91 /* Initialize the thread storage key. */
92 if ((__gthread_objc_data_tls
= TlsAlloc ()) != (DWORD
) -1)
98 /* Close the threads subsystem. */
100 __gthread_objc_close_thread_system (void)
102 if (__gthread_objc_data_tls
!= (DWORD
) -1)
103 TlsFree (__gthread_objc_data_tls
);
107 /* Backend thread functions */
109 /* Create a new thread of execution. */
111 __gthread_objc_thread_detach (void (*func
)(void *arg
), void *arg
)
116 if (!(win32_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) func
,
117 arg
, 0, &thread_id
)))
120 return (objc_thread_t
) thread_id
;
123 /* Set the current thread's priority. */
125 __gthread_objc_thread_set_priority (int priority
)
127 int sys_priority
= 0;
131 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
132 sys_priority
= THREAD_PRIORITY_NORMAL
;
135 case OBJC_THREAD_BACKGROUND_PRIORITY
:
136 sys_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
138 case OBJC_THREAD_LOW_PRIORITY
:
139 sys_priority
= THREAD_PRIORITY_LOWEST
;
143 /* Change priority */
144 if (SetThreadPriority (GetCurrentThread (), sys_priority
))
150 /* Return the current thread's priority. */
152 __gthread_objc_thread_get_priority (void)
156 sys_priority
= GetThreadPriority (GetCurrentThread ());
158 switch (sys_priority
)
160 case THREAD_PRIORITY_HIGHEST
:
161 case THREAD_PRIORITY_TIME_CRITICAL
:
162 case THREAD_PRIORITY_ABOVE_NORMAL
:
163 case THREAD_PRIORITY_NORMAL
:
164 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
167 case THREAD_PRIORITY_BELOW_NORMAL
:
168 return OBJC_THREAD_BACKGROUND_PRIORITY
;
170 case THREAD_PRIORITY_IDLE
:
171 case THREAD_PRIORITY_LOWEST
:
172 return OBJC_THREAD_LOW_PRIORITY
;
175 /* Couldn't get priority. */
179 /* Yield our process time to another thread. */
181 __gthread_objc_thread_yield (void)
186 /* Terminate the current thread. */
188 __gthread_objc_thread_exit (void)
190 /* exit the thread */
191 ExitThread (__objc_thread_exit_status
);
193 /* Failed if we reached here */
197 /* Returns an integer value which uniquely describes a thread. */
199 __gthread_objc_thread_id (void)
201 return (objc_thread_t
) GetCurrentThreadId ();
204 /* Sets the thread's local storage pointer. */
206 __gthread_objc_thread_set_data (void *value
)
208 if (TlsSetValue (__gthread_objc_data_tls
, value
))
214 /* Returns the thread's local storage pointer. */
216 __gthread_objc_thread_get_data (void)
221 lasterror
= GetLastError ();
223 ptr
= TlsGetValue (__gthread_objc_data_tls
); /* Return thread data. */
225 SetLastError (lasterror
);
230 /* Backend mutex functions */
232 /* Allocate a mutex. */
234 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
236 if ((mutex
->backend
= (void *) CreateMutex (NULL
, 0, NULL
)) == NULL
)
242 /* Deallocate a mutex. */
244 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
246 CloseHandle ((HANDLE
) (mutex
->backend
));
250 /* Grab a lock on a mutex. */
252 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
256 status
= WaitForSingleObject ((HANDLE
) (mutex
->backend
), INFINITE
);
257 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
263 /* Try to grab a lock on a mutex. */
265 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
269 status
= WaitForSingleObject ((HANDLE
) (mutex
->backend
), 0);
270 if (status
!= WAIT_OBJECT_0
&& status
!= WAIT_ABANDONED
)
276 /* Unlock the mutex */
278 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
280 if (ReleaseMutex ((HANDLE
) (mutex
->backend
)) == 0)
286 /* Backend condition mutex functions */
288 /* Allocate a condition. */
290 __gthread_objc_condition_allocate (objc_condition_t condition
)
296 /* Deallocate a condition. */
298 __gthread_objc_condition_deallocate (objc_condition_t condition
)
304 /* Wait on the condition */
306 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
312 /* Wake up all threads waiting on this condition. */
314 __gthread_objc_condition_broadcast (objc_condition_t condition
)
320 /* Wake up one thread waiting on this condition. */
322 __gthread_objc_condition_signal (objc_condition_t condition
)
334 typedef unsigned long __gthread_key_t
;
351 } __gthread_recursive_mutex_t
;
353 #define __GTHREAD_ONCE_INIT {0, -1}
354 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
355 #define __GTHREAD_MUTEX_INIT_DEFAULT {-1, 0}
356 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION \
357 __gthread_recursive_mutex_init_function
358 #define __GTHREAD_RECURSIVE_MUTEX_INIT_DEFAULT {-1, 0, 0, 0}
360 #if __MINGW32_MAJOR_VERSION >= 1 || \
361 (__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
362 #define MINGW32_SUPPORTS_MT_EH 1
363 /* Mingw runtime >= v0.3 provides a magic variable that is set to nonzero
364 if -mthreads option was specified, or 0 otherwise. This is to get around
365 the lack of weak symbols in PE-COFF. */
367 extern int __mingwthr_key_dtor (unsigned long, void (*) (void *));
368 #endif /* __MINGW32__ version */
370 /* The Windows95 kernel does not export InterlockedCompareExchange.
371 This provides a substitute. When building apps that reference
372 gthread_mutex_try_lock, the __GTHREAD_I486_INLINE_LOCK_PRIMITIVES
373 macro must be defined if Windows95 is a target. Currently
374 gthread_mutex_try_lock is not referenced by libgcc or libstdc++. */
375 #ifdef __GTHREAD_I486_INLINE_LOCK_PRIMITIVES
377 __gthr_i486_lock_cmp_xchg(long *dest
, long xchg
, long comperand
)
380 __asm__
__volatile__ ("\n\
382 cmpxchg{l} {%4, %1|%1, %4}\n"
383 : "=a" (result
), "=m" (*dest
)
384 : "0" (comperand
), "m" (*dest
), "r" (xchg
)
388 #define __GTHR_W32_InterlockedCompareExchange __gthr_i486_lock_cmp_xchg
389 #else /* __GTHREAD_I486_INLINE_LOCK_PRIMITIVES */
390 #define __GTHR_W32_InterlockedCompareExchange InterlockedCompareExchange
391 #endif /* __GTHREAD_I486_INLINE_LOCK_PRIMITIVES */
394 __gthread_active_p (void)
396 #ifdef MINGW32_SUPPORTS_MT_EH
403 #if __GTHREAD_HIDE_WIN32API
405 /* The implementations are in config/i386/gthr-win32.c in libgcc.a.
406 Only stubs are exposed to avoid polluting the C++ namespace with
407 windows api definitions. */
409 extern int __gthr_win32_once (__gthread_once_t
*, void (*) (void));
410 extern int __gthr_win32_key_create (__gthread_key_t
*, void (*) (void*));
411 extern int __gthr_win32_key_delete (__gthread_key_t
);
412 extern void * __gthr_win32_getspecific (__gthread_key_t
);
413 extern int __gthr_win32_setspecific (__gthread_key_t
, const void *);
414 extern void __gthr_win32_mutex_init_function (__gthread_mutex_t
*);
415 extern int __gthr_win32_mutex_lock (__gthread_mutex_t
*);
416 extern int __gthr_win32_mutex_trylock (__gthread_mutex_t
*);
417 extern int __gthr_win32_mutex_unlock (__gthread_mutex_t
*);
419 __gthr_win32_recursive_mutex_init_function (__gthread_recursive_mutex_t
*);
420 extern int __gthr_win32_recursive_mutex_lock (__gthread_recursive_mutex_t
*);
422 __gthr_win32_recursive_mutex_trylock (__gthread_recursive_mutex_t
*);
423 extern int __gthr_win32_recursive_mutex_unlock (__gthread_recursive_mutex_t
*);
426 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
428 if (__gthread_active_p ())
429 return __gthr_win32_once (once
, func
);
435 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
437 return __gthr_win32_key_create (key
, dtor
);
441 __gthread_key_delete (__gthread_key_t key
)
443 return __gthr_win32_key_delete (key
);
447 __gthread_getspecific (__gthread_key_t key
)
449 return __gthr_win32_getspecific (key
);
453 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
455 return __gthr_win32_setspecific (key
, ptr
);
459 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
461 __gthr_win32_mutex_init_function (mutex
);
465 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
467 if (__gthread_active_p ())
468 return __gthr_win32_mutex_lock (mutex
);
474 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
476 if (__gthread_active_p ())
477 return __gthr_win32_mutex_trylock (mutex
);
483 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
485 if (__gthread_active_p ())
486 return __gthr_win32_mutex_unlock (mutex
);
492 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
494 __gthr_win32_recursive_mutex_init_function (mutex
);
498 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
500 if (__gthread_active_p ())
501 return __gthr_win32_recursive_mutex_lock (mutex
);
507 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
509 if (__gthread_active_p ())
510 return __gthr_win32_recursive_mutex_trylock (mutex
);
516 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
518 if (__gthread_active_p ())
519 return __gthr_win32_recursive_mutex_unlock (mutex
);
524 #else /* ! __GTHREAD_HIDE_WIN32API */
530 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
532 if (! __gthread_active_p ())
534 else if (once
== NULL
|| func
== NULL
)
539 if (InterlockedIncrement (&(once
->started
)) == 0)
546 /* Another thread is currently executing the code, so wait for it
547 to finish; yield the CPU in the meantime. If performance
548 does become an issue, the solution is to use an Event that
549 we wait on here (and set above), but that implies a place to
550 create the event before this routine is called. */
559 /* Windows32 thread local keys don't support destructors; this leads to
560 leaks, especially in threaded applications making extensive use of
561 C++ EH. Mingw uses a thread-support DLL to work-around this problem. */
563 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
566 DWORD tls_index
= TlsAlloc ();
567 if (tls_index
!= 0xFFFFFFFF)
570 #ifdef MINGW32_SUPPORTS_MT_EH
571 /* Mingw runtime will run the dtors in reverse order for each thread
572 when the thread exits. */
573 status
= __mingwthr_key_dtor (*key
, dtor
);
577 status
= (int) GetLastError ();
582 __gthread_key_delete (__gthread_key_t key
)
584 return (TlsFree (key
) != 0) ? 0 : (int) GetLastError ();
588 __gthread_getspecific (__gthread_key_t key
)
593 lasterror
= GetLastError ();
595 ptr
= TlsGetValue (key
);
597 SetLastError (lasterror
);
603 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
605 return (TlsSetValue (key
, (void*) ptr
) != 0) ? 0 : (int) GetLastError ();
609 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
612 mutex
->sema
= CreateSemaphore (NULL
, 0, 65535, NULL
);
616 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
620 if (__gthread_active_p ())
622 if (InterlockedIncrement (&mutex
->counter
) == 0 ||
623 WaitForSingleObject (mutex
->sema
, INFINITE
) == WAIT_OBJECT_0
)
627 /* WaitForSingleObject returns WAIT_FAILED, and we can only do
628 some best-effort cleanup here. */
629 InterlockedDecrement (&mutex
->counter
);
637 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
641 if (__gthread_active_p ())
643 if (__GTHR_W32_InterlockedCompareExchange (&mutex
->counter
, 0, -1) < 0)
652 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
654 if (__gthread_active_p ())
656 if (InterlockedDecrement (&mutex
->counter
) >= 0)
657 return ReleaseSemaphore (mutex
->sema
, 1, NULL
) ? 0 : 1;
663 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
668 mutex
->sema
= CreateSemaphore (NULL
, 0, 65535, NULL
);
672 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
674 if (__gthread_active_p ())
676 DWORD me
= GetCurrentThreadId();
677 if (InterlockedIncrement (&mutex
->counter
) == 0)
682 else if (mutex
->owner
== me
)
684 InterlockedDecrement (&mutex
->counter
);
687 else if (WaitForSingleObject (mutex
->sema
, INFINITE
) == WAIT_OBJECT_0
)
694 /* WaitForSingleObject returns WAIT_FAILED, and we can only do
695 some best-effort cleanup here. */
696 InterlockedDecrement (&mutex
->counter
);
704 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
706 if (__gthread_active_p ())
708 DWORD me
= GetCurrentThreadId();
709 if (__GTHR_W32_InterlockedCompareExchange (&mutex
->counter
, 0, -1) < 0)
714 else if (mutex
->owner
== me
)
723 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
725 if (__gthread_active_p ())
728 if (mutex
->depth
== 0)
732 if (InterlockedDecrement (&mutex
->counter
) >= 0)
733 return ReleaseSemaphore (mutex
->sema
, 1, NULL
) ? 0 : 1;
739 #endif /* __GTHREAD_HIDE_WIN32API */
745 #endif /* _LIBOBJC */
747 #endif /* ! GCC_GTHR_WIN32_H */