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 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 /* ========================================================================= */
81 /* Use the POSIX threads library. */
89 # if PTHREAD_IN_USE_DETECTION_HARD
91 /* The pthread_in_use() detection needs to be done at runtime. */
92 # define pthread_in_use() \
94 extern int glthread_in_use (void);
98 # if USE_POSIX_THREADS_WEAK
100 /* Use weak references to the POSIX threads library. */
102 /* Weak references avoid dragging in external libraries if the other parts
103 of the program don't use them. Here we use them, because we don't want
104 every program that uses libintl to depend on libpthread. This assumes
105 that libpthread would not be loaded after libintl; i.e. if libintl is
106 loaded first, by an executable that does not depend on libpthread, and
107 then a module is dynamically loaded that depends on libpthread, libintl
108 will not be multithread-safe. */
110 /* The way to test at runtime whether libpthread is present is to test
111 whether a function pointer's value, such as &pthread_mutex_init, is
112 non-NULL. However, some versions of GCC have a bug through which, in
113 PIC mode, &foo != NULL always evaluates to true if there is a direct
114 call to foo(...) in the same function. To avoid this, we test the
115 address of a function in libpthread that we don't use. */
117 # pragma weak pthread_create
118 # pragma weak pthread_sigmask
119 # pragma weak pthread_join
120 # ifndef pthread_self
121 # pragma weak pthread_self
123 # pragma weak pthread_exit
124 # if HAVE_PTHREAD_ATFORK
125 # pragma weak pthread_atfork
128 # if !PTHREAD_IN_USE_DETECTION_HARD
129 # pragma weak pthread_cancel
130 # define pthread_in_use() (pthread_cancel != NULL)
135 # if !PTHREAD_IN_USE_DETECTION_HARD
136 # define pthread_in_use() 1
141 /* -------------------------- gl_thread_t datatype -------------------------- */
143 /* This choice of gl_thread_t assumes that
144 pthread_equal (a, b) is equivalent to ((a) == (b)).
145 This is the case on all platforms in use in 2008. */
146 typedef pthread_t gl_thread_t;
147 # define glthread_create(THREADP, FUNC, ARG) \
148 (pthread_in_use () ? pthread_create (THREADP, NULL, FUNC, ARG) : ENOSYS)
149 # define glthread_sigmask(HOW, SET, OSET) \
150 (pthread_in_use () ? pthread_sigmask (HOW, SET, OSET) : 0)
151 # define glthread_join(THREAD, RETVALP) \
152 (pthread_in_use () ? pthread_join (THREAD, RETVALP) : 0)
153 # ifdef PTW32_VERSION
154 /* In pthreads-win32, pthread_t is a struct with a pointer field 'p' and
156 # define gl_thread_self() \
157 (pthread_in_use () ? pthread_self () : gl_null_thread)
158 # define gl_thread_self_pointer() \
159 (pthread_in_use () ? pthread_self ().p : NULL)
160 extern const gl_thread_t gl_null_thread;
162 # define gl_thread_self() \
163 (pthread_in_use () ? pthread_self () : (pthread_t) NULL)
164 # define gl_thread_self_pointer() \
165 (pthread_in_use () ? (void *) pthread_self () : NULL)
167 # define gl_thread_exit(RETVAL) \
168 (pthread_in_use () ? pthread_exit (RETVAL) : 0)
170 # if HAVE_PTHREAD_ATFORK
171 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \
172 (pthread_in_use () ? pthread_atfork (PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) : 0)
174 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
183 /* ========================================================================= */
187 /* Use the GNU Pth threads library. */
195 # if USE_PTH_THREADS_WEAK
197 /* Use weak references to the GNU Pth threads library. */
199 # pragma weak pth_spawn
200 # pragma weak pth_sigmask
201 # pragma weak pth_join
202 # pragma weak pth_self
203 # pragma weak pth_exit
205 # pragma weak pth_cancel
206 # define pth_in_use() (pth_cancel != NULL)
210 # define pth_in_use() 1
213 /* -------------------------- gl_thread_t datatype -------------------------- */
215 typedef pth_t gl_thread_t;
216 # define glthread_create(THREADP, FUNC, ARG) \
217 (pth_in_use () ? ((*(THREADP) = pth_spawn (NULL, FUNC, ARG)) ? 0 : errno) : 0)
218 # define glthread_sigmask(HOW, SET, OSET) \
219 (pth_in_use () && !pth_sigmask (HOW, SET, OSET) ? errno : 0)
220 # define glthread_join(THREAD, RETVALP) \
221 (pth_in_use () && !pth_join (THREAD, RETVALP) ? errno : 0)
222 # define gl_thread_self() \
223 (pth_in_use () ? (void *) pth_self () : NULL)
224 # define gl_thread_self_pointer() \
226 # define gl_thread_exit(RETVAL) \
227 (pth_in_use () ? pth_exit (RETVAL) : 0)
228 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
236 /* ========================================================================= */
238 #if USE_SOLARIS_THREADS
240 /* Use the old Solaris threads library. */
249 # if USE_SOLARIS_THREADS_WEAK
251 /* Use weak references to the old Solaris threads library. */
253 # pragma weak thr_create
254 # pragma weak thr_join
255 # pragma weak thr_self
256 # pragma weak thr_exit
258 # pragma weak thr_suspend
259 # define thread_in_use() (thr_suspend != NULL)
263 # define thread_in_use() 1
267 /* -------------------------- gl_thread_t datatype -------------------------- */
269 typedef thread_t gl_thread_t;
270 # define glthread_create(THREADP, FUNC, ARG) \
271 (thread_in_use () ? thr_create (NULL, 0, FUNC, ARG, 0, THREADP) : 0)
272 # define glthread_sigmask(HOW, SET, OSET) \
273 (thread_in_use () ? sigprocmask (HOW, SET, OSET) : 0)
274 # define glthread_join(THREAD, RETVALP) \
275 (thread_in_use () ? thr_join (THREAD, NULL, RETVALP) : 0)
276 # define gl_thread_self() \
277 (thread_in_use () ? (void *) thr_self () : NULL)
278 # define gl_thread_self_pointer() \
280 # define gl_thread_exit(RETVAL) \
281 (thread_in_use () ? thr_exit (RETVAL) : 0)
282 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
290 /* ========================================================================= */
292 #if USE_WINDOWS_THREADS
294 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
295 # include <windows.h>
301 /* -------------------------- gl_thread_t datatype -------------------------- */
303 /* The gl_thread_t is a pointer to a structure in memory.
304 Why not the thread handle? If it were the thread handle, it would be hard
305 to implement gl_thread_self() (since GetCurrentThread () returns a pseudo-
306 handle, DuplicateHandle (GetCurrentThread ()) returns a handle that must be
307 closed afterwards, and there is no function for quickly retrieving a thread
309 Why not the thread id? I tried it. It did not work: Sometimes ids appeared
310 that did not belong to running threads, and glthread_join failed with ESRCH.
312 typedef struct gl_thread_struct *gl_thread_t;
313 # define glthread_create(THREADP, FUNC, ARG) \
314 glthread_create_func (THREADP, FUNC, ARG)
315 # define glthread_sigmask(HOW, SET, OSET) \
317 # define glthread_join(THREAD, RETVALP) \
318 glthread_join_func (THREAD, RETVALP)
319 # define gl_thread_self() \
320 gl_thread_self_func ()
321 # define gl_thread_self_pointer() \
323 # define gl_thread_exit(RETVAL) \
324 gl_thread_exit_func (RETVAL)
325 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
326 extern int glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg);
327 extern int glthread_join_func (gl_thread_t thread, void **retvalp);
328 extern gl_thread_t gl_thread_self_func (void);
329 extern int gl_thread_exit_func (void *retval);
337 /* ========================================================================= */
339 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
341 /* Provide dummy implementation if threads are not supported. */
343 typedef int gl_thread_t;
344 # define glthread_create(THREADP, FUNC, ARG) ENOSYS
345 # define glthread_sigmask(HOW, SET, OSET) 0
346 # define glthread_join(THREAD, RETVALP) 0
347 # define gl_thread_self() 0
348 # define gl_thread_self_pointer() \
349 ((void *) gl_thread_self ())
350 # define gl_thread_exit(RETVAL) 0
351 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
355 /* ========================================================================= */
357 /* Macros with built-in error handling. */
363 static inline gl_thread_t
364 gl_thread_create (void *(*func) (void *arg), void *arg)
369 ret = glthread_create (&thread, func, arg);
374 #define gl_thread_sigmask(HOW, SET, OSET) \
377 if (glthread_sigmask (HOW, SET, OSET)) \
381 #define gl_thread_join(THREAD, RETVAL) \
384 if (glthread_join (THREAD, RETVAL)) \
388 #define gl_thread_atfork(PREPARE, PARENT, CHILD) \
391 if (glthread_atfork (PREPARE, PARENT, CHILD)) \
400 #endif /* _GLTHREAD_THREAD_H */