1 /* Creating and controlling threads (native Windows implementation).
2 Copyright (C) 2005-2021 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005.
18 Based on GCC's gthr-win32.h. */
23 #include "windows-thread.h"
29 #include "windows-once.h"
30 #include "windows-tls.h"
32 /* The Thread-Local Storage (TLS) key that allows to access each thread's
33 'struct glwthread_thread_struct *' pointer. */
34 static DWORD self_key = (DWORD)-1;
36 /* Initializes self_key. This function must only be called once. */
38 do_init_self_key (void)
40 self_key = TlsAlloc ();
41 /* If this fails, we're hosed. */
42 if (self_key == (DWORD)-1)
46 /* Initializes self_key. */
50 static glwthread_once_t once = GLWTHREAD_ONCE_INIT;
51 glwthread_once (&once, do_init_self_key);
54 /* This structure contains information about a thread.
55 It is stored in TLS under key self_key. */
56 struct glwthread_thread_struct
58 /* Fields for managing the handle. */
59 HANDLE volatile handle;
60 CRITICAL_SECTION handle_lock;
61 /* Fields for managing the exit value. */
62 BOOL volatile detached;
63 void * volatile result;
64 /* Fields for managing the thread start. */
65 void * (*func) (void *);
69 /* Return a real HANDLE object for the current thread. */
71 get_current_thread_handle (void)
75 /* GetCurrentThread() returns a pseudo-handle, i.e. only a symbolic
76 identifier, not a real handle. */
77 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
78 GetCurrentProcess (), &this_handle,
79 0, FALSE, DUPLICATE_SAME_ACCESS))
85 glwthread_thread_self (void)
87 glwthread_thread_t thread;
89 if (self_key == (DWORD)-1)
91 thread = TlsGetValue (self_key);
94 /* This happens only in threads that have not been created through
95 glthread_create(), such as the main thread. */
99 (struct glwthread_thread_struct *)
100 malloc (sizeof (struct glwthread_thread_struct));
103 /* Memory allocation failed. There is not much we can do. Have to
104 busy-loop, waiting for the availability of memory. */
108 thread->handle = get_current_thread_handle ();
109 InitializeCriticalSection (&thread->handle_lock);
110 thread->detached = FALSE; /* This can lead to a memory leak. */
111 thread->result = NULL; /* just to be deterministic */
112 TlsSetValue (self_key, thread);
117 /* The main function of a freshly creating thread. It's a wrapper around
118 the FUNC and ARG arguments passed to glthread_create_func. */
119 static unsigned int WINAPI
120 wrapper_func (void *varg)
122 struct glwthread_thread_struct *thread =
123 (struct glwthread_thread_struct *) varg;
125 EnterCriticalSection (&thread->handle_lock);
126 /* Create a new handle for the thread only if the parent thread did not yet
127 fill in the handle. */
128 if (thread->handle == NULL)
129 thread->handle = get_current_thread_handle ();
130 LeaveCriticalSection (&thread->handle_lock);
132 if (self_key == (DWORD)-1)
134 TlsSetValue (self_key, thread);
136 /* Run the thread. Store the exit value if the thread was not terminated
138 thread->result = thread->func (thread->arg);
140 /* Process the TLS destructors. */
141 glwthread_tls_process_destructors ();
143 if (thread->detached)
145 /* Clean up the thread, like thrd_join would do. */
146 DeleteCriticalSection (&thread->handle_lock);
147 CloseHandle (thread->handle);
155 glwthread_thread_create (glwthread_thread_t *threadp, unsigned int attr,
156 void * (*func) (void *), void *arg)
158 struct glwthread_thread_struct *thread =
159 (struct glwthread_thread_struct *)
160 malloc (sizeof (struct glwthread_thread_struct));
163 thread->handle = NULL;
164 InitializeCriticalSection (&thread->handle_lock);
165 thread->detached = (attr & GLWTHREAD_ATTR_DETACHED ? TRUE : FALSE);
166 thread->result = NULL; /* just to be deterministic */
171 unsigned int thread_id;
172 HANDLE thread_handle;
174 thread_handle = (HANDLE)
175 _beginthreadex (NULL, 100000, wrapper_func, thread, 0, &thread_id);
176 /* calls CreateThread with the same arguments */
177 if (thread_handle == NULL)
179 DeleteCriticalSection (&thread->handle_lock);
184 EnterCriticalSection (&thread->handle_lock);
185 if (thread->handle == NULL)
186 thread->handle = thread_handle;
188 /* thread->handle was already set by the thread itself. */
189 CloseHandle (thread_handle);
190 LeaveCriticalSection (&thread->handle_lock);
198 glwthread_thread_join (glwthread_thread_t thread, void **retvalp)
203 if (thread == glwthread_thread_self ())
206 if (thread->detached)
209 if (WaitForSingleObject (thread->handle, INFINITE) == WAIT_FAILED)
213 *retvalp = thread->result;
215 DeleteCriticalSection (&thread->handle_lock);
216 CloseHandle (thread->handle);
223 glwthread_thread_detach (glwthread_thread_t thread)
228 if (thread->detached)
231 thread->detached = TRUE;
236 glwthread_thread_exit (void *retval)
238 glwthread_thread_t thread = glwthread_thread_self ();
239 thread->result = retval;
240 glwthread_tls_process_destructors ();
241 _endthreadex (0); /* calls ExitThread (0) */