1 # threadlib.m4 serial 24
2 dnl Copyright (C) 2005-2019 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
13 dnl Tests for the libraries needs for using the POSIX threads API.
14 dnl Sets the variable LIBPTHREAD to the linker options for use in a Makefile.
15 dnl Sets the variable LIBPMULTITHREAD, for programs that really need
16 dnl multithread functionality. The difference between LIBPTHREAD and
17 dnl LIBPMULTITHREAD is that on platforms supporting weak symbols, typically
18 dnl LIBPTHREAD is empty whereas LIBPMULTITHREAD is not.
19 dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
20 dnl multithread-safe programs.
21 dnl Defines the C macro HAVE_PTHREAD_API if (at least parts of) the POSIX
22 dnl threads API is available.
26 dnl Tests for a multithreading library to be used.
27 dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
28 dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
29 dnl default is 'no', otherwise it is system dependent. In both cases, the user
30 dnl can change the choice through the options --enable-threads=choice or
31 dnl --disable-threads.
32 dnl Defines at most one of the macros USE_ISOC_THREADS, USE_POSIX_THREADS,
33 dnl USE_ISOC_AND_POSIX_THREADS, USE_WINDOWS_THREADS.
34 dnl The choice --enable-threads=isoc+posix is available only on platforms that
35 dnl have both the ISO C and the POSIX threads APIs. It has the effect of using
36 dnl the ISO C API for most things and the POSIX API only for creating and
37 dnl controlling threads (because there is no equivalent to pthread_atfork in
39 dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
40 dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
42 dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
43 dnl programs that really need multithread functionality. The difference
44 dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
45 dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
46 dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
47 dnl multithread-safe programs.
48 dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the
49 dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the
50 dnl same value. Only system libraries are needed.
52 AC_DEFUN([gl_ANYTHREADLIB_EARLY],
54 AC_REQUIRE([AC_CANONICAL_HOST])
55 if test -z "$gl_anythreadlib_early_done"; then
58 # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
59 # groks <pthread.h>. cc also understands the flag -pthread, but
60 # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
61 # 2. putting a flag into CPPFLAGS that has an effect on the linker
62 # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
63 # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
64 CPPFLAGS="$CPPFLAGS -D_REENTRANT"
67 # Some systems optimize for single-threaded programs by default, and
68 # need special flags to disable these optimizations. For example, the
69 # definition of 'errno' in <errno.h>.
71 aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
72 solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
74 gl_anythreadlib_early_done=done
78 AC_DEFUN([gl_THREADLIB_EARLY],
80 AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
83 dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
85 AC_DEFUN([gl_THREADLIB_EARLY_BODY],
87 dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
88 dnl influences the result of the autoconf tests that test for *_unlocked
89 dnl declarations, on AIX 5 at least. Therefore it must come early.
90 AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
91 AC_BEFORE([$0], [gl_ARGP])dnl
93 AC_REQUIRE([AC_CANONICAL_HOST])
94 dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
95 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
96 dnl Check for multithreading.
97 m4_ifdef([gl_THREADLIB_DEFAULT_NO],
98 [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
99 [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
100 m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=])
101 AC_ARG_ENABLE([threads],
102 AC_HELP_STRING([--enable-threads={isoc|posix|isoc+posix|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
103 AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
104 [gl_use_threads=$enableval],
105 [if test -n "$gl_use_threads_default"; then
106 gl_use_threads="$gl_use_threads_default"
110 dnl Disable multithreading by default on OSF/1, because it interferes
111 dnl with fork()/exec(): When msgexec is linked with -lpthread, its
112 dnl child process gets an endless segmentation fault inside execvp().
113 osf*) gl_use_threads=no ;;
114 dnl Disable multithreading by default on Cygwin 1.5.x, because it has
115 dnl bugs that lead to endless loops or crashes. See
116 dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
119 1.[0-5].*) gl_use_threads=no ;;
120 *) gl_use_threads=yes ;;
123 dnl Obey gl_AVOID_WINPTHREAD on mingw.
125 case "$gl_use_winpthreads_default" in
126 yes) gl_use_threads=posix ;;
127 no) gl_use_threads=windows ;;
128 *) gl_use_threads=yes ;;
131 *) gl_use_threads=yes ;;
136 if test "$gl_use_threads" = yes \
137 || test "$gl_use_threads" = isoc \
138 || test "$gl_use_threads" = posix \
139 || test "$gl_use_threads" = isoc+posix; then
140 # For using <threads.h> or <pthread.h>:
141 gl_ANYTHREADLIB_EARLY
145 dnl Checks whether the compiler and linker support weak declarations of symbols.
147 AC_DEFUN([gl_WEAK_SYMBOLS],
149 AC_CACHE_CHECK([whether imported symbols can be declared weak],
152 dnl First, test whether the compiler accepts it syntactically.
155 [[extern void xyzzy ();
156 #pragma weak xyzzy]],
158 [gl_cv_have_weak=maybe])
159 if test $gl_cv_have_weak = maybe; then
160 dnl Second, test whether it actually works. On Cygwin 1.7.2, with
161 dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
168 return (fputs == NULL);
170 [gl_cv_have_weak=yes],
171 [gl_cv_have_weak=no],
172 [dnl When cross-compiling, assume that only ELF platforms support
174 AC_EGREP_CPP([Extensible Linking Format],
176 Extensible Linking Format
179 [gl_cv_have_weak="guessing yes"],
180 [gl_cv_have_weak="guessing no"])
183 dnl But when linking statically, weak symbols don't work.
185 *" -static "*) gl_cv_have_weak=no ;;
188 case "$gl_cv_have_weak" in
190 AC_DEFINE([HAVE_WEAK_SYMBOLS], [1],
191 [Define to 1 if the compiler and linker support weak declarations of symbols.])
196 dnl The guts of gl_PTHREADLIB. Needs to be expanded only once.
198 AC_DEFUN([gl_PTHREADLIB_BODY],
200 AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
201 if test -z "$gl_threadlib_body_done"; then
205 # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
206 # it groks <pthread.h>. It's added above, in gl_ANYTHREADLIB_EARLY.
207 AC_CHECK_HEADER([pthread.h],
208 [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
209 if test "$gl_have_pthread_h" = yes; then
210 # Other possible tests:
211 # -lpthreads (FSU threads, PCthreads)
213 # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
214 # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
215 # the second one only in libpthread, and lock.c needs it.
217 # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
218 # needs -pthread for some reason. See:
219 # https://lists.gnu.org/r/bug-gnulib/2014-09/msg00023.html
221 for gl_pthread in '' '-pthread'; do
222 LIBS="$LIBS $gl_pthread"
225 [[#include <pthread.h>
227 pthread_mutexattr_t ma;
229 [[pthread_mutex_lock (&m);
230 pthread_mutexattr_init (&ma);]])],
232 LIBPTHREAD=$gl_pthread
233 LIBPMULTITHREAD=$gl_pthread])
235 test $gl_pthread_api = yes && break
238 # Test for libpthread by looking for pthread_kill. (Not pthread_self,
239 # since it is defined as a macro on OSF/1.)
240 if test $gl_pthread_api = yes && test -z "$LIBPTHREAD"; then
241 # The program links fine without libpthread. But it may actually
242 # need to link with libpthread in order to create multiple threads.
243 AC_CHECK_LIB([pthread], [pthread_kill],
244 [LIBPMULTITHREAD=-lpthread
245 # On Solaris and HP-UX, most pthread functions exist also in libc.
246 # Therefore pthread_in_use() needs to actually try to create a
247 # thread: pthread_create from libc will fail, whereas
248 # pthread_create will actually create a thread.
249 # On Solaris 10 or newer, this test is no longer needed, because
250 # libc contains the fully functional pthread functions.
252 solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
253 AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
254 [Define if the pthread_in_use() detection is hard.])
257 elif test $gl_pthread_api != yes; then
258 # Some library is needed. Try libpthread and libc_r.
259 AC_CHECK_LIB([pthread], [pthread_kill],
262 LIBPMULTITHREAD=-lpthread])
263 if test $gl_pthread_api != yes; then
265 AC_CHECK_LIB([c_r], [pthread_kill],
268 LIBPMULTITHREAD=-lc_r])
272 AC_MSG_CHECKING([whether POSIX threads API is available])
273 AC_MSG_RESULT([$gl_pthread_api])
274 AC_SUBST([LIBPTHREAD])
275 AC_SUBST([LIBPMULTITHREAD])
276 if test $gl_pthread_api = yes; then
277 AC_DEFINE([HAVE_PTHREAD_API], [1],
278 [Define if you have the <pthread.h> header and the POSIX threads API.])
280 gl_threadlib_body_done=done
284 dnl The guts of gl_THREADLIB. Needs to be expanded only once.
286 AC_DEFUN([gl_THREADLIB_BODY],
288 AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
294 if test "$gl_use_threads" != no; then
295 dnl Check whether the compiler and linker support weak declarations.
297 if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
298 dnl If we use weak symbols to implement pthread_in_use / pth_in_use /
299 dnl thread_in_use, we also need to test whether the ISO C 11 thrd_create
300 dnl facility is in use.
301 AC_CHECK_HEADERS_ONCE([threads.h])
304 if test "$gl_use_threads" = isoc || test "$gl_use_threads" = isoc+posix; then
305 AC_CHECK_HEADERS_ONCE([threads.h])
306 if test $ac_cv_header_threads_h = yes; then
307 gl_have_isoc_threads=
308 # Test whether both mtx_lock and cnd_timedwait exist in libc.
311 [[#include <threads.h>
317 cnd_timedwait (&c, &m, NULL);]])],
318 [gl_have_isoc_threads=yes])
321 if test "$gl_use_threads" = yes \
322 || test "$gl_use_threads" = posix \
323 || test "$gl_use_threads" = isoc+posix; then
325 LIBTHREAD=$LIBPTHREAD LTLIBTHREAD=$LIBPTHREAD
326 LIBMULTITHREAD=$LIBPMULTITHREAD LTLIBMULTITHREAD=$LIBPMULTITHREAD
327 if test $gl_pthread_api = yes; then
328 if test "$gl_use_threads" = isoc+posix && test "$gl_have_isoc_threads" = yes; then
329 gl_threads_api='isoc+posix'
330 AC_DEFINE([USE_ISOC_AND_POSIX_THREADS], [1],
331 [Define if the combination of the ISO C and POSIX multithreading APIs can be used.])
332 LIBTHREAD= LTLIBTHREAD=
335 AC_DEFINE([USE_POSIX_THREADS], [1],
336 [Define if the POSIX multithreading library can be used.])
337 if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
338 if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
339 AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
340 [Define if references to the POSIX multithreading library should be made weak.])
341 LIBTHREAD= LTLIBTHREAD=
347 if test $gl_threads_api = none; then
348 if test "$gl_use_threads" = isoc && test "$gl_have_isoc_threads" = yes; then
350 AC_DEFINE([USE_ISOC_THREADS], [1],
351 [Define if the ISO C multithreading library can be used.])
354 if test $gl_threads_api = none; then
355 case "$gl_use_threads" in
356 yes | windows | win32) # The 'win32' is for backward compatibility.
357 if { case "$host_os" in
362 gl_threads_api=windows
363 AC_DEFINE([USE_WINDOWS_THREADS], [1],
364 [Define if the native Windows multithreading API can be used.])
370 AC_MSG_CHECKING([for multithread API to use])
371 AC_MSG_RESULT([$gl_threads_api])
372 AC_SUBST([LIBTHREAD])
373 AC_SUBST([LTLIBTHREAD])
374 AC_SUBST([LIBMULTITHREAD])
375 AC_SUBST([LTLIBMULTITHREAD])
378 AC_DEFUN([gl_PTHREADLIB],
380 AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
384 AC_DEFUN([gl_THREADLIB],
386 AC_REQUIRE([gl_THREADLIB_EARLY])
387 AC_REQUIRE([gl_THREADLIB_BODY])
391 dnl gl_DISABLE_THREADS
392 dnl ------------------
393 dnl Sets the gl_THREADLIB default so that threads are not used by default.
394 dnl The user can still override it at installation time, by using the
395 dnl configure option '--enable-threads'.
397 AC_DEFUN([gl_DISABLE_THREADS], [
398 m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
402 dnl gl_AVOID_WINPTHREAD
403 dnl -------------------
404 dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the
405 dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided.
406 dnl The user can still override it at installation time, by using the
407 dnl configure option '--enable-threads'.
409 AC_DEFUN([gl_AVOID_WINPTHREAD], [
410 m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no])
414 dnl Survey of platforms:
416 dnl Platform Available Compiler Supports test-lock
417 dnl flavours option weak result
418 dnl --------------- --------- --------- -------- ---------
419 dnl Linux 2.4/glibc posix -lpthread Y OK
421 dnl GNU Hurd/glibc posix
423 dnl Ubuntu 14.04 posix -pthread Y OK
425 dnl FreeBSD 5.3 posix -lc_r Y
427 dnl posix -lpthread ? Y
430 dnl FreeBSD 5.2 posix -lc_r Y
434 dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
438 dnl OpenBSD 3.4 posix -lpthread Y OK
440 dnl Mac OS X 10.[123] posix -lpthread Y OK
442 dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
444 dnl HP-UX 11 posix -lpthread N (cc) OK
447 dnl IRIX 6.5 posix -lpthread Y 0.5
449 dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
451 dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
452 dnl -lpthread (gcc) Y
454 dnl Cygwin posix -lpthread Y OK
456 dnl Mingw windows N OK
460 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
462 dnl OK if all three tests terminate OK,
463 dnl 0.5 if the first test terminates OK but the second one loops endlessly,
464 dnl 0.0 if the first test already loops endlessly.