OSDN Git Service

* config/c4x/rtems.h: New file.
[pf3gnuchains/gcc-fork.git] / gcc / gthr-win32.h
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>.
5
6 This file is part of GNU CC.
7
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.
12
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.
17
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.  */
22
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.  */
29
30 #ifndef __gthr_win32_h
31 #define __gthr_win32_h
32
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.
36
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).
40
41       This memory leak may be significant for long-running applications
42       that make heavy use of C++ EH.
43
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).
47   
48    The basic framework should work well enough. */
49
50 #define __GTHREADS 1
51
52 #include <windows.h>
53 #include <errno.h>
54
55 typedef DWORD __gthread_key_t;
56
57 typedef struct {
58   int done;
59   long started;
60 } __gthread_once_t;
61
62 typedef HANDLE __gthread_mutex_t;
63
64 #define __GTHREAD_ONCE_INIT {FALSE, -1}
65 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
66
67 static inline int
68 __gthread_active_p ()
69 {
70   return 1;
71 }
72
73 static inline int
74 __gthread_once (__gthread_once_t *once, void (*func) ())
75 {
76   if (! __gthread_active_p ())
77     return -1;
78   else if (once == NULL || func == NULL)
79     return EINVAL;
80
81   if (! once->done)
82     {
83       if (InterlockedIncrement (&(once->started)) == 0)
84         {
85           (*func) ();
86           once->done = TRUE;
87         }
88     }
89   else
90     {
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);
95     }
96   
97   return 0;
98 }
99
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__)))
105 {
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;
113 }
114
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)
119 {
120   int status = 0;
121
122   /* Just reset the key value to zero. */
123   if (ptr)
124     status = (TlsSetValue (key, 0) != 0) ? 0 : (int) GetLastError ();
125   return status;
126 }
127
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)
132 {
133   return (TlsFree (key) != 0) ? 0 : (int) GetLastError ();
134 }
135
136 static inline void *
137 __gthread_getspecific (__gthread_key_t key)
138 {
139   return TlsGetValue (key);
140 }
141
142 static inline int
143 __gthread_setspecific (__gthread_key_t key, const void *ptr)
144 {
145   return (TlsSetValue (key, ptr) != 0) ? 0 : (int) GetLastError ();
146 }
147
148 static inline void
149 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
150 {
151   /* Create unnamed mutex with default security attr and no initial owner.  */ 
152   *mutex = CreateMutex (NULL, 0, NULL);
153 }
154
155 static inline int
156 __gthread_mutex_lock (__gthread_mutex_t *mutex)
157 {
158   int status = 0;
159
160   if (__gthread_active_p ())
161     {
162       if (WaitForSingleObject (*mutex, INFINITE) == WAIT_OBJECT_0)
163         status = 0;
164       else
165         status = 1;
166     }
167   return status;
168 }
169
170 static inline int
171 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
172 {
173   int status = 0;
174
175   if (__gthread_active_p ())
176     {
177       if (WaitForSingleObject (*mutex, 0) == WAIT_OBJECT_0)
178         status = 0;
179       else
180         status = 1;
181     }
182   return status;
183 }
184
185 static inline int
186 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
187 {
188   if (__gthread_active_p ())
189     return (ReleaseMutex (*mutex) != 0) ? 0 : 1;
190   else
191     return 0;
192 }
193
194 #endif /* not __gthr_win32_h */
195