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