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