Build: Set gcc_cv_as_mips_explicit_relocs if gcc_cv_as_mips_explicit_relocs_pcrel
[official-gcc.git] / libgcc / config / i386 / gthr-win32-thread.c
blob0030fcc080e190fad2064a69ebb61338068ad669
1 /* Implementation of threads compatibility routines for libgcc2. */
3 /* Copyright (C) 1999-2024 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This module is separate from the rest of the implementation because only
27 one copy of it ought to be linked. */
29 /* The implementation strategy for the c++0x thread support is as follows.
31 A GNU thread is represented by a Win32 HANDLE that is obtained when the
32 Win32 thread is created, except of course for the initial thread. This
33 Win32 HANDLE is stored in a descriptor keyed from TLS memory for every
34 thread, so the self routine can return it instead of having to duplicate
35 the pseudo-handle returned by GetCurrentThread each time it is invoked.
36 For the initial thread, this Win32 HANDLE is created during the first
37 call to the self routine using the aforementioned technique.
39 Note that the equal routine compares the identifier of threads instead
40 of their Win32 HANDLE, which will give the correct positive answer even
41 in the case where distinct Win32 HANDLEs have been created for the same
42 thread by multiple instances of libgcc included in the link. */
44 #include "gthr-win32.h"
46 /* The thread descriptor keyed from TLS memory. */
47 struct __gthr_win32_thr_desc
49 void *(*func) (void*);
50 void *args;
51 HANDLE h;
54 /* The TLS key used by one instance of the library. */
55 static __gthread_key_t __gthr_win32_tls = TLS_OUT_OF_INDEXES;
57 /* The initialization device for the TLS key. */
58 static __gthread_once_t __gthr_win32_tls_once = __GTHREAD_ONCE_INIT;
60 /* Initialize the TLS key. */
62 static void
63 __gthr_win32_tls_init (void)
65 if (__gthread_key_create (&__gthr_win32_tls, free))
66 abort ();
69 /* Wrapper routine around thread functions. */
71 static DWORD
72 __gthr_win32_thread_wrapper (void *args)
74 struct __gthr_win32_thr_desc *td = (struct __gthr_win32_thr_desc *) args;
76 __gthread_setspecific (__gthr_win32_tls, td);
78 DWORD exit_code = (DWORD) (ULONG_PTR) (*td->func) (td->args);
80 ExitThread (exit_code);
81 return exit_code;
84 /* Implement the __gthread_create routine. */
86 int
87 __gthr_win32_create (__gthread_t *thr, void *(*func) (void*), void *args)
89 struct __gthr_win32_thr_desc *td;
91 __gthread_once (&__gthr_win32_tls_once, __gthr_win32_tls_init);
93 td = malloc (sizeof (struct __gthr_win32_thr_desc));
94 td->func = func;
95 td->args = args;
96 td->h = CreateThread (NULL, 0,
97 (LPTHREAD_START_ROUTINE) __gthr_win32_thread_wrapper,
98 (LPVOID) td, CREATE_SUSPENDED, NULL);
99 if (td->h)
101 ResumeThread (td->h);
102 *thr = (__gthread_t) td->h;
103 return 0;
105 else
107 free (td);
108 return (int) GetLastError ();
112 /* Implement the __gthread_join routine. */
115 __gthr_win32_join (__gthread_t thr, void **value_ptr)
117 int status = 0;
119 if (GetThreadId ((HANDLE) thr) == GetCurrentThreadId ())
120 return 1;
122 if (WaitForSingleObject ((HANDLE) thr, INFINITE) == WAIT_OBJECT_0)
124 if (value_ptr)
126 DWORD exit_code;
127 if (GetExitCodeThread ((HANDLE) thr, &exit_code))
128 *value_ptr = (void *) (ULONG_PTR) exit_code;
129 else
130 status = (int) GetLastError ();
133 else
134 status = (int) GetLastError ();
136 CloseHandle ((HANDLE) thr);
137 return status;
140 /* Implement the __gthread_self routine. */
142 __gthread_t
143 __gthr_win32_self (void)
145 struct __gthr_win32_thr_desc *td;
147 __gthread_once (&__gthr_win32_tls_once, __gthr_win32_tls_init);
149 if (!(td = __gthread_getspecific (__gthr_win32_tls)))
151 HANDLE proc = GetCurrentProcess ();
152 td = malloc (sizeof (struct __gthr_win32_thr_desc));
153 td->func = NULL;
154 td->args = NULL;
155 if (!DuplicateHandle (proc, GetCurrentThread(), proc, &td->h, 0, FALSE,
156 DUPLICATE_SAME_ACCESS))
157 abort ();
158 __gthread_setspecific (__gthr_win32_tls, td);
161 return td->h;