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