1 /* Creating and controlling threads.
2 Copyright (C) 2005-2013 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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005.
18 Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-solaris.h,
21 /* This file contains primitives for creating and controlling threads.
23 Thread data type: gl_thread_t.
26 thread = gl_thread_create (func, arg);
27 Or with control of error handling:
28 err = glthread_create (&thread, func, arg);
29 extern int glthread_create (gl_thread_t *result,
30 void *(*func) (void *), void *arg);
32 Querying and changing the signal mask of a thread (not supported on all
34 gl_thread_sigmask (how, newmask, oldmask);
35 Or with control of error handling:
36 err = glthread_sigmask (how, newmask, oldmask);
37 extern int glthread_sigmask (int how, const sigset_t *newmask, sigset_t *oldmask);
39 Waiting for termination of another thread:
40 gl_thread_join (thread, &return_value);
41 Or with control of error handling:
42 err = glthread_join (thread, &return_value);
43 extern int glthread_join (gl_thread_t thread, void **return_value_ptr);
45 Getting a reference to the current thread:
46 current = gl_thread_self ();
47 extern gl_thread_t gl_thread_self (void);
49 Getting a reference to the current thread as a pointer, for debugging:
50 ptr = gl_thread_self_pointer ();
51 extern void * gl_thread_self_pointer (void);
53 Terminating the current thread:
54 gl_thread_exit (return_value);
55 extern _Noreturn void gl_thread_exit (void *return_value);
57 Requesting custom code to be executed at fork() time(not supported on all
59 gl_thread_atfork (prepare_func, parent_func, child_func);
60 Or with control of error handling:
61 err = glthread_atfork (prepare_func, parent_func, child_func);
62 extern int glthread_atfork (void (*prepare_func) (void),
63 void (*parent_func) (void),
64 void (*child_func) (void));
65 Note that even on platforms where this is supported, use of fork() and
66 threads together is problematic, see
67 <http://lists.gnu.org/archive/html/bug-gnulib/2008-08/msg00062.html>
71 #ifndef _GLTHREAD_THREAD_H
72 #define _GLTHREAD_THREAD_H
77 _GL_INLINE_HEADER_BEGIN
78 #ifndef _GLTHREAD_THREAD_INLINE
79 # define _GLTHREAD_THREAD_INLINE _GL_INLINE
82 /* ========================================================================= */
86 /* Use the POSIX threads library. */
94 # if PTHREAD_IN_USE_DETECTION_HARD
96 /* The pthread_in_use() detection needs to be done at runtime. */
97 # define pthread_in_use() \
99 extern int glthread_in_use (void);
103 # if USE_POSIX_THREADS_WEAK
105 /* Use weak references to the POSIX threads library. */
107 /* Weak references avoid dragging in external libraries if the other parts
108 of the program don't use them. Here we use them, because we don't want
109 every program that uses libintl to depend on libpthread. This assumes
110 that libpthread would not be loaded after libintl; i.e. if libintl is
111 loaded first, by an executable that does not depend on libpthread, and
112 then a module is dynamically loaded that depends on libpthread, libintl
113 will not be multithread-safe. */
115 /* The way to test at runtime whether libpthread is present is to test
116 whether a function pointer's value, such as &pthread_mutex_init, is
117 non-NULL. However, some versions of GCC have a bug through which, in
118 PIC mode, &foo != NULL always evaluates to true if there is a direct
119 call to foo(...) in the same function. To avoid this, we test the
120 address of a function in libpthread that we don't use. */
122 # pragma weak pthread_create
123 # pragma weak pthread_sigmask
124 # pragma weak pthread_join
125 # ifndef pthread_self
126 # pragma weak pthread_self
128 # pragma weak pthread_exit
129 # if HAVE_PTHREAD_ATFORK
130 # pragma weak pthread_atfork
133 # if !PTHREAD_IN_USE_DETECTION_HARD
134 # pragma weak pthread_cancel
135 # define pthread_in_use() (pthread_cancel != NULL)
140 # if !PTHREAD_IN_USE_DETECTION_HARD
141 # define pthread_in_use() 1
146 /* -------------------------- gl_thread_t datatype -------------------------- */
148 /* This choice of gl_thread_t assumes that
149 pthread_equal (a, b) is equivalent to ((a) == (b)).
150 This is the case on all platforms in use in 2008. */
151 typedef pthread_t gl_thread_t;
152 # define glthread_create(THREADP, FUNC, ARG) \
153 (pthread_in_use () ? pthread_create (THREADP, NULL, FUNC, ARG) : ENOSYS)
154 # define glthread_sigmask(HOW, SET, OSET) \
155 (pthread_in_use () ? pthread_sigmask (HOW, SET, OSET) : 0)
156 # define glthread_join(THREAD, RETVALP) \
157 (pthread_in_use () ? pthread_join (THREAD, RETVALP) : 0)
158 # ifdef PTW32_VERSION
159 /* In pthreads-win32, pthread_t is a struct with a pointer field 'p' and
161 # define gl_thread_self() \
162 (pthread_in_use () ? pthread_self () : gl_null_thread)
163 # define gl_thread_self_pointer() \
164 (pthread_in_use () ? pthread_self ().p : NULL)
165 extern const gl_thread_t gl_null_thread;
167 # define gl_thread_self() \
168 (pthread_in_use () ? pthread_self () : (pthread_t) NULL)
169 # define gl_thread_self_pointer() \
170 (pthread_in_use () ? (void *) pthread_self () : NULL)
172 # define gl_thread_exit(RETVAL) \
173 (pthread_in_use () ? pthread_exit (RETVAL) : 0)
175 # if HAVE_PTHREAD_ATFORK
176 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \
177 (pthread_in_use () ? pthread_atfork (PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) : 0)
179 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
188 /* ========================================================================= */
192 /* Use the GNU Pth threads library. */
200 # if USE_PTH_THREADS_WEAK
202 /* Use weak references to the GNU Pth threads library. */
204 # pragma weak pth_spawn
205 # pragma weak pth_sigmask
206 # pragma weak pth_join
207 # pragma weak pth_self
208 # pragma weak pth_exit
210 # pragma weak pth_cancel
211 # define pth_in_use() (pth_cancel != NULL)
215 # define pth_in_use() 1
218 /* -------------------------- gl_thread_t datatype -------------------------- */
220 typedef pth_t gl_thread_t;
221 # define glthread_create(THREADP, FUNC, ARG) \
222 (pth_in_use () ? ((*(THREADP) = pth_spawn (NULL, FUNC, ARG)) ? 0 : errno) : 0)
223 # define glthread_sigmask(HOW, SET, OSET) \
224 (pth_in_use () && !pth_sigmask (HOW, SET, OSET) ? errno : 0)
225 # define glthread_join(THREAD, RETVALP) \
226 (pth_in_use () && !pth_join (THREAD, RETVALP) ? errno : 0)
227 # define gl_thread_self() \
228 (pth_in_use () ? (void *) pth_self () : NULL)
229 # define gl_thread_self_pointer() \
231 # define gl_thread_exit(RETVAL) \
232 (pth_in_use () ? pth_exit (RETVAL) : 0)
233 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
241 /* ========================================================================= */
243 #if USE_SOLARIS_THREADS
245 /* Use the old Solaris threads library. */
254 # if USE_SOLARIS_THREADS_WEAK
256 /* Use weak references to the old Solaris threads library. */
258 # pragma weak thr_create
259 # pragma weak thr_join
260 # pragma weak thr_self
261 # pragma weak thr_exit
263 # pragma weak thr_suspend
264 # define thread_in_use() (thr_suspend != NULL)
268 # define thread_in_use() 1
272 /* -------------------------- gl_thread_t datatype -------------------------- */
274 typedef thread_t gl_thread_t;
275 # define glthread_create(THREADP, FUNC, ARG) \
276 (thread_in_use () ? thr_create (NULL, 0, FUNC, ARG, 0, THREADP) : 0)
277 # define glthread_sigmask(HOW, SET, OSET) \
278 (thread_in_use () ? sigprocmask (HOW, SET, OSET) : 0)
279 # define glthread_join(THREAD, RETVALP) \
280 (thread_in_use () ? thr_join (THREAD, NULL, RETVALP) : 0)
281 # define gl_thread_self() \
282 (thread_in_use () ? (void *) thr_self () : NULL)
283 # define gl_thread_self_pointer() \
285 # define gl_thread_exit(RETVAL) \
286 (thread_in_use () ? thr_exit (RETVAL) : 0)
287 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
295 /* ========================================================================= */
297 #if USE_WINDOWS_THREADS
299 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
300 # include <windows.h>
306 /* -------------------------- gl_thread_t datatype -------------------------- */
308 /* The gl_thread_t is a pointer to a structure in memory.
309 Why not the thread handle? If it were the thread handle, it would be hard
310 to implement gl_thread_self() (since GetCurrentThread () returns a pseudo-
311 handle, DuplicateHandle (GetCurrentThread ()) returns a handle that must be
312 closed afterwards, and there is no function for quickly retrieving a thread
314 Why not the thread id? I tried it. It did not work: Sometimes ids appeared
315 that did not belong to running threads, and glthread_join failed with ESRCH.
317 typedef struct gl_thread_struct *gl_thread_t;
318 # define glthread_create(THREADP, FUNC, ARG) \
319 glthread_create_func (THREADP, FUNC, ARG)
320 # define glthread_sigmask(HOW, SET, OSET) \
322 # define glthread_join(THREAD, RETVALP) \
323 glthread_join_func (THREAD, RETVALP)
324 # define gl_thread_self() \
325 gl_thread_self_func ()
326 # define gl_thread_self_pointer() \
328 # define gl_thread_exit(RETVAL) \
329 gl_thread_exit_func (RETVAL)
330 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
331 extern int glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg);
332 extern int glthread_join_func (gl_thread_t thread, void **retvalp);
333 extern gl_thread_t gl_thread_self_func (void);
334 extern int gl_thread_exit_func (void *retval);
342 /* ========================================================================= */
344 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
346 /* Provide dummy implementation if threads are not supported. */
348 typedef int gl_thread_t;
349 # define glthread_create(THREADP, FUNC, ARG) ENOSYS
350 # define glthread_sigmask(HOW, SET, OSET) 0
351 # define glthread_join(THREAD, RETVALP) 0
352 # define gl_thread_self() 0
353 # define gl_thread_self_pointer() \
354 ((void *) gl_thread_self ())
355 # define gl_thread_exit(RETVAL) 0
356 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
360 /* ========================================================================= */
362 /* Macros with built-in error handling. */
368 _GLTHREAD_THREAD_INLINE gl_thread_t
369 gl_thread_create (void *(*func) (void *arg), void *arg)
374 ret = glthread_create (&thread, func, arg);
379 #define gl_thread_sigmask(HOW, SET, OSET) \
382 if (glthread_sigmask (HOW, SET, OSET)) \
386 #define gl_thread_join(THREAD, RETVAL) \
389 if (glthread_join (THREAD, RETVAL)) \
393 #define gl_thread_atfork(PREPARE, PARENT, CHILD) \
396 if (glthread_atfork (PREPARE, PARENT, CHILD)) \
405 _GL_INLINE_HEADER_END
407 #endif /* _GLTHREAD_THREAD_H */