1 /* Creating and controlling threads.
2 Copyright (C) 2005-2020 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 <https://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-win32.h. */
20 /* This file contains primitives for creating and controlling threads.
22 Thread data type: gl_thread_t.
25 thread = gl_thread_create (func, arg);
26 Or with control of error handling:
27 err = glthread_create (&thread, func, arg);
28 extern int glthread_create (gl_thread_t *result,
29 void *(*func) (void *), void *arg);
31 Querying and changing the signal mask of a thread (not supported on all
33 gl_thread_sigmask (how, newmask, oldmask);
34 Or with control of error handling:
35 err = glthread_sigmask (how, newmask, oldmask);
36 extern int glthread_sigmask (int how, const sigset_t *newmask, sigset_t *oldmask);
38 Waiting for termination of another thread:
39 gl_thread_join (thread, &return_value);
40 Or with control of error handling:
41 err = glthread_join (thread, &return_value);
42 extern int glthread_join (gl_thread_t thread, void **return_value_ptr);
44 Getting a reference to the current thread:
45 current = gl_thread_self ();
46 extern gl_thread_t gl_thread_self (void);
48 Getting a reference to the current thread as a pointer, for debugging:
49 ptr = gl_thread_self_pointer ();
50 extern void * gl_thread_self_pointer (void);
52 Terminating the current thread:
53 gl_thread_exit (return_value);
54 extern _Noreturn void gl_thread_exit (void *return_value);
56 Requesting custom code to be executed at fork() time (not supported on all
58 gl_thread_atfork (prepare_func, parent_func, child_func);
59 Or with control of error handling:
60 err = glthread_atfork (prepare_func, parent_func, child_func);
61 extern int glthread_atfork (void (*prepare_func) (void),
62 void (*parent_func) (void),
63 void (*child_func) (void));
64 Note that even on platforms where this is supported, use of fork() and
65 threads together is problematic, see
66 <https://lists.gnu.org/r/bug-gnulib/2008-08/msg00062.html>
70 #ifndef _GLTHREAD_THREAD_H
71 #define _GLTHREAD_THREAD_H
76 #if !defined c11_threads_in_use
77 # if HAVE_THREADS_H && USE_POSIX_THREADS_WEAK
79 # pragma weak thrd_exit
80 # define c11_threads_in_use() (thrd_exit != NULL)
82 # define c11_threads_in_use() 0
86 #ifndef _GL_INLINE_HEADER_BEGIN
87 #error "Please include config.h first."
89 _GL_INLINE_HEADER_BEGIN
90 #ifndef _GLTHREAD_THREAD_INLINE
91 # define _GLTHREAD_THREAD_INLINE _GL_INLINE
94 /* ========================================================================= */
98 /* Use the ISO C threads library. */
100 # include <threads.h>
106 /* -------------------------- gl_thread_t datatype -------------------------- */
108 typedef struct thrd_with_exitvalue
*gl_thread_t
;
109 extern int glthread_create (gl_thread_t
*threadp
,
110 void *(*func
) (void *), void *arg
);
111 # define glthread_sigmask(HOW, SET, OSET) \
112 pthread_sigmask (HOW, SET, OSET)
113 extern int glthread_join (gl_thread_t thread
, void **return_value_ptr
);
114 extern gl_thread_t
gl_thread_self (void);
115 # define gl_thread_self_pointer() \
116 (void *) gl_thread_self ()
117 extern _Noreturn
void gl_thread_exit (void *return_value
);
118 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
126 /* ========================================================================= */
128 #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
130 /* Use the POSIX threads library. */
132 # include <pthread.h>
134 /* On IRIX, pthread_atfork is declared in <unistd.h>, not in <pthread.h>. */
139 # if USE_POSIX_THREADS_WEAK
140 /* Compilers other than GCC need to see the declaration of pthread_sigmask
141 before the "#pragma weak pthread_sigmask" below. */
149 # if PTHREAD_IN_USE_DETECTION_HARD
151 /* The pthread_in_use() detection needs to be done at runtime. */
152 # define pthread_in_use() \
154 extern int glthread_in_use (void);
158 # if USE_POSIX_THREADS_WEAK
160 /* Use weak references to the POSIX threads library. */
162 /* Weak references avoid dragging in external libraries if the other parts
163 of the program don't use them. Here we use them, because we don't want
164 every program that uses libintl to depend on libpthread. This assumes
165 that libpthread would not be loaded after libintl; i.e. if libintl is
166 loaded first, by an executable that does not depend on libpthread, and
167 then a module is dynamically loaded that depends on libpthread, libintl
168 will not be multithread-safe. */
170 /* The way to test at runtime whether libpthread is present is to test
171 whether a function pointer's value, such as &pthread_mutex_init, is
172 non-NULL. However, some versions of GCC have a bug through which, in
173 PIC mode, &foo != NULL always evaluates to true if there is a direct
174 call to foo(...) in the same function. To avoid this, we test the
175 address of a function in libpthread that we don't use. */
177 # ifndef pthread_sigmask /* Do not declare rpl_pthread_sigmask weak. */
178 # pragma weak pthread_sigmask
181 # pragma weak pthread_join
182 # ifndef pthread_self
183 # pragma weak pthread_self
185 # pragma weak pthread_exit
186 # if HAVE_PTHREAD_ATFORK
187 # pragma weak pthread_atfork
190 # if !PTHREAD_IN_USE_DETECTION_HARD
191 # pragma weak pthread_mutexattr_gettype
192 # define pthread_in_use() \
193 (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
198 # if !PTHREAD_IN_USE_DETECTION_HARD
199 # define pthread_in_use() 1
204 /* -------------------------- gl_thread_t datatype -------------------------- */
206 /* This choice of gl_thread_t assumes that
207 pthread_equal (a, b) is equivalent to ((a) == (b)).
208 This is the case on all platforms in use in 2008. */
209 typedef pthread_t gl_thread_t
;
210 # define glthread_create(THREADP, FUNC, ARG) \
211 (pthread_in_use () ? pthread_create (THREADP, NULL, FUNC, ARG) : ENOSYS)
212 # define glthread_sigmask(HOW, SET, OSET) \
213 (pthread_in_use () ? pthread_sigmask (HOW, SET, OSET) : 0)
214 # define glthread_join(THREAD, RETVALP) \
215 (pthread_in_use () ? pthread_join (THREAD, RETVALP) : 0)
216 # ifdef PTW32_VERSION
217 /* In pthreads-win32, pthread_t is a struct with a pointer field 'p' and
219 # define gl_thread_self() \
220 (pthread_in_use () ? pthread_self () : gl_null_thread)
221 # define gl_thread_self_pointer() \
222 (pthread_in_use () ? pthread_self ().p : NULL)
223 extern const gl_thread_t gl_null_thread
;
224 # elif defined __MVS__
225 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
226 The first three bytes of this field appear to uniquely identify a
227 pthread_t, though not necessarily representing a pointer. */
228 # define gl_thread_self() \
229 (pthread_in_use () ? pthread_self () : gl_null_thread)
230 # define gl_thread_self_pointer() \
231 (pthread_in_use () ? *((void **) pthread_self ().__) : NULL)
232 extern const gl_thread_t gl_null_thread
;
234 # define gl_thread_self() \
235 (pthread_in_use () ? pthread_self () : (pthread_t) 0)
236 # define gl_thread_self_pointer() \
237 (pthread_in_use () ? (void *) pthread_self () : NULL)
239 # define gl_thread_exit(RETVAL) \
240 (void) (pthread_in_use () ? (pthread_exit (RETVAL), 0) : 0)
242 # if HAVE_PTHREAD_ATFORK
243 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \
244 (pthread_in_use () ? pthread_atfork (PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) : 0)
246 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
255 /* ========================================================================= */
257 #if USE_WINDOWS_THREADS
259 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
260 # include <windows.h>
262 # include "windows-thread.h"
268 /* -------------------------- gl_thread_t datatype -------------------------- */
270 typedef glwthread_thread_t gl_thread_t
;
271 # define glthread_create(THREADP, FUNC, ARG) \
272 glwthread_thread_create (THREADP, 0, FUNC, ARG)
273 # define glthread_sigmask(HOW, SET, OSET) \
275 # define glthread_join(THREAD, RETVALP) \
276 glwthread_thread_join (THREAD, RETVALP)
277 # define gl_thread_self() \
278 glwthread_thread_self ()
279 # define gl_thread_self_pointer() \
281 # define gl_thread_exit(RETVAL) \
282 glwthread_thread_exit (RETVAL)
283 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
291 /* ========================================================================= */
293 #if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
295 /* Provide dummy implementation if threads are not supported. */
297 typedef int gl_thread_t
;
298 # define glthread_create(THREADP, FUNC, ARG) ENOSYS
299 # define glthread_sigmask(HOW, SET, OSET) 0
300 # define glthread_join(THREAD, RETVALP) 0
301 # define gl_thread_self() 0
302 # define gl_thread_self_pointer() \
303 ((void *) gl_thread_self ())
304 # define gl_thread_exit(RETVAL) (void)0
305 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
309 /* ========================================================================= */
311 /* Macros with built-in error handling. */
317 _GLTHREAD_THREAD_INLINE gl_thread_t
318 gl_thread_create (void *(*func
) (void *arg
), void *arg
)
323 ret
= glthread_create (&thread
, func
, arg
);
328 #define gl_thread_sigmask(HOW, SET, OSET) \
331 if (glthread_sigmask (HOW, SET, OSET)) \
335 #define gl_thread_join(THREAD, RETVAL) \
338 if (glthread_join (THREAD, RETVAL)) \
342 #define gl_thread_atfork(PREPARE, PARENT, CHILD) \
345 if (glthread_atfork (PREPARE, PARENT, CHILD)) \
354 _GL_INLINE_HEADER_END
356 #endif /* _GLTHREAD_THREAD_H */