xref: /openbsd/gnu/usr.bin/gcc/gcc/gthr-win32.h (revision c87b03e5)
1*c87b03e5Sespie /* Threads compatibility routines for libgcc2 and libobjc.  */
2*c87b03e5Sespie /* Compile this one with gcc.  */
3*c87b03e5Sespie /* Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
4*c87b03e5Sespie    Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
5*c87b03e5Sespie 
6*c87b03e5Sespie This file is part of GCC.
7*c87b03e5Sespie 
8*c87b03e5Sespie GCC is free software; you can redistribute it and/or modify it under
9*c87b03e5Sespie the terms of the GNU General Public License as published by the Free
10*c87b03e5Sespie Software Foundation; either version 2, or (at your option) any later
11*c87b03e5Sespie version.
12*c87b03e5Sespie 
13*c87b03e5Sespie GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*c87b03e5Sespie WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*c87b03e5Sespie FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16*c87b03e5Sespie for more details.
17*c87b03e5Sespie 
18*c87b03e5Sespie You should have received a copy of the GNU General Public License
19*c87b03e5Sespie along with GCC; see the file COPYING.  If not, write to the Free
20*c87b03e5Sespie Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21*c87b03e5Sespie 02111-1307, USA.  */
22*c87b03e5Sespie 
23*c87b03e5Sespie /* As a special exception, if you link this library with other files,
24*c87b03e5Sespie    some of which are compiled with GCC, to produce an executable,
25*c87b03e5Sespie    this library does not by itself cause the resulting executable
26*c87b03e5Sespie    to be covered by the GNU General Public License.
27*c87b03e5Sespie    This exception does not however invalidate any other reasons why
28*c87b03e5Sespie    the executable file might be covered by the GNU General Public License.  */
29*c87b03e5Sespie 
30*c87b03e5Sespie #ifndef GCC_GTHR_WIN32_H
31*c87b03e5Sespie #define GCC_GTHR_WIN32_H
32*c87b03e5Sespie 
33*c87b03e5Sespie /* Windows32 threads specific definitions. The windows32 threading model
34*c87b03e5Sespie    does not map well into pthread-inspired gcc's threading model, and so
35*c87b03e5Sespie    there are caveats one needs to be aware of.
36*c87b03e5Sespie 
37*c87b03e5Sespie    1. The destructor supplied to __gthread_key_create is ignored for
38*c87b03e5Sespie       generic x86-win32 ports. This will certainly cause memory leaks
39*c87b03e5Sespie       due to unreclaimed eh contexts (sizeof (eh_context) is at least
40*c87b03e5Sespie       24 bytes for x86 currently).
41*c87b03e5Sespie 
42*c87b03e5Sespie       This memory leak may be significant for long-running applications
43*c87b03e5Sespie       that make heavy use of C++ EH.
44*c87b03e5Sespie 
45*c87b03e5Sespie       However, Mingw runtime (version 0.3 or newer) provides a mechanism
46*c87b03e5Sespie       to emulate pthreads key dtors; the runtime provides a special DLL,
47*c87b03e5Sespie       linked in if -mthreads option is specified, that runs the dtors in
48*c87b03e5Sespie       the reverse order of registration when each thread exits. If
49*c87b03e5Sespie       -mthreads option is not given, a stub is linked in instead of the
50*c87b03e5Sespie       DLL, which results in memory leak. Other x86-win32 ports can use
51*c87b03e5Sespie       the same technique of course to avoid the leak.
52*c87b03e5Sespie 
53*c87b03e5Sespie    2. The error codes returned are non-POSIX like, and cast into ints.
54*c87b03e5Sespie       This may cause incorrect error return due to truncation values on
55*c87b03e5Sespie       hw where sizeof (DWORD) > sizeof (int).
56*c87b03e5Sespie 
57*c87b03e5Sespie    3. We might consider using Critical Sections instead of Windows32
58*c87b03e5Sespie       mutexes for better performance, but emulating __gthread_mutex_trylock
59*c87b03e5Sespie       interface becomes more complicated (Win9x does not support
60*c87b03e5Sespie       TryEnterCriticalSectioni, while NT does).
61*c87b03e5Sespie 
62*c87b03e5Sespie    The basic framework should work well enough. In the long term, GCC
63*c87b03e5Sespie    needs to use Structured Exception Handling on Windows32.  */
64*c87b03e5Sespie 
65*c87b03e5Sespie #define __GTHREADS 1
66*c87b03e5Sespie 
67*c87b03e5Sespie #include <errno.h>
68*c87b03e5Sespie #ifdef __MINGW32__
69*c87b03e5Sespie #include <_mingw.h>
70*c87b03e5Sespie #endif
71*c87b03e5Sespie 
72*c87b03e5Sespie #ifdef _LIBOBJC
73*c87b03e5Sespie 
74*c87b03e5Sespie /* This is necessary to prevent windef.h (included from windows.h) from
75*c87b03e5Sespie    defining it's own BOOL as a typedef.  */
76*c87b03e5Sespie #ifndef __OBJC__
77*c87b03e5Sespie #define __OBJC__
78*c87b03e5Sespie #endif
79*c87b03e5Sespie #include <windows.h>
80*c87b03e5Sespie /* Now undef the windows BOOL.  */
81*c87b03e5Sespie #undef BOOL
82*c87b03e5Sespie 
83*c87b03e5Sespie /* Key structure for maintaining thread specific storage */
84*c87b03e5Sespie static DWORD	__gthread_objc_data_tls = (DWORD) -1;
85*c87b03e5Sespie 
86*c87b03e5Sespie /* Backend initialization functions */
87*c87b03e5Sespie 
88*c87b03e5Sespie /* Initialize the threads subsystem.  */
89*c87b03e5Sespie int
__gthread_objc_init_thread_system(void)90*c87b03e5Sespie __gthread_objc_init_thread_system (void)
91*c87b03e5Sespie {
92*c87b03e5Sespie   /* Initialize the thread storage key */
93*c87b03e5Sespie   if ((__gthread_objc_data_tls = TlsAlloc ()) != (DWORD) -1)
94*c87b03e5Sespie     return 0;
95*c87b03e5Sespie   else
96*c87b03e5Sespie     return -1;
97*c87b03e5Sespie }
98*c87b03e5Sespie 
99*c87b03e5Sespie /* Close the threads subsystem.  */
100*c87b03e5Sespie int
__gthread_objc_close_thread_system(void)101*c87b03e5Sespie __gthread_objc_close_thread_system (void)
102*c87b03e5Sespie {
103*c87b03e5Sespie   if (__gthread_objc_data_tls != (DWORD) -1)
104*c87b03e5Sespie     TlsFree (__gthread_objc_data_tls);
105*c87b03e5Sespie   return 0;
106*c87b03e5Sespie }
107*c87b03e5Sespie 
108*c87b03e5Sespie /* Backend thread functions */
109*c87b03e5Sespie 
110*c87b03e5Sespie /* Create a new thread of execution.  */
111*c87b03e5Sespie objc_thread_t
__gthread_objc_thread_detach(void (* func)(void * arg),void * arg)112*c87b03e5Sespie __gthread_objc_thread_detach (void (*func)(void *arg), void *arg)
113*c87b03e5Sespie {
114*c87b03e5Sespie   DWORD	thread_id = 0;
115*c87b03e5Sespie   HANDLE win32_handle;
116*c87b03e5Sespie 
117*c87b03e5Sespie   if (!(win32_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) func,
118*c87b03e5Sespie 				     arg, 0, &thread_id)))
119*c87b03e5Sespie     thread_id = 0;
120*c87b03e5Sespie 
121*c87b03e5Sespie   return (objc_thread_t) thread_id;
122*c87b03e5Sespie }
123*c87b03e5Sespie 
124*c87b03e5Sespie /* Set the current thread's priority.  */
125*c87b03e5Sespie int
__gthread_objc_thread_set_priority(int priority)126*c87b03e5Sespie __gthread_objc_thread_set_priority (int priority)
127*c87b03e5Sespie {
128*c87b03e5Sespie   int sys_priority = 0;
129*c87b03e5Sespie 
130*c87b03e5Sespie   switch (priority)
131*c87b03e5Sespie     {
132*c87b03e5Sespie     case OBJC_THREAD_INTERACTIVE_PRIORITY:
133*c87b03e5Sespie       sys_priority = THREAD_PRIORITY_NORMAL;
134*c87b03e5Sespie       break;
135*c87b03e5Sespie     default:
136*c87b03e5Sespie     case OBJC_THREAD_BACKGROUND_PRIORITY:
137*c87b03e5Sespie       sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
138*c87b03e5Sespie       break;
139*c87b03e5Sespie     case OBJC_THREAD_LOW_PRIORITY:
140*c87b03e5Sespie       sys_priority = THREAD_PRIORITY_LOWEST;
141*c87b03e5Sespie       break;
142*c87b03e5Sespie     }
143*c87b03e5Sespie 
144*c87b03e5Sespie   /* Change priority */
145*c87b03e5Sespie   if (SetThreadPriority (GetCurrentThread (), sys_priority))
146*c87b03e5Sespie     return 0;
147*c87b03e5Sespie   else
148*c87b03e5Sespie     return -1;
149*c87b03e5Sespie }
150*c87b03e5Sespie 
151*c87b03e5Sespie /* Return the current thread's priority.  */
152*c87b03e5Sespie int
__gthread_objc_thread_get_priority(void)153*c87b03e5Sespie __gthread_objc_thread_get_priority (void)
154*c87b03e5Sespie {
155*c87b03e5Sespie   int sys_priority;
156*c87b03e5Sespie 
157*c87b03e5Sespie   sys_priority = GetThreadPriority (GetCurrentThread ());
158*c87b03e5Sespie 
159*c87b03e5Sespie   switch (sys_priority)
160*c87b03e5Sespie     {
161*c87b03e5Sespie     case THREAD_PRIORITY_HIGHEST:
162*c87b03e5Sespie     case THREAD_PRIORITY_TIME_CRITICAL:
163*c87b03e5Sespie     case THREAD_PRIORITY_ABOVE_NORMAL:
164*c87b03e5Sespie     case THREAD_PRIORITY_NORMAL:
165*c87b03e5Sespie       return OBJC_THREAD_INTERACTIVE_PRIORITY;
166*c87b03e5Sespie 
167*c87b03e5Sespie     default:
168*c87b03e5Sespie     case THREAD_PRIORITY_BELOW_NORMAL:
169*c87b03e5Sespie       return OBJC_THREAD_BACKGROUND_PRIORITY;
170*c87b03e5Sespie 
171*c87b03e5Sespie     case THREAD_PRIORITY_IDLE:
172*c87b03e5Sespie     case THREAD_PRIORITY_LOWEST:
173*c87b03e5Sespie       return OBJC_THREAD_LOW_PRIORITY;
174*c87b03e5Sespie     }
175*c87b03e5Sespie 
176*c87b03e5Sespie   /* Couldn't get priority.  */
177*c87b03e5Sespie   return -1;
178*c87b03e5Sespie }
179*c87b03e5Sespie 
180*c87b03e5Sespie /* Yield our process time to another thread.  */
181*c87b03e5Sespie void
__gthread_objc_thread_yield(void)182*c87b03e5Sespie __gthread_objc_thread_yield (void)
183*c87b03e5Sespie {
184*c87b03e5Sespie   Sleep (0);
185*c87b03e5Sespie }
186*c87b03e5Sespie 
187*c87b03e5Sespie /* Terminate the current thread.  */
188*c87b03e5Sespie int
__gthread_objc_thread_exit(void)189*c87b03e5Sespie __gthread_objc_thread_exit (void)
190*c87b03e5Sespie {
191*c87b03e5Sespie   /* exit the thread */
192*c87b03e5Sespie   ExitThread (__objc_thread_exit_status);
193*c87b03e5Sespie 
194*c87b03e5Sespie   /* Failed if we reached here */
195*c87b03e5Sespie   return -1;
196*c87b03e5Sespie }
197*c87b03e5Sespie 
198*c87b03e5Sespie /* Returns an integer value which uniquely describes a thread.  */
199*c87b03e5Sespie objc_thread_t
__gthread_objc_thread_id(void)200*c87b03e5Sespie __gthread_objc_thread_id (void)
201*c87b03e5Sespie {
202*c87b03e5Sespie   return (objc_thread_t) GetCurrentThreadId ();
203*c87b03e5Sespie }
204*c87b03e5Sespie 
205*c87b03e5Sespie /* Sets the thread's local storage pointer.  */
206*c87b03e5Sespie int
__gthread_objc_thread_set_data(void * value)207*c87b03e5Sespie __gthread_objc_thread_set_data (void *value)
208*c87b03e5Sespie {
209*c87b03e5Sespie   if (TlsSetValue (__gthread_objc_data_tls, value))
210*c87b03e5Sespie     return 0;
211*c87b03e5Sespie   else
212*c87b03e5Sespie     return -1;
213*c87b03e5Sespie }
214*c87b03e5Sespie 
215*c87b03e5Sespie /* Returns the thread's local storage pointer.  */
216*c87b03e5Sespie void *
__gthread_objc_thread_get_data(void)217*c87b03e5Sespie __gthread_objc_thread_get_data (void)
218*c87b03e5Sespie {
219*c87b03e5Sespie   DWORD lasterror;
220*c87b03e5Sespie   void *ptr;
221*c87b03e5Sespie 
222*c87b03e5Sespie   lasterror = GetLastError ();
223*c87b03e5Sespie 
224*c87b03e5Sespie   ptr = TlsGetValue (__gthread_objc_data_tls);          /* Return thread data.  */
225*c87b03e5Sespie 
226*c87b03e5Sespie   SetLastError (lasterror);
227*c87b03e5Sespie 
228*c87b03e5Sespie   return ptr;
229*c87b03e5Sespie }
230*c87b03e5Sespie 
231*c87b03e5Sespie /* Backend mutex functions */
232*c87b03e5Sespie 
233*c87b03e5Sespie /* Allocate a mutex.  */
234*c87b03e5Sespie int
__gthread_objc_mutex_allocate(objc_mutex_t mutex)235*c87b03e5Sespie __gthread_objc_mutex_allocate (objc_mutex_t mutex)
236*c87b03e5Sespie {
237*c87b03e5Sespie   if ((mutex->backend = (void *) CreateMutex (NULL, 0, NULL)) == NULL)
238*c87b03e5Sespie     return -1;
239*c87b03e5Sespie   else
240*c87b03e5Sespie     return 0;
241*c87b03e5Sespie }
242*c87b03e5Sespie 
243*c87b03e5Sespie /* Deallocate a mutex.  */
244*c87b03e5Sespie int
__gthread_objc_mutex_deallocate(objc_mutex_t mutex)245*c87b03e5Sespie __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
246*c87b03e5Sespie {
247*c87b03e5Sespie   CloseHandle ((HANDLE) (mutex->backend));
248*c87b03e5Sespie   return 0;
249*c87b03e5Sespie }
250*c87b03e5Sespie 
251*c87b03e5Sespie /* Grab a lock on a mutex.  */
252*c87b03e5Sespie int
__gthread_objc_mutex_lock(objc_mutex_t mutex)253*c87b03e5Sespie __gthread_objc_mutex_lock (objc_mutex_t mutex)
254*c87b03e5Sespie {
255*c87b03e5Sespie   int status;
256*c87b03e5Sespie 
257*c87b03e5Sespie   status = WaitForSingleObject ((HANDLE) (mutex->backend), INFINITE);
258*c87b03e5Sespie   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
259*c87b03e5Sespie     return -1;
260*c87b03e5Sespie   else
261*c87b03e5Sespie     return 0;
262*c87b03e5Sespie }
263*c87b03e5Sespie 
264*c87b03e5Sespie /* Try to grab a lock on a mutex.  */
265*c87b03e5Sespie int
__gthread_objc_mutex_trylock(objc_mutex_t mutex)266*c87b03e5Sespie __gthread_objc_mutex_trylock (objc_mutex_t mutex)
267*c87b03e5Sespie {
268*c87b03e5Sespie   int status;
269*c87b03e5Sespie 
270*c87b03e5Sespie   status = WaitForSingleObject ((HANDLE) (mutex->backend), 0);
271*c87b03e5Sespie   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
272*c87b03e5Sespie     return -1;
273*c87b03e5Sespie   else
274*c87b03e5Sespie     return 0;
275*c87b03e5Sespie }
276*c87b03e5Sespie 
277*c87b03e5Sespie /* Unlock the mutex */
278*c87b03e5Sespie int
__gthread_objc_mutex_unlock(objc_mutex_t mutex)279*c87b03e5Sespie __gthread_objc_mutex_unlock (objc_mutex_t mutex)
280*c87b03e5Sespie {
281*c87b03e5Sespie   if (ReleaseMutex ((HANDLE) (mutex->backend)) == 0)
282*c87b03e5Sespie     return -1;
283*c87b03e5Sespie   else
284*c87b03e5Sespie     return 0;
285*c87b03e5Sespie }
286*c87b03e5Sespie 
287*c87b03e5Sespie /* Backend condition mutex functions */
288*c87b03e5Sespie 
289*c87b03e5Sespie /* Allocate a condition.  */
290*c87b03e5Sespie int
__gthread_objc_condition_allocate(objc_condition_t condition)291*c87b03e5Sespie __gthread_objc_condition_allocate (objc_condition_t condition)
292*c87b03e5Sespie {
293*c87b03e5Sespie   /* Unimplemented.  */
294*c87b03e5Sespie   return -1;
295*c87b03e5Sespie }
296*c87b03e5Sespie 
297*c87b03e5Sespie /* Deallocate a condition.  */
298*c87b03e5Sespie int
__gthread_objc_condition_deallocate(objc_condition_t condition)299*c87b03e5Sespie __gthread_objc_condition_deallocate (objc_condition_t condition)
300*c87b03e5Sespie {
301*c87b03e5Sespie   /* Unimplemented.  */
302*c87b03e5Sespie   return -1;
303*c87b03e5Sespie }
304*c87b03e5Sespie 
305*c87b03e5Sespie /* Wait on the condition */
306*c87b03e5Sespie int
__gthread_objc_condition_wait(objc_condition_t condition,objc_mutex_t mutex)307*c87b03e5Sespie __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
308*c87b03e5Sespie {
309*c87b03e5Sespie   /* Unimplemented.  */
310*c87b03e5Sespie   return -1;
311*c87b03e5Sespie }
312*c87b03e5Sespie 
313*c87b03e5Sespie /* Wake up all threads waiting on this condition.  */
314*c87b03e5Sespie int
__gthread_objc_condition_broadcast(objc_condition_t condition)315*c87b03e5Sespie __gthread_objc_condition_broadcast (objc_condition_t condition)
316*c87b03e5Sespie {
317*c87b03e5Sespie   /* Unimplemented.  */
318*c87b03e5Sespie   return -1;
319*c87b03e5Sespie }
320*c87b03e5Sespie 
321*c87b03e5Sespie /* Wake up one thread waiting on this condition.  */
322*c87b03e5Sespie int
__gthread_objc_condition_signal(objc_condition_t condition)323*c87b03e5Sespie __gthread_objc_condition_signal (objc_condition_t condition)
324*c87b03e5Sespie {
325*c87b03e5Sespie   /* Unimplemented.  */
326*c87b03e5Sespie   return -1;
327*c87b03e5Sespie }
328*c87b03e5Sespie 
329*c87b03e5Sespie #else /* _LIBOBJC */
330*c87b03e5Sespie 
331*c87b03e5Sespie #ifdef __cplusplus
332*c87b03e5Sespie extern "C" {
333*c87b03e5Sespie #endif
334*c87b03e5Sespie 
335*c87b03e5Sespie typedef unsigned long __gthread_key_t;
336*c87b03e5Sespie 
337*c87b03e5Sespie typedef struct {
338*c87b03e5Sespie   int done;
339*c87b03e5Sespie   long started;
340*c87b03e5Sespie } __gthread_once_t;
341*c87b03e5Sespie 
342*c87b03e5Sespie typedef void* __gthread_mutex_t;
343*c87b03e5Sespie 
344*c87b03e5Sespie #define __GTHREAD_ONCE_INIT {0, -1}
345*c87b03e5Sespie #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
346*c87b03e5Sespie #define __GTHREAD_MUTEX_INIT_DEFAULT 0
347*c87b03e5Sespie 
348*c87b03e5Sespie #if __MINGW32_MAJOR_VERSION >= 1 || \
349*c87b03e5Sespie   (__MINGW32_MAJOR_VERSION == 0 && __MINGW32_MINOR_VERSION > 2)
350*c87b03e5Sespie #define MINGW32_SUPPORTS_MT_EH 1
351*c87b03e5Sespie /* Mingw runtime >= v0.3 provides a magic variable that is set to nonzero
352*c87b03e5Sespie    if -mthreads option was specified, or 0 otherwise. This is to get around
353*c87b03e5Sespie    the lack of weak symbols in PE-COFF.  */
354*c87b03e5Sespie extern int _CRT_MT;
355*c87b03e5Sespie extern int __mingwthr_key_dtor (unsigned long, void (*) (void *));
356*c87b03e5Sespie #endif /* __MINGW32__ version */
357*c87b03e5Sespie 
358*c87b03e5Sespie static inline int
__gthread_active_p(void)359*c87b03e5Sespie __gthread_active_p (void)
360*c87b03e5Sespie {
361*c87b03e5Sespie #ifdef MINGW32_SUPPORTS_MT_EH
362*c87b03e5Sespie   return _CRT_MT;
363*c87b03e5Sespie #else
364*c87b03e5Sespie   return 1;
365*c87b03e5Sespie #endif
366*c87b03e5Sespie }
367*c87b03e5Sespie 
368*c87b03e5Sespie #ifdef __GTHREAD_HIDE_WIN32API
369*c87b03e5Sespie 
370*c87b03e5Sespie /* The implementations are in config/i386/gthr-win32.c in libgcc.a.
371*c87b03e5Sespie    Only stubs are exposed to avoid polluting the C++ namespace with
372*c87b03e5Sespie    windows api definitions.  */
373*c87b03e5Sespie 
374*c87b03e5Sespie extern int __gthr_win32_once (__gthread_once_t *, void (*) (void));
375*c87b03e5Sespie extern int __gthr_win32_key_create (__gthread_key_t *, void (*) (void*));
376*c87b03e5Sespie extern int __gthr_win32_key_delete (__gthread_key_t);
377*c87b03e5Sespie extern void * __gthr_win32_getspecific (__gthread_key_t);
378*c87b03e5Sespie extern int __gthr_win32_setspecific (__gthread_key_t, const void *);
379*c87b03e5Sespie extern void __gthr_win32_mutex_init_function (__gthread_mutex_t *);
380*c87b03e5Sespie extern int __gthr_win32_mutex_lock (__gthread_mutex_t *);
381*c87b03e5Sespie extern int __gthr_win32_mutex_trylock (__gthread_mutex_t *);
382*c87b03e5Sespie extern int __gthr_win32_mutex_unlock (__gthread_mutex_t *);
383*c87b03e5Sespie 
384*c87b03e5Sespie static inline int
__gthread_once(__gthread_once_t * once,void (* func)(void))385*c87b03e5Sespie __gthread_once (__gthread_once_t *once, void (*func) (void))
386*c87b03e5Sespie {
387*c87b03e5Sespie   if (__gthread_active_p ())
388*c87b03e5Sespie     return __gthr_win32_once (once, func);
389*c87b03e5Sespie   else
390*c87b03e5Sespie     return -1;
391*c87b03e5Sespie }
392*c87b03e5Sespie 
393*c87b03e5Sespie static inline int
__gthread_key_create(__gthread_key_t * key,void (* dtor)(void *))394*c87b03e5Sespie __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
395*c87b03e5Sespie {
396*c87b03e5Sespie   return __gthr_win32_key_create (key, dtor);
397*c87b03e5Sespie }
398*c87b03e5Sespie 
399*c87b03e5Sespie static inline int
__gthread_key_dtor(__gthread_key_t key,void * ptr)400*c87b03e5Sespie __gthread_key_dtor (__gthread_key_t key, void *ptr)
401*c87b03e5Sespie {
402*c87b03e5Sespie   /* Nothing needed.  */
403*c87b03e5Sespie   return 0;
404*c87b03e5Sespie }
405*c87b03e5Sespie 
406*c87b03e5Sespie static inline int
__gthread_key_delete(__gthread_key_t key)407*c87b03e5Sespie __gthread_key_delete (__gthread_key_t key)
408*c87b03e5Sespie {
409*c87b03e5Sespie   return __gthr_win32_key_delete (key);
410*c87b03e5Sespie }
411*c87b03e5Sespie 
412*c87b03e5Sespie static inline void *
__gthread_getspecific(__gthread_key_t key)413*c87b03e5Sespie __gthread_getspecific (__gthread_key_t key)
414*c87b03e5Sespie {
415*c87b03e5Sespie   return __gthr_win32_getspecific (key);
416*c87b03e5Sespie }
417*c87b03e5Sespie 
418*c87b03e5Sespie static inline int
__gthread_setspecific(__gthread_key_t key,const void * ptr)419*c87b03e5Sespie __gthread_setspecific (__gthread_key_t key, const void *ptr)
420*c87b03e5Sespie {
421*c87b03e5Sespie   return __gthr_win32_setspecific (key, ptr);
422*c87b03e5Sespie }
423*c87b03e5Sespie 
424*c87b03e5Sespie static inline void
__gthread_mutex_init_function(__gthread_mutex_t * mutex)425*c87b03e5Sespie __gthread_mutex_init_function (__gthread_mutex_t *mutex)
426*c87b03e5Sespie {
427*c87b03e5Sespie   __gthr_win32_mutex_init_function (mutex);
428*c87b03e5Sespie }
429*c87b03e5Sespie 
430*c87b03e5Sespie static inline int
__gthread_mutex_lock(__gthread_mutex_t * mutex)431*c87b03e5Sespie __gthread_mutex_lock (__gthread_mutex_t *mutex)
432*c87b03e5Sespie {
433*c87b03e5Sespie   if (__gthread_active_p ())
434*c87b03e5Sespie     return __gthr_win32_mutex_lock (mutex);
435*c87b03e5Sespie   else
436*c87b03e5Sespie     return 0;
437*c87b03e5Sespie }
438*c87b03e5Sespie 
439*c87b03e5Sespie static inline int
__gthread_mutex_trylock(__gthread_mutex_t * mutex)440*c87b03e5Sespie __gthread_mutex_trylock (__gthread_mutex_t *mutex)
441*c87b03e5Sespie {
442*c87b03e5Sespie   if (__gthread_active_p ())
443*c87b03e5Sespie     return __gthr_win32_mutex_trylock (mutex);
444*c87b03e5Sespie   else
445*c87b03e5Sespie     return 0;
446*c87b03e5Sespie }
447*c87b03e5Sespie 
448*c87b03e5Sespie static inline int
__gthread_mutex_unlock(__gthread_mutex_t * mutex)449*c87b03e5Sespie __gthread_mutex_unlock (__gthread_mutex_t *mutex)
450*c87b03e5Sespie {
451*c87b03e5Sespie   if (__gthread_active_p ())
452*c87b03e5Sespie     return __gthr_win32_mutex_unlock (mutex);
453*c87b03e5Sespie   else
454*c87b03e5Sespie     return 0;
455*c87b03e5Sespie }
456*c87b03e5Sespie 
457*c87b03e5Sespie #else /* ! __GTHREAD_HIDE_WIN32API */
458*c87b03e5Sespie 
459*c87b03e5Sespie #include <windows.h>
460*c87b03e5Sespie #include <errno.h>
461*c87b03e5Sespie 
462*c87b03e5Sespie static inline int
__gthread_once(__gthread_once_t * once,void (* func)(void))463*c87b03e5Sespie __gthread_once (__gthread_once_t *once, void (*func) (void))
464*c87b03e5Sespie {
465*c87b03e5Sespie   if (! __gthread_active_p ())
466*c87b03e5Sespie     return -1;
467*c87b03e5Sespie   else if (once == NULL || func == NULL)
468*c87b03e5Sespie     return EINVAL;
469*c87b03e5Sespie 
470*c87b03e5Sespie   if (! once->done)
471*c87b03e5Sespie     {
472*c87b03e5Sespie       if (InterlockedIncrement (&(once->started)) == 0)
473*c87b03e5Sespie 	{
474*c87b03e5Sespie 	  (*func) ();
475*c87b03e5Sespie 	  once->done = TRUE;
476*c87b03e5Sespie 	}
477*c87b03e5Sespie       else
478*c87b03e5Sespie 	{
479*c87b03e5Sespie 	  /* Another thread is currently executing the code, so wait for it
480*c87b03e5Sespie 	     to finish; yield the CPU in the meantime.  If performance
481*c87b03e5Sespie 	     does become an issue, the solution is to use an Event that
482*c87b03e5Sespie 	     we wait on here (and set above), but that implies a place to
483*c87b03e5Sespie 	     create the event before this routine is called.  */
484*c87b03e5Sespie 	  while (! once->done)
485*c87b03e5Sespie 	    Sleep (0);
486*c87b03e5Sespie 	}
487*c87b03e5Sespie     }
488*c87b03e5Sespie 
489*c87b03e5Sespie   return 0;
490*c87b03e5Sespie }
491*c87b03e5Sespie 
492*c87b03e5Sespie /* Windows32 thread local keys don't support destructors; this leads to
493*c87b03e5Sespie    leaks, especially in threaded applications making extensive use of
494*c87b03e5Sespie    C++ EH. Mingw uses a thread-support DLL to work-around this problem.  */
495*c87b03e5Sespie static inline int
__gthread_key_create(__gthread_key_t * key,void (* dtor)(void *))496*c87b03e5Sespie __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
497*c87b03e5Sespie {
498*c87b03e5Sespie   int status = 0;
499*c87b03e5Sespie   DWORD tls_index = TlsAlloc ();
500*c87b03e5Sespie   if (tls_index != 0xFFFFFFFF)
501*c87b03e5Sespie     {
502*c87b03e5Sespie       *key = tls_index;
503*c87b03e5Sespie #ifdef MINGW32_SUPPORTS_MT_EH
504*c87b03e5Sespie       /* Mingw runtime will run the dtors in reverse order for each thread
505*c87b03e5Sespie          when the thread exits.  */
506*c87b03e5Sespie       status = __mingwthr_key_dtor (*key, dtor);
507*c87b03e5Sespie #endif
508*c87b03e5Sespie     }
509*c87b03e5Sespie   else
510*c87b03e5Sespie     status = (int) GetLastError ();
511*c87b03e5Sespie   return status;
512*c87b03e5Sespie }
513*c87b03e5Sespie 
514*c87b03e5Sespie /* Currently, this routine is called only for Mingw runtime, and if
515*c87b03e5Sespie    -mthreads option is chosen to link in the thread support DLL.  */
516*c87b03e5Sespie static inline int
__gthread_key_dtor(__gthread_key_t key,void * ptr)517*c87b03e5Sespie __gthread_key_dtor (__gthread_key_t key, void *ptr)
518*c87b03e5Sespie {
519*c87b03e5Sespie   /* Nothing needed.  */
520*c87b03e5Sespie   return 0;
521*c87b03e5Sespie }
522*c87b03e5Sespie 
523*c87b03e5Sespie static inline int
__gthread_key_delete(__gthread_key_t key)524*c87b03e5Sespie __gthread_key_delete (__gthread_key_t key)
525*c87b03e5Sespie {
526*c87b03e5Sespie   return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
527*c87b03e5Sespie }
528*c87b03e5Sespie 
529*c87b03e5Sespie static inline void *
__gthread_getspecific(__gthread_key_t key)530*c87b03e5Sespie __gthread_getspecific (__gthread_key_t key)
531*c87b03e5Sespie {
532*c87b03e5Sespie   DWORD lasterror;
533*c87b03e5Sespie   void *ptr;
534*c87b03e5Sespie 
535*c87b03e5Sespie   lasterror = GetLastError ();
536*c87b03e5Sespie 
537*c87b03e5Sespie   ptr = TlsGetValue (key);
538*c87b03e5Sespie 
539*c87b03e5Sespie   SetLastError (lasterror);
540*c87b03e5Sespie 
541*c87b03e5Sespie   return ptr;
542*c87b03e5Sespie }
543*c87b03e5Sespie 
544*c87b03e5Sespie static inline int
__gthread_setspecific(__gthread_key_t key,const void * ptr)545*c87b03e5Sespie __gthread_setspecific (__gthread_key_t key, const void *ptr)
546*c87b03e5Sespie {
547*c87b03e5Sespie   return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError ();
548*c87b03e5Sespie }
549*c87b03e5Sespie 
550*c87b03e5Sespie static inline void
__gthread_mutex_init_function(__gthread_mutex_t * mutex)551*c87b03e5Sespie __gthread_mutex_init_function (__gthread_mutex_t *mutex)
552*c87b03e5Sespie {
553*c87b03e5Sespie   /* Create unnamed mutex with default security attr and no initial owner.  */
554*c87b03e5Sespie   *mutex = CreateMutex (NULL, 0, NULL);
555*c87b03e5Sespie }
556*c87b03e5Sespie 
557*c87b03e5Sespie static inline int
__gthread_mutex_lock(__gthread_mutex_t * mutex)558*c87b03e5Sespie __gthread_mutex_lock (__gthread_mutex_t *mutex)
559*c87b03e5Sespie {
560*c87b03e5Sespie   int status = 0;
561*c87b03e5Sespie 
562*c87b03e5Sespie   if (__gthread_active_p ())
563*c87b03e5Sespie     {
564*c87b03e5Sespie       if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
565*c87b03e5Sespie 	status = 0;
566*c87b03e5Sespie       else
567*c87b03e5Sespie 	status = 1;
568*c87b03e5Sespie     }
569*c87b03e5Sespie   return status;
570*c87b03e5Sespie }
571*c87b03e5Sespie 
572*c87b03e5Sespie static inline int
__gthread_mutex_trylock(__gthread_mutex_t * mutex)573*c87b03e5Sespie __gthread_mutex_trylock (__gthread_mutex_t *mutex)
574*c87b03e5Sespie {
575*c87b03e5Sespie   int status = 0;
576*c87b03e5Sespie 
577*c87b03e5Sespie   if (__gthread_active_p ())
578*c87b03e5Sespie     {
579*c87b03e5Sespie       if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
580*c87b03e5Sespie 	status = 0;
581*c87b03e5Sespie       else
582*c87b03e5Sespie 	status = 1;
583*c87b03e5Sespie     }
584*c87b03e5Sespie   return status;
585*c87b03e5Sespie }
586*c87b03e5Sespie 
587*c87b03e5Sespie static inline int
__gthread_mutex_unlock(__gthread_mutex_t * mutex)588*c87b03e5Sespie __gthread_mutex_unlock (__gthread_mutex_t *mutex)
589*c87b03e5Sespie {
590*c87b03e5Sespie   if (__gthread_active_p ())
591*c87b03e5Sespie     return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
592*c87b03e5Sespie   else
593*c87b03e5Sespie     return 0;
594*c87b03e5Sespie }
595*c87b03e5Sespie 
596*c87b03e5Sespie #endif /*  __GTHREAD_HIDE_WIN32API */
597*c87b03e5Sespie 
598*c87b03e5Sespie #ifdef __cplusplus
599*c87b03e5Sespie }
600*c87b03e5Sespie #endif
601*c87b03e5Sespie 
602*c87b03e5Sespie #endif /* _LIBOBJC */
603*c87b03e5Sespie 
604*c87b03e5Sespie #endif /* ! GCC_GTHR_WIN32_H */
605