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,
22 /* This file contains primitives for creating and controlling threads.
24 Thread data type: gl_thread_t.
27 thread = gl_thread_create (func, arg);
28 Or with control of error handling:
29 err = glthread_create (&thread, func, arg);
30 extern int glthread_create (gl_thread_t *result,
31 void *(*func) (void *), void *arg);
33 Querying and changing the signal mask of a thread (not supported on all
35 gl_thread_sigmask (how, newmask, oldmask);
36 Or with control of error handling:
37 err = glthread_sigmask (how, newmask, oldmask);
38 extern int glthread_sigmask (int how, const sigset_t *newmask, sigset_t *oldmask);
40 Waiting for termination of another thread:
41 gl_thread_join (thread, &return_value);
42 Or with control of error handling:
43 err = glthread_join (thread, &return_value);
44 extern int glthread_join (gl_thread_t thread, void **return_value_ptr);
46 Getting a reference to the current thread:
47 current = gl_thread_self ();
48 extern gl_thread_t gl_thread_self (void);
50 Getting a reference to the current thread as a pointer, for debugging:
51 ptr = gl_thread_self_pointer ();
52 extern void * gl_thread_self_pointer (void);
54 Terminating the current thread:
55 gl_thread_exit (return_value);
56 extern _Noreturn void gl_thread_exit (void *return_value);
58 Requesting custom code to be executed at fork() time(not supported on all
60 gl_thread_atfork (prepare_func, parent_func, child_func);
61 Or with control of error handling:
62 err = glthread_atfork (prepare_func, parent_func, child_func);
63 extern int glthread_atfork (void (*prepare_func) (void),
64 void (*parent_func) (void),
65 void (*child_func) (void));
66 Note that even on platforms where this is supported, use of fork() and
67 threads together is problematic, see
68 <http://lists.gnu.org/archive/html/bug-gnulib/2008-08/msg00062.html>
72 #ifndef _GLTHREAD_THREAD_H
73 #define _GLTHREAD_THREAD_H
78 /* ========================================================================= */
82 /* Use the POSIX threads library. */
90 # if PTHREAD_IN_USE_DETECTION_HARD
92 /* The pthread_in_use() detection needs to be done at runtime. */
93 # define pthread_in_use() \
95 extern int glthread_in_use (void);
99 # if USE_POSIX_THREADS_WEAK
101 /* Use weak references to the POSIX threads library. */
103 /* Weak references avoid dragging in external libraries if the other parts
104 of the program don't use them. Here we use them, because we don't want
105 every program that uses libintl to depend on libpthread. This assumes
106 that libpthread would not be loaded after libintl; i.e. if libintl is
107 loaded first, by an executable that does not depend on libpthread, and
108 then a module is dynamically loaded that depends on libpthread, libintl
109 will not be multithread-safe. */
111 /* The way to test at runtime whether libpthread is present is to test
112 whether a function pointer's value, such as &pthread_mutex_init, is
113 non-NULL. However, some versions of GCC have a bug through which, in
114 PIC mode, &foo != NULL always evaluates to true if there is a direct
115 call to foo(...) in the same function. To avoid this, we test the
116 address of a function in libpthread that we don't use. */
118 # pragma weak pthread_create
119 # pragma weak pthread_sigmask
120 # pragma weak pthread_join
121 # ifndef pthread_self
122 # pragma weak pthread_self
124 # pragma weak pthread_exit
125 # if HAVE_PTHREAD_ATFORK
126 # pragma weak pthread_atfork
129 # if !PTHREAD_IN_USE_DETECTION_HARD
130 # pragma weak pthread_cancel
131 # define pthread_in_use() (pthread_cancel != NULL)
136 # if !PTHREAD_IN_USE_DETECTION_HARD
137 # define pthread_in_use() 1
142 /* -------------------------- gl_thread_t datatype -------------------------- */
144 /* This choice of gl_thread_t assumes that
145 pthread_equal (a, b) is equivalent to ((a) == (b)).
146 This is the case on all platforms in use in 2008. */
147 typedef pthread_t gl_thread_t;
148 # define glthread_create(THREADP, FUNC, ARG) \
149 (pthread_in_use () ? pthread_create (THREADP, NULL, FUNC, ARG) : ENOSYS)
150 # define glthread_sigmask(HOW, SET, OSET) \
151 (pthread_in_use () ? pthread_sigmask (HOW, SET, OSET) : 0)
152 # define glthread_join(THREAD, RETVALP) \
153 (pthread_in_use () ? pthread_join (THREAD, RETVALP) : 0)
154 # ifdef PTW32_VERSION
155 /* In pthreads-win32, pthread_t is a struct with a pointer field 'p' and
157 # define gl_thread_self() \
158 (pthread_in_use () ? pthread_self () : gl_null_thread)
159 # define gl_thread_self_pointer() \
160 (pthread_in_use () ? pthread_self ().p : NULL)
161 extern const gl_thread_t gl_null_thread;
163 # define gl_thread_self() \
164 (pthread_in_use () ? pthread_self () : (pthread_t) NULL)
165 # define gl_thread_self_pointer() \
166 (pthread_in_use () ? (void *) pthread_self () : NULL)
168 # define gl_thread_exit(RETVAL) \
169 (pthread_in_use () ? pthread_exit (RETVAL) : 0)
171 # if HAVE_PTHREAD_ATFORK
172 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \
173 (pthread_in_use () ? pthread_atfork (PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) : 0)
175 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
184 /* ========================================================================= */
188 /* Use the GNU Pth threads library. */
196 # if USE_PTH_THREADS_WEAK
198 /* Use weak references to the GNU Pth threads library. */
200 # pragma weak pth_spawn
201 # pragma weak pth_sigmask
202 # pragma weak pth_join
203 # pragma weak pth_self
204 # pragma weak pth_exit
206 # pragma weak pth_cancel
207 # define pth_in_use() (pth_cancel != NULL)
211 # define pth_in_use() 1
214 /* -------------------------- gl_thread_t datatype -------------------------- */
216 typedef pth_t gl_thread_t;
217 # define glthread_create(THREADP, FUNC, ARG) \
218 (pth_in_use () ? ((*(THREADP) = pth_spawn (NULL, FUNC, ARG)) ? 0 : errno) : 0)
219 # define glthread_sigmask(HOW, SET, OSET) \
220 (pth_in_use () && !pth_sigmask (HOW, SET, OSET) ? errno : 0)
221 # define glthread_join(THREAD, RETVALP) \
222 (pth_in_use () && !pth_join (THREAD, RETVALP) ? errno : 0)
223 # define gl_thread_self() \
224 (pth_in_use () ? (void *) pth_self () : NULL)
225 # define gl_thread_self_pointer() \
227 # define gl_thread_exit(RETVAL) \
228 (pth_in_use () ? pth_exit (RETVAL) : 0)
229 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
237 /* ========================================================================= */
239 #if USE_SOLARIS_THREADS
241 /* Use the old Solaris threads library. */
250 # if USE_SOLARIS_THREADS_WEAK
252 /* Use weak references to the old Solaris threads library. */
254 # pragma weak thr_create
255 # pragma weak thr_join
256 # pragma weak thr_self
257 # pragma weak thr_exit
259 # pragma weak thr_suspend
260 # define thread_in_use() (thr_suspend != NULL)
264 # define thread_in_use() 1
268 /* -------------------------- gl_thread_t datatype -------------------------- */
270 typedef thread_t gl_thread_t;
271 # define glthread_create(THREADP, FUNC, ARG) \
272 (thread_in_use () ? thr_create (NULL, 0, FUNC, ARG, 0, THREADP) : 0)
273 # define glthread_sigmask(HOW, SET, OSET) \
274 (thread_in_use () ? sigprocmask (HOW, SET, OSET) : 0)
275 # define glthread_join(THREAD, RETVALP) \
276 (thread_in_use () ? thr_join (THREAD, NULL, RETVALP) : 0)
277 # define gl_thread_self() \
278 (thread_in_use () ? (void *) thr_self () : NULL)
279 # define gl_thread_self_pointer() \
281 # define gl_thread_exit(RETVAL) \
282 (thread_in_use () ? thr_exit (RETVAL) : 0)
283 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
291 /* ========================================================================= */
293 #if USE_WINDOWS_THREADS
295 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
296 # include <windows.h>
302 /* -------------------------- gl_thread_t datatype -------------------------- */
304 /* The gl_thread_t is a pointer to a structure in memory.
305 Why not the thread handle? If it were the thread handle, it would be hard
306 to implement gl_thread_self() (since GetCurrentThread () returns a pseudo-
307 handle, DuplicateHandle (GetCurrentThread ()) returns a handle that must be
308 closed afterwards, and there is no function for quickly retrieving a thread
310 Why not the thread id? I tried it. It did not work: Sometimes ids appeared
311 that did not belong to running threads, and glthread_join failed with ESRCH.
313 typedef struct gl_thread_struct *gl_thread_t;
314 # define glthread_create(THREADP, FUNC, ARG) \
315 glthread_create_func (THREADP, FUNC, ARG)
316 # define glthread_sigmask(HOW, SET, OSET) \
318 # define glthread_join(THREAD, RETVALP) \
319 glthread_join_func (THREAD, RETVALP)
320 # define gl_thread_self() \
321 gl_thread_self_func ()
322 # define gl_thread_self_pointer() \
324 # define gl_thread_exit(RETVAL) \
325 gl_thread_exit_func (RETVAL)
326 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
327 extern int glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg);
328 extern int glthread_join_func (gl_thread_t thread, void **retvalp);
329 extern gl_thread_t gl_thread_self_func (void);
330 extern int gl_thread_exit_func (void *retval);
338 /* ========================================================================= */
340 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
342 /* Provide dummy implementation if threads are not supported. */
344 typedef int gl_thread_t;
345 # define glthread_create(THREADP, FUNC, ARG) ENOSYS
346 # define glthread_sigmask(HOW, SET, OSET) 0
347 # define glthread_join(THREAD, RETVALP) 0
348 # define gl_thread_self() 0
349 # define gl_thread_self_pointer() \
350 ((void *) gl_thread_self ())
351 # define gl_thread_exit(RETVAL) 0
352 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
356 /* ========================================================================= */
358 /* Macros with built-in error handling. */
364 static inline gl_thread_t
365 gl_thread_create (void *(*func) (void *arg), void *arg)
370 ret = glthread_create (&thread, func, arg);
375 #define gl_thread_sigmask(HOW, SET, OSET) \
378 if (glthread_sigmask (HOW, SET, OSET)) \
382 #define gl_thread_join(THREAD, RETVAL) \
385 if (glthread_join (THREAD, RETVAL)) \
389 #define gl_thread_atfork(PREPARE, PARENT, CHILD) \
392 if (glthread_atfork (PREPARE, PARENT, CHILD)) \
401 #endif /* _GLTHREAD_THREAD_H */