]>
Commit | Line | Data |
---|---|---|
b25bb36a | 1 | /* Implementation of W32-specific threads compatibility routines for |
43f3a59d | 2 | libgcc2. */ |
b25bb36a | 3 | |
283334f0 | 4 | /* Copyright (C) 1999, 2000, 2002, 2004 Free Software Foundation, Inc. |
b25bb36a DS |
5 | Contributed by Mumit Khan <khan@xraylith.wisc.edu>. |
6 | Modified and moved to separate file by Danny Smith | |
7 | <dannysmith@users.sourceforge.net>. | |
8 | ||
9 | This file is part of GCC. | |
10 | ||
11 | GCC is free software; you can redistribute it and/or modify it under | |
12 | the terms of the GNU General Public License as published by the Free | |
13 | Software Foundation; either version 2, or (at your option) any later | |
14 | version. | |
15 | ||
16 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
17 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
18 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with GCC; see the file COPYING. If not, write to the Free | |
23 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
24 | 02111-1307, USA. */ | |
25 | ||
26 | /* As a special exception, if you link this library with other files, | |
27 | some of which are compiled with GCC, to produce an executable, | |
28 | this library does not by itself cause the resulting executable | |
29 | to be covered by the GNU General Public License. | |
30 | This exception does not however invalidate any other reasons why | |
31 | the executable file might be covered by the GNU General Public License. */ | |
32 | ||
33 | ||
42dfcf84 | 34 | #include <windows.h> |
b25bb36a | 35 | #ifndef __GTHREAD_HIDE_WIN32API |
4fab535e | 36 | # define __GTHREAD_HIDE_WIN32API 1 |
b25bb36a | 37 | #endif |
42dfcf84 WY |
38 | #undef __GTHREAD_I486_INLINE_LOCK_PRIMITIVES |
39 | #define __GTHREAD_I486_INLINE_LOCK_PRIMITIVES | |
b25bb36a | 40 | #include <gthr-win32.h> |
b25bb36a DS |
41 | |
42 | /* Windows32 threads specific definitions. The windows32 threading model | |
43 | does not map well into pthread-inspired gcc's threading model, and so | |
44 | there are caveats one needs to be aware of. | |
45 | ||
46 | 1. The destructor supplied to __gthread_key_create is ignored for | |
47 | generic x86-win32 ports. This will certainly cause memory leaks | |
48 | due to unreclaimed eh contexts (sizeof (eh_context) is at least | |
49 | 24 bytes for x86 currently). | |
50 | ||
51 | This memory leak may be significant for long-running applications | |
52 | that make heavy use of C++ EH. | |
53 | ||
54 | However, Mingw runtime (version 0.3 or newer) provides a mechanism | |
55 | to emulate pthreads key dtors; the runtime provides a special DLL, | |
56 | linked in if -mthreads option is specified, that runs the dtors in | |
57 | the reverse order of registration when each thread exits. If | |
58 | -mthreads option is not given, a stub is linked in instead of the | |
59 | DLL, which results in memory leak. Other x86-win32 ports can use | |
60 | the same technique of course to avoid the leak. | |
61 | ||
62 | 2. The error codes returned are non-POSIX like, and cast into ints. | |
63 | This may cause incorrect error return due to truncation values on | |
64 | hw where sizeof (DWORD) > sizeof (int). | |
65 | ||
80408cac WY |
66 | 3. We are currently using a special mutex instead of the Critical |
67 | Sections, since Win9x does not support TryEnterCriticalSection | |
68 | (while NT does). | |
b25bb36a DS |
69 | |
70 | The basic framework should work well enough. In the long term, GCC | |
71 | needs to use Structured Exception Handling on Windows32. */ | |
72 | ||
73 | int | |
74 | __gthr_win32_once (__gthread_once_t *once, void (*func) (void)) | |
75 | { | |
76 | if (once == NULL || func == NULL) | |
77 | return EINVAL; | |
78 | ||
79 | if (! once->done) | |
80 | { | |
81 | if (InterlockedIncrement (&(once->started)) == 0) | |
82 | { | |
83 | (*func) (); | |
84 | once->done = TRUE; | |
85 | } | |
86 | else | |
87 | { | |
88 | /* Another thread is currently executing the code, so wait for it | |
89 | to finish; yield the CPU in the meantime. If performance | |
90 | does become an issue, the solution is to use an Event that | |
91 | we wait on here (and set above), but that implies a place to | |
92 | create the event before this routine is called. */ | |
93 | while (! once->done) | |
94 | Sleep (0); | |
95 | } | |
96 | } | |
97 | return 0; | |
98 | } | |
99 | ||
100 | /* Windows32 thread local keys don't support destructors; this leads to | |
101 | leaks, especially in threaded applications making extensive use of | |
102 | C++ EH. Mingw uses a thread-support DLL to work-around this problem. */ | |
103 | ||
104 | int | |
105 | __gthr_win32_key_create (__gthread_key_t *key, void (*dtor) (void *)) | |
106 | { | |
107 | int status = 0; | |
108 | DWORD tls_index = TlsAlloc (); | |
109 | if (tls_index != 0xFFFFFFFF) | |
110 | { | |
111 | *key = tls_index; | |
112 | #ifdef MINGW32_SUPPORTS_MT_EH | |
113 | /* Mingw runtime will run the dtors in reverse order for each thread | |
114 | when the thread exits. */ | |
115 | status = __mingwthr_key_dtor (*key, dtor); | |
116 | #endif | |
117 | } | |
118 | else | |
119 | status = (int) GetLastError (); | |
120 | return status; | |
121 | } | |
122 | ||
123 | int | |
124 | __gthr_win32_key_delete (__gthread_key_t key) | |
125 | { | |
126 | return (TlsFree (key) != 0) ? 0 : (int) GetLastError (); | |
127 | } | |
128 | ||
129 | void * | |
130 | __gthr_win32_getspecific (__gthread_key_t key) | |
131 | { | |
132 | DWORD lasterror; | |
133 | void *ptr; | |
134 | lasterror = GetLastError(); | |
135 | ptr = TlsGetValue(key); | |
136 | SetLastError( lasterror ); | |
137 | return ptr; | |
138 | } | |
139 | ||
140 | int | |
141 | __gthr_win32_setspecific (__gthread_key_t key, const void *ptr) | |
142 | { | |
143 | return (TlsSetValue (key, (void*) ptr) != 0) ? 0 : (int) GetLastError (); | |
144 | } | |
145 | ||
146 | void | |
147 | __gthr_win32_mutex_init_function (__gthread_mutex_t *mutex) | |
148 | { | |
42dfcf84 | 149 | mutex->counter = -1; |
80408cac | 150 | mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL); |
b25bb36a DS |
151 | } |
152 | ||
153 | int | |
154 | __gthr_win32_mutex_lock (__gthread_mutex_t *mutex) | |
155 | { | |
42dfcf84 | 156 | if (InterlockedIncrement (&mutex->counter) == 0 || |
80408cac | 157 | WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0) |
b25bb36a DS |
158 | return 0; |
159 | else | |
80408cac | 160 | { |
42dfcf84 WY |
161 | /* WaitForSingleObject returns WAIT_FAILED, and we can only do |
162 | some best-effort cleanup here. */ | |
80408cac WY |
163 | InterlockedDecrement (&mutex->counter); |
164 | return 1; | |
165 | } | |
b25bb36a DS |
166 | } |
167 | ||
168 | int | |
169 | __gthr_win32_mutex_trylock (__gthread_mutex_t *mutex) | |
170 | { | |
42dfcf84 | 171 | if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0) |
b25bb36a DS |
172 | return 0; |
173 | else | |
174 | return 1; | |
175 | } | |
176 | ||
177 | int | |
178 | __gthr_win32_mutex_unlock (__gthread_mutex_t *mutex) | |
179 | { | |
42dfcf84 | 180 | if (InterlockedDecrement (&mutex->counter) >= 0) |
80408cac WY |
181 | return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1; |
182 | else | |
183 | return 0; | |
b25bb36a | 184 | } |
40aac948 JM |
185 | |
186 | void | |
187 | __gthr_win32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex) | |
188 | { | |
189 | mutex->counter = -1; | |
190 | mutex->depth = 0; | |
191 | mutex->owner = 0; | |
192 | mutex->sema = CreateSemaphore (NULL, 0, 65535, NULL); | |
193 | } | |
194 | ||
195 | int | |
196 | __gthr_win32_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex) | |
197 | { | |
198 | DWORD me = GetCurrentThreadId(); | |
199 | if (InterlockedIncrement (&mutex->counter) == 0) | |
200 | { | |
201 | mutex->depth = 1; | |
202 | mutex->owner = me; | |
203 | } | |
204 | else if (mutex->owner == me) | |
205 | { | |
206 | InterlockedDecrement (&mx->lock_idx); | |
207 | ++(mutex->depth); | |
208 | } | |
209 | else if (WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0) | |
210 | { | |
211 | mutex->depth = 1; | |
212 | mutex->owner = me; | |
213 | } | |
214 | else | |
215 | { | |
216 | /* WaitForSingleObject returns WAIT_FAILED, and we can only do | |
217 | some best-effort cleanup here. */ | |
218 | InterlockedDecrement (&mutex->counter); | |
219 | return 1; | |
220 | } | |
221 | return 0; | |
222 | } | |
223 | ||
224 | int | |
225 | __gthr_win32_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex) | |
226 | { | |
227 | DWORD me = GetCurrentThreadId(); | |
228 | if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0) | |
229 | { | |
230 | mutex->depth = 1; | |
231 | mutex->owner = me; | |
232 | } | |
233 | else if (mutex->owner == me) | |
234 | ++(mutex->depth); | |
235 | else | |
236 | return 1; | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | int | |
242 | __gthr_win32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex) | |
243 | { | |
244 | --(mutex->depth); | |
245 | if (mutex->depth == 0) | |
246 | { | |
247 | mutex->owner = 0; | |
248 | ||
249 | if (InterlockedDecrement (&mutex->counter) >= 0) | |
250 | return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1; | |
251 | } | |
252 | ||
253 | return 0; | |
254 | } |