1 /* Creating and controlling threads.
2 Copyright (C) 2005-2008 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 2, 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 /* ========================================================================= */
34 /* -------------------------- gl_thread_t datatype -------------------------- */
36 /* The Thread-Local Storage (TLS) key that allows to access each thread's
37 'struct gl_thread_struct *' pointer. */
38 static DWORD self_key = (DWORD)-1;
40 /* Initializes self_key. This function must only be called once. */
42 do_init_self_key (void)
44 self_key = TlsAlloc ();
45 /* If this fails, we're hosed. */
46 if (self_key == (DWORD)-1)
50 /* Initializes self_key. */
54 gl_once_define(static, once)
55 gl_once (once, do_init_self_key);
58 /* This structure contains information about a thread.
59 It is stored in TLS under key self_key. */
60 struct gl_thread_struct
62 /* Fields for managing the handle. */
63 HANDLE volatile handle;
64 CRITICAL_SECTION handle_lock;
65 /* Fields for managing the exit value. */
66 void * volatile result;
67 /* Fields for managing the thread start. */
68 void * (*func) (void *);
72 /* Return a real HANDLE object for the current thread. */
74 get_current_thread_handle (void)
78 /* GetCurrentThread() returns a pseudo-handle, i.e. only a symbolic
79 identifier, not a real handle. */
80 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
81 GetCurrentProcess (), &this_handle,
82 0, FALSE, DUPLICATE_SAME_ACCESS))
88 gl_thread_self_func (void)
92 if (self_key == (DWORD)-1)
94 thread = TlsGetValue (self_key);
97 /* This happens only in threads that have not been created through
98 glthread_create(), such as the main thread. */
102 (struct gl_thread_struct *)
103 malloc (sizeof (struct gl_thread_struct));
106 /* Memory allocation failed. There is not much we can do. Have to
107 busy-loop, waiting for the availability of memory. */
111 thread->handle = get_current_thread_handle ();
112 InitializeCriticalSection (&thread->handle_lock);
113 thread->result = NULL; /* just to be deterministic */
114 TlsSetValue (self_key, thread);
119 /* The main function of a freshly creating thread. It's a wrapper around
120 the FUNC and ARG arguments passed to glthread_create_func. */
122 wrapper_func (void *varg)
124 struct gl_thread_struct *thread = (struct gl_thread_struct *)varg;
126 EnterCriticalSection (&thread->handle_lock);
127 /* Create a new handle for the thread only if the parent thread did not yet
128 fill in the handle. */
129 if (thread->handle == NULL)
130 thread->handle = get_current_thread_handle ();
131 LeaveCriticalSection (&thread->handle_lock);
133 if (self_key == (DWORD)-1)
135 TlsSetValue (self_key, thread);
137 /* Run the thread. Store the exit value if the thread was not terminated
139 thread->result = thread->func (thread->arg);
144 glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg)
146 struct gl_thread_struct *thread =
147 (struct gl_thread_struct *) malloc (sizeof (struct gl_thread_struct));
150 thread->handle = NULL;
151 InitializeCriticalSection (&thread->handle_lock);
152 thread->result = NULL; /* just to be deterministic */
158 HANDLE thread_handle;
161 CreateThread (NULL, 100000, wrapper_func, thread, 0, &thread_id);
162 if (thread_handle == NULL)
164 DeleteCriticalSection (&thread->handle_lock);
169 EnterCriticalSection (&thread->handle_lock);
170 if (thread->handle == NULL)
171 thread->handle = thread_handle;
173 /* thread->handle was already set by the thread itself. */
174 CloseHandle (thread_handle);
175 LeaveCriticalSection (&thread->handle_lock);
183 glthread_join_func (gl_thread_t thread, void **retvalp)
188 if (thread == gl_thread_self ())
191 if (WaitForSingleObject (thread->handle, INFINITE) == WAIT_FAILED)
195 *retvalp = thread->result;
197 DeleteCriticalSection (&thread->handle_lock);
198 CloseHandle (thread->handle);
205 gl_thread_exit_func (void *retval)
207 gl_thread_t thread = gl_thread_self ();
208 thread->result = retval;
214 /* ========================================================================= */