* cp-tree.h (DECL_LOCAL_FUCNTION_P): New macro.
[official-gcc.git] / gcc / gthr-win32.h
blobb9402202722cd11fd70226469ebf7bcb96c587ec
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)
11 any later version.
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. */
50 #define __GTHREADS 1
52 #include <windows.h>
53 #include <errno.h>
55 typedef DWORD __gthread_key_t;
57 typedef struct {
58 int done;
59 long started;
60 } __gthread_once_t;
62 typedef HANDLE __gthread_mutex_t;
64 #define __GTHREAD_ONCE_INIT {FALSE, -1}
65 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
67 static inline int
68 __gthread_active_p ()
70 return 1;
73 static inline int
74 __gthread_once (__gthread_once_t *once, void (*func) ())
76 if (! __gthread_active_p ())
77 return -1;
78 else if (once == NULL || func == NULL)
79 return EINVAL;
81 if (! once->done)
83 if (InterlockedIncrement (&(once->started)) == 0)
85 (*func) ();
86 once->done = TRUE;
89 else
91 /* Another thread is currently executing the code, so wait for it to
92 finish; yield the CPU in the meantime. */
93 while (! once->done)
94 Sleep (0);
97 return 0;
100 /* Windows32 thread local keys don't support destructors; to avoid leaks,
101 we will have to figure something out in the future. */
102 static inline int
103 __gthread_key_create (__gthread_key_t *key,
104 void (*dtor) (void *) __attribute__((__unused__)))
106 int status = 0;
107 DWORD tls_index = TlsAlloc ();
108 if (tls_index != 0xFFFFFFFF)
109 *key = tls_index;
110 else
111 status = (int) GetLastError ();
112 return status;
115 /* Currently, this routine is never called since win32 keys don't support
116 destructors. Hopefully we'll find a way in the future. */
117 static inline int
118 __gthread_key_dtor (__gthread_key_t key, void *ptr)
120 int status = 0;
122 /* Just reset the key value to zero. */
123 if (ptr)
124 status = (TlsSetValue (key, 0) != 0) ? 0 : (int) GetLastError ();
125 return status;
128 /* Currently, this routine is never called since win32 keys don't support
129 destructors. Hopefully we'll find a way in the future. */
130 static inline int
131 __gthread_key_delete (__gthread_key_t key)
133 return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
136 static inline void *
137 __gthread_getspecific (__gthread_key_t key)
139 return TlsGetValue (key);
142 static inline int
143 __gthread_setspecific (__gthread_key_t key, const void *ptr)
145 return (TlsSetValue (key, ptr) != 0) ? 0 : (int) GetLastError ();
148 static inline void
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);
155 static inline int
156 __gthread_mutex_lock (__gthread_mutex_t *mutex)
158 int status = 0;
160 if (__gthread_active_p ())
162 if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
163 status = 0;
164 else
165 status = 1;
167 return status;
170 static inline int
171 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
173 int status = 0;
175 if (__gthread_active_p ())
177 if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
178 status = 0;
179 else
180 status = 1;
182 return status;
185 static inline int
186 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
188 if (__gthread_active_p ())
189 return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
190 else
191 return 0;
194 #endif /* not __gthr_win32_h */