1 /* Creating and controlling threads.
2 Copyright (C) 2005-2012 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005.
19 Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-solaris.h,
25 #include "glthread/thread.h"
28 #include "glthread/lock.h"
30 /* ========================================================================= */
38 const gl_thread_t gl_null_thread /* = { .p = NULL } */;
44 /* ========================================================================= */
46 #if USE_WINDOWS_THREADS
50 /* -------------------------- gl_thread_t datatype -------------------------- */
52 /* The Thread-Local Storage (TLS) key that allows to access each thread's
53 'struct gl_thread_struct *' pointer. */
54 static DWORD self_key = (DWORD)-1;
56 /* Initializes self_key. This function must only be called once. */
58 do_init_self_key (void)
60 self_key = TlsAlloc ();
61 /* If this fails, we're hosed. */
62 if (self_key == (DWORD)-1)
66 /* Initializes self_key. */
70 gl_once_define(static, once)
71 gl_once (once, do_init_self_key);
74 /* This structure contains information about a thread.
75 It is stored in TLS under key self_key. */
76 struct gl_thread_struct
78 /* Fields for managing the handle. */
79 HANDLE volatile handle;
80 CRITICAL_SECTION handle_lock;
81 /* Fields for managing the exit value. */
82 void * volatile result;
83 /* Fields for managing the thread start. */
84 void * (*func) (void *);
88 /* Return a real HANDLE object for the current thread. */
90 get_current_thread_handle (void)
94 /* GetCurrentThread() returns a pseudo-handle, i.e. only a symbolic
95 identifier, not a real handle. */
96 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
97 GetCurrentProcess (), &this_handle,
98 0, FALSE, DUPLICATE_SAME_ACCESS))
104 gl_thread_self_func (void)
108 if (self_key == (DWORD)-1)
110 thread = TlsGetValue (self_key);
113 /* This happens only in threads that have not been created through
114 glthread_create(), such as the main thread. */
118 (struct gl_thread_struct *)
119 malloc (sizeof (struct gl_thread_struct));
122 /* Memory allocation failed. There is not much we can do. Have to
123 busy-loop, waiting for the availability of memory. */
127 thread->handle = get_current_thread_handle ();
128 InitializeCriticalSection (&thread->handle_lock);
129 thread->result = NULL; /* just to be deterministic */
130 TlsSetValue (self_key, thread);
135 /* The main function of a freshly creating thread. It's a wrapper around
136 the FUNC and ARG arguments passed to glthread_create_func. */
137 static unsigned int WINAPI
138 wrapper_func (void *varg)
140 struct gl_thread_struct *thread = (struct gl_thread_struct *)varg;
142 EnterCriticalSection (&thread->handle_lock);
143 /* Create a new handle for the thread only if the parent thread did not yet
144 fill in the handle. */
145 if (thread->handle == NULL)
146 thread->handle = get_current_thread_handle ();
147 LeaveCriticalSection (&thread->handle_lock);
149 if (self_key == (DWORD)-1)
151 TlsSetValue (self_key, thread);
153 /* Run the thread. Store the exit value if the thread was not terminated
155 thread->result = thread->func (thread->arg);
160 glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg)
162 struct gl_thread_struct *thread =
163 (struct gl_thread_struct *) malloc (sizeof (struct gl_thread_struct));
166 thread->handle = NULL;
167 InitializeCriticalSection (&thread->handle_lock);
168 thread->result = NULL; /* just to be deterministic */
173 unsigned int thread_id;
174 HANDLE thread_handle;
176 thread_handle = (HANDLE)
177 _beginthreadex (NULL, 100000, wrapper_func, thread, 0, &thread_id);
178 /* calls CreateThread with the same arguments */
179 if (thread_handle == NULL)
181 DeleteCriticalSection (&thread->handle_lock);
186 EnterCriticalSection (&thread->handle_lock);
187 if (thread->handle == NULL)
188 thread->handle = thread_handle;
190 /* thread->handle was already set by the thread itself. */
191 CloseHandle (thread_handle);
192 LeaveCriticalSection (&thread->handle_lock);
200 glthread_join_func (gl_thread_t thread, void **retvalp)
205 if (thread == gl_thread_self ())
208 if (WaitForSingleObject (thread->handle, INFINITE) == WAIT_FAILED)
212 *retvalp = thread->result;
214 DeleteCriticalSection (&thread->handle_lock);
215 CloseHandle (thread->handle);
222 gl_thread_exit_func (void *retval)
224 gl_thread_t thread = gl_thread_self ();
225 thread->result = retval;
226 _endthreadex (0); /* calls ExitThread (0) */
232 /* ========================================================================= */