1 /* Creating and controlling threads.
2 Copyright (C) 2005-2011 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 /* ========================================================================= */
36 /* -------------------------- gl_thread_t datatype -------------------------- */
38 /* The Thread-Local Storage (TLS) key that allows to access each thread's
39 'struct gl_thread_struct *' pointer. */
40 static DWORD self_key = (DWORD)-1;
42 /* Initializes self_key. This function must only be called once. */
44 do_init_self_key (void)
46 self_key = TlsAlloc ();
47 /* If this fails, we're hosed. */
48 if (self_key == (DWORD)-1)
52 /* Initializes self_key. */
56 gl_once_define(static, once)
57 gl_once (once, do_init_self_key);
60 /* This structure contains information about a thread.
61 It is stored in TLS under key self_key. */
62 struct gl_thread_struct
64 /* Fields for managing the handle. */
65 HANDLE volatile handle;
66 CRITICAL_SECTION handle_lock;
67 /* Fields for managing the exit value. */
68 void * volatile result;
69 /* Fields for managing the thread start. */
70 void * (*func) (void *);
74 /* Return a real HANDLE object for the current thread. */
76 get_current_thread_handle (void)
80 /* GetCurrentThread() returns a pseudo-handle, i.e. only a symbolic
81 identifier, not a real handle. */
82 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
83 GetCurrentProcess (), &this_handle,
84 0, FALSE, DUPLICATE_SAME_ACCESS))
90 gl_thread_self_func (void)
94 if (self_key == (DWORD)-1)
96 thread = TlsGetValue (self_key);
99 /* This happens only in threads that have not been created through
100 glthread_create(), such as the main thread. */
104 (struct gl_thread_struct *)
105 malloc (sizeof (struct gl_thread_struct));
108 /* Memory allocation failed. There is not much we can do. Have to
109 busy-loop, waiting for the availability of memory. */
113 thread->handle = get_current_thread_handle ();
114 InitializeCriticalSection (&thread->handle_lock);
115 thread->result = NULL; /* just to be deterministic */
116 TlsSetValue (self_key, thread);
121 /* The main function of a freshly creating thread. It's a wrapper around
122 the FUNC and ARG arguments passed to glthread_create_func. */
123 static unsigned int WINAPI
124 wrapper_func (void *varg)
126 struct gl_thread_struct *thread = (struct gl_thread_struct *)varg;
128 EnterCriticalSection (&thread->handle_lock);
129 /* Create a new handle for the thread only if the parent thread did not yet
130 fill in the handle. */
131 if (thread->handle == NULL)
132 thread->handle = get_current_thread_handle ();
133 LeaveCriticalSection (&thread->handle_lock);
135 if (self_key == (DWORD)-1)
137 TlsSetValue (self_key, thread);
139 /* Run the thread. Store the exit value if the thread was not terminated
141 thread->result = thread->func (thread->arg);
146 glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg)
148 struct gl_thread_struct *thread =
149 (struct gl_thread_struct *) malloc (sizeof (struct gl_thread_struct));
152 thread->handle = NULL;
153 InitializeCriticalSection (&thread->handle_lock);
154 thread->result = NULL; /* just to be deterministic */
159 unsigned int thread_id;
160 HANDLE thread_handle;
162 thread_handle = (HANDLE)
163 _beginthreadex (NULL, 100000, wrapper_func, thread, 0, &thread_id);
164 /* calls CreateThread with the same arguments */
165 if (thread_handle == NULL)
167 DeleteCriticalSection (&thread->handle_lock);
172 EnterCriticalSection (&thread->handle_lock);
173 if (thread->handle == NULL)
174 thread->handle = thread_handle;
176 /* thread->handle was already set by the thread itself. */
177 CloseHandle (thread_handle);
178 LeaveCriticalSection (&thread->handle_lock);
186 glthread_join_func (gl_thread_t thread, void **retvalp)
191 if (thread == gl_thread_self ())
194 if (WaitForSingleObject (thread->handle, INFINITE) == WAIT_FAILED)
198 *retvalp = thread->result;
200 DeleteCriticalSection (&thread->handle_lock);
201 CloseHandle (thread->handle);
208 gl_thread_exit_func (void *retval)
210 gl_thread_t thread = gl_thread_self ();
211 thread->result = retval;
212 _endthreadex (0); /* calls ExitThread (0) */
218 /* ========================================================================= */