1 /* Threads compatibily routines for libgcc2. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
4 Contributed by Mumit Khan <khan@xraylith.wisc.edu>.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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 __gthr_win32_h
31 #define __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. This
38 will certainly cause memory leaks due to unreclaimed eh contexts
39 (sizeof (eh_context) is at least 24 bytes for x86 currently).
41 This memory leak may be significant for long-running applications
42 that make heavy use of C++ EH.
44 2. The error codes returned are non-POSIX like, and cast into ints.
45 This may cause incorrect error return due to truncation values on
46 hw where sizeof (DWORD) > sizeof (int).
48 The basic framework should work well enough. */
55 typedef DWORD __gthread_key_t
;
62 typedef HANDLE __gthread_mutex_t
;
64 #define __GTHREAD_ONCE_INIT {FALSE, -1}
65 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
74 __gthread_once (__gthread_once_t
*once
, void (*func
) ())
76 if (! __gthread_active_p ())
78 else if (once
== NULL
|| func
== NULL
)
83 if (InterlockedIncrement (&(once
->started
)) == 0)
91 /* Another thread is currently executing the code, so wait for it to
92 finish; yield the CPU in the meantime. */
100 /* Windows32 thread local keys don't support destructors; to avoid leaks,
101 we will have to figure something out in the future. */
103 __gthread_key_create (__gthread_key_t
*key
,
104 void (*dtor
) (void *) __attribute__((__unused__
)))
107 DWORD tls_index
= TlsAlloc ();
108 if (tls_index
!= 0xFFFFFFFF)
111 status
= (int) GetLastError ();
115 /* Currently, this routine is never called since win32 keys don't support
116 destructors. Hopefully we'll find a way in the future. */
118 __gthread_key_dtor (__gthread_key_t key
, void *ptr
)
122 /* Just reset the key value to zero. */
124 status
= (TlsSetValue (key
, 0) != 0) ? 0 : (int) GetLastError ();
128 /* Currently, this routine is never called since win32 keys don't support
129 destructors. Hopefully we'll find a way in the future. */
131 __gthread_key_delete (__gthread_key_t key
)
133 return (TlsFree (key
) != 0) ? 0 : (int) GetLastError ();
137 __gthread_getspecific (__gthread_key_t key
)
139 return TlsGetValue (key
);
143 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
145 return (TlsSetValue (key
, ptr
) != 0) ? 0 : (int) GetLastError ();
149 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
151 /* Create unnamed mutex with default security attr and no initial owner. */
152 *mutex
= CreateMutex (NULL
, 0, NULL
);
156 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
160 if (__gthread_active_p ())
162 if (WaitForSingleObject (*mutex
, INFINITE
) == WAIT_OBJECT_0
)
171 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
175 if (__gthread_active_p ())
177 if (WaitForSingleObject (*mutex
, 0) == WAIT_OBJECT_0
)
186 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
188 if (__gthread_active_p ())
189 return (ReleaseMutex (*mutex
) != 0) ? 0 : 1;
194 #endif /* not __gthr_win32_h */