1# threadlib.m4 serial 31
2dnl Copyright (C) 2005-2021 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Bruno Haible.
8
9AC_PREREQ([2.60])
10
11dnl The general structure of the multithreading modules in gnulib is that we
12dnl have three set of modules:
13dnl
14dnl   * POSIX API:
15dnl     pthread, which combines
16dnl       pthread-h
17dnl       pthread-thread
18dnl       pthread-once
19dnl       pthread-mutex
20dnl       pthread-rwlock
21dnl       pthread-cond
22dnl       pthread-tss
23dnl       pthread-spin
24dnl     sched_yield
25dnl
26dnl   * ISO C API:
27dnl     threads, which combines
28dnl       threads-h
29dnl       thrd
30dnl       mtx
31dnl       cnd
32dnl       tss
33dnl
34dnl   * Gnulib API, with an implementation that can be chosen at configure
35dnl     time through the option --enable-threads=...
36dnl       thread
37dnl       lock
38dnl       cond
39dnl       tls
40dnl       yield
41dnl
42dnl They are independent, except for the fact that
43dnl   - the implementation of the ISO C API may use the POSIX (or some other
44dnl     platform dependent) API,
45dnl   - the implementation of the Gnulib API may use the POSIX or ISO C or
46dnl     some other platform dependent API, depending on the --enable-threads
47dnl     option.
48dnl
49dnl This file contains macros for all of these APIs!
50
51dnl ============================================================================
52dnl Macros for all thread APIs
53
54AC_DEFUN([gl_ANYTHREADLIB_EARLY],
55[
56  AC_REQUIRE([AC_CANONICAL_HOST])
57  if test -z "$gl_anythreadlib_early_done"; then
58    case "$host_os" in
59      osf*)
60        # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
61        # groks <pthread.h>. cc also understands the flag -pthread, but
62        # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
63        # 2. putting a flag into CPPFLAGS that has an effect on the linker
64        # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
65        # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
66        THREADLIB_CPPFLAGS="$THREADLIB_CPPFLAGS -D_REENTRANT"
67        ;;
68    esac
69    # Some systems optimize for single-threaded programs by default, and
70    # need special flags to disable these optimizations. For example, the
71    # definition of 'errno' in <errno.h>.
72    case "$host_os" in
73      aix* | freebsd*) THREADLIB_CPPFLAGS="$THREADLIB_CPPFLAGS -D_THREAD_SAFE" ;;
74      solaris*) THREADLIB_CPPFLAGS="$THREADLIB_CPPFLAGS -D_REENTRANT" ;;
75    esac
76    gl_anythreadlib_early_done=done
77  fi
78  if test x"$THREADLIB_CPPFLAGS" != x ; then
79    CPPFLAGS="$CPPFLAGS $THREADLIB_CPPFLAGS"
80  fi
81])
82
83dnl Checks whether the compiler and linker support weak declarations of symbols.
84
85AC_DEFUN([gl_WEAK_SYMBOLS],
86[
87  AC_REQUIRE([AC_CANONICAL_HOST])
88  AC_CACHE_CHECK([whether imported symbols can be declared weak],
89    [gl_cv_have_weak],
90    [gl_cv_have_weak=no
91     dnl First, test whether the compiler accepts it syntactically.
92     AC_LINK_IFELSE(
93       [AC_LANG_PROGRAM(
94          [[extern void xyzzy ();
95#pragma weak xyzzy]],
96          [[xyzzy();]])],
97       [gl_cv_have_weak=maybe])
98     if test $gl_cv_have_weak = maybe; then
99       dnl Second, test whether it actually works. On Cygwin 1.7.2, with
100       dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
101       AC_RUN_IFELSE(
102         [AC_LANG_SOURCE([[
103#include <stdio.h>
104#pragma weak fputs
105int main ()
106{
107  return (fputs == NULL);
108}]])],
109         [gl_cv_have_weak=yes],
110         [gl_cv_have_weak=no],
111         [dnl When cross-compiling, assume that only ELF platforms support
112          dnl weak symbols.
113          AC_EGREP_CPP([Extensible Linking Format],
114            [#ifdef __ELF__
115             Extensible Linking Format
116             #endif
117            ],
118            [gl_cv_have_weak="guessing yes"],
119            [gl_cv_have_weak="guessing no"])
120         ])
121     fi
122     dnl But when linking statically, weak symbols don't work.
123     case " $LDFLAGS " in
124       *" -static "*) gl_cv_have_weak=no ;;
125     esac
126     dnl Test for a bug in FreeBSD 11: A link error occurs when using a weak
127     dnl symbol and linking against a shared library that has a dependency on
128     dnl the shared library that defines the symbol.
129     case "$gl_cv_have_weak" in
130       *yes)
131         case "$host_os" in
132           freebsd* | dragonfly* | midnightbsd*)
133             : > conftest1.c
134             $CC $CPPFLAGS $CFLAGS $LDFLAGS -fPIC -shared -o libempty.so conftest1.c -lpthread >&AS_MESSAGE_LOG_FD 2>&1
135             cat <<EOF > conftest2.c
136#include <pthread.h>
137#pragma weak pthread_mutexattr_gettype
138int main ()
139{
140  return (pthread_mutexattr_gettype != NULL);
141}
142EOF
143             $CC $CPPFLAGS $CFLAGS $LDFLAGS -o conftest conftest2.c libempty.so >&AS_MESSAGE_LOG_FD 2>&1 \
144               || gl_cv_have_weak=no
145             rm -f conftest1.c libempty.so conftest2.c conftest
146             ;;
147         esac
148         ;;
149     esac
150    ])
151  case "$gl_cv_have_weak" in
152    *yes)
153      AC_DEFINE([HAVE_WEAK_SYMBOLS], [1],
154        [Define to 1 if the compiler and linker support weak declarations of symbols.])
155      ;;
156  esac
157])
158
159dnl ============================================================================
160dnl Macros for the POSIX API
161
162dnl gl_PTHREADLIB
163dnl -------------
164dnl Tests for the libraries needs for using the POSIX threads API.
165dnl Sets the variable LIBPTHREAD to the linker options for use in a Makefile.
166dnl Sets the variable LIBPMULTITHREAD, for programs that really need
167dnl multithread functionality. The difference between LIBPTHREAD and
168dnl LIBPMULTITHREAD is that on platforms supporting weak symbols, typically
169dnl LIBPTHREAD is empty whereas LIBPMULTITHREAD is not.
170dnl Sets the variable LIB_SCHED_YIELD to the linker options needed to use the
171dnl sched_yield() function.
172dnl Sets the variable THREADLIB_CPPFLAGS the flag -D_REENTRANT or
173dnl -D_THREAD_SAFE if needed for multithread-safe programs and adds
174dnl THREADLIB_CPPFLAGS to CPPFLAGS.
175dnl Defines the C macro HAVE_PTHREAD_API if (at least parts of) the POSIX
176dnl threads API is available.
177
178dnl The guts of gl_PTHREADLIB. Needs to be expanded only once.
179
180AC_DEFUN([gl_PTHREADLIB_BODY],
181[
182  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
183  if test -z "$gl_pthreadlib_body_done"; then
184    gl_pthread_api=no
185    LIBPTHREAD=
186    LIBPMULTITHREAD=
187    # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
188    # it groks <pthread.h>. It's added above, in gl_ANYTHREADLIB_EARLY.
189    AC_CHECK_HEADER([pthread.h],
190      [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
191    if test "$gl_have_pthread_h" = yes; then
192      # Other possible tests:
193      #   -lpthreads (FSU threads, PCthreads)
194      #   -lgthreads
195      # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
196      # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
197      # the second one only in libpthread, and lock.c needs it.
198      #
199      # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
200      # needs -pthread for some reason.  See:
201      # https://lists.gnu.org/r/bug-gnulib/2014-09/msg00023.html
202      save_LIBS=$LIBS
203      for gl_pthread in '' '-pthread'; do
204        LIBS="$LIBS $gl_pthread"
205        AC_LINK_IFELSE(
206          [AC_LANG_PROGRAM(
207             [[#include <pthread.h>
208               pthread_mutex_t m;
209               pthread_mutexattr_t ma;
210             ]],
211             [[pthread_mutex_lock (&m);
212               pthread_mutexattr_init (&ma);]])],
213          [gl_pthread_api=yes
214           LIBPTHREAD=$gl_pthread
215           LIBPMULTITHREAD=$gl_pthread])
216        LIBS=$save_LIBS
217        test $gl_pthread_api = yes && break
218      done
219      echo "$as_me:__oline__: gl_pthread_api=$gl_pthread_api" >&AS_MESSAGE_LOG_FD
220      echo "$as_me:__oline__: LIBPTHREAD=$LIBPTHREAD" >&AS_MESSAGE_LOG_FD
221
222      gl_pthread_in_glibc=no
223      # On Linux with glibc >= 2.34, libc contains the fully functional
224      # pthread functions.
225      case "$host_os" in
226        linux*)
227          AC_EGREP_CPP([Lucky user],
228            [#include <features.h>
229             #ifdef __GNU_LIBRARY__
230              #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34) || (__GLIBC__ > 2)
231               Lucky user
232              #endif
233             #endif
234            ],
235            [gl_pthread_in_glibc=yes],
236            [])
237          ;;
238      esac
239      echo "$as_me:__oline__: gl_pthread_in_glibc=$gl_pthread_in_glibc" >&AS_MESSAGE_LOG_FD
240
241      # Test for libpthread by looking for pthread_kill. (Not pthread_self,
242      # since it is defined as a macro on OSF/1.)
243      if test $gl_pthread_api = yes && test -z "$LIBPTHREAD"; then
244        # The program links fine without libpthread. But it may actually
245        # need to link with libpthread in order to create multiple threads.
246        AC_CHECK_LIB([pthread], [pthread_kill],
247          [if test $gl_pthread_in_glibc = yes; then
248             LIBPMULTITHREAD=
249           else
250             LIBPMULTITHREAD=-lpthread
251             # On Solaris and HP-UX, most pthread functions exist also in libc.
252             # Therefore pthread_in_use() needs to actually try to create a
253             # thread: pthread_create from libc will fail, whereas
254             # pthread_create will actually create a thread.
255             # On Solaris 10 or newer, this test is no longer needed, because
256             # libc contains the fully functional pthread functions.
257             case "$host_os" in
258               solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
259                 AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
260                   [Define if the pthread_in_use() detection is hard.])
261             esac
262           fi
263          ])
264      elif test $gl_pthread_api != yes; then
265        # Some library is needed. Try libpthread and libc_r.
266        AC_CHECK_LIB([pthread], [pthread_kill],
267          [gl_pthread_api=yes
268           LIBPTHREAD=-lpthread
269           LIBPMULTITHREAD=-lpthread])
270        if test $gl_pthread_api != yes; then
271          # For FreeBSD 4.
272          AC_CHECK_LIB([c_r], [pthread_kill],
273            [gl_pthread_api=yes
274             LIBPTHREAD=-lc_r
275             LIBPMULTITHREAD=-lc_r])
276        fi
277      fi
278      echo "$as_me:__oline__: LIBPMULTITHREAD=$LIBPMULTITHREAD" >&AS_MESSAGE_LOG_FD
279    fi
280    AC_MSG_CHECKING([whether POSIX threads API is available])
281    AC_MSG_RESULT([$gl_pthread_api])
282    AC_SUBST([LIBPTHREAD])
283    AC_SUBST([LIBPMULTITHREAD])
284    if test $gl_pthread_api = yes; then
285      AC_DEFINE([HAVE_PTHREAD_API], [1],
286        [Define if you have the <pthread.h> header and the POSIX threads API.])
287    fi
288
289    dnl On some systems, sched_yield is in librt, rather than in libpthread.
290    AC_LINK_IFELSE(
291      [AC_LANG_PROGRAM(
292         [[#include <sched.h>]],
293         [[sched_yield ();]])],
294      [LIB_SCHED_YIELD=
295      ],
296      [dnl Solaris 7...10 has sched_yield in librt, not in libpthread or libc.
297       AC_CHECK_LIB([rt], [sched_yield], [LIB_SCHED_YIELD=-lrt],
298         [dnl Solaris 2.5.1, 2.6 has sched_yield in libposix4, not librt.
299          AC_CHECK_LIB([posix4], [sched_yield], [LIB_SCHED_YIELD=-lposix4])])
300      ])
301    AC_SUBST([LIB_SCHED_YIELD])
302
303    gl_pthreadlib_body_done=done
304  fi
305])
306
307AC_DEFUN([gl_PTHREADLIB],
308[
309  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
310  gl_PTHREADLIB_BODY
311])
312
313dnl ============================================================================
314dnl Macros for the ISO C API
315
316dnl gl_STDTHREADLIB
317dnl ---------------
318dnl Tests for the libraries needs for using the ISO C threads API.
319dnl Sets the variable LIBSTDTHREAD to the linker options for use in a Makefile.
320dnl Sets the variable THREADLIB_CPPFLAGS the flag -D_REENTRANT or
321dnl -D_THREAD_SAFE if needed for multithread-safe programs and adds
322dnl THREADLIB_CPPFLAGS to CPPFLAGS.
323dnl Defines the C macro HAVE_THREADS_H if (at least parts of) the ISO C threads
324dnl API is available.
325
326dnl The guts of gl_STDTHREADLIB. Needs to be expanded only once.
327
328AC_DEFUN([gl_STDTHREADLIB_BODY],
329[
330  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
331  AC_REQUIRE([AC_CANONICAL_HOST])
332  if test -z "$gl_stdthreadlib_body_done"; then
333    AC_CHECK_HEADERS_ONCE([threads.h])
334
335    case "$host_os" in
336      mingw*)
337        LIBSTDTHREAD=
338        ;;
339      *)
340        gl_PTHREADLIB_BODY
341        if test $ac_cv_header_threads_h = yes; then
342          dnl glibc >= 2.29 has thrd_create in libpthread.
343          dnl FreeBSD >= 10 has thrd_create in libstdthreads; this library depends
344          dnl on libpthread (for the symbol 'pthread_mutexattr_gettype').
345          dnl glibc >= 2.34, AIX >= 7.1, and Solaris >= 11.4 have thrd_create in
346          dnl libc.
347          AC_CHECK_FUNCS([thrd_create])
348          if test $ac_cv_func_thrd_create = yes; then
349            LIBSTDTHREAD=
350          else
351            AC_CHECK_LIB([stdthreads], [thrd_create], [
352              LIBSTDTHREAD='-lstdthreads -lpthread'
353            ], [
354              dnl Guess that thrd_create is in libpthread.
355              LIBSTDTHREAD="$LIBPMULTITHREAD"
356            ])
357          fi
358        else
359          dnl Libraries needed by thrd.c, mtx.c, cnd.c, tss.c.
360          LIBSTDTHREAD="$LIBPMULTITHREAD $LIB_SCHED_YIELD"
361        fi
362        ;;
363    esac
364    AC_SUBST([LIBSTDTHREAD])
365
366    AC_MSG_CHECKING([whether ISO C threads API is available])
367    AC_MSG_RESULT([$ac_cv_header_threads_h])
368    gl_stdthreadlib_body_done=done
369  fi
370])
371
372AC_DEFUN([gl_STDTHREADLIB],
373[
374  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
375  gl_STDTHREADLIB_BODY
376])
377
378dnl ============================================================================
379dnl Macros for the Gnulib API
380
381dnl gl_THREADLIB
382dnl ------------
383dnl Tests for a multithreading library to be used.
384dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
385dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
386dnl default is 'no', otherwise it is system dependent. In both cases, the user
387dnl can change the choice through the options --enable-threads=choice or
388dnl --disable-threads.
389dnl Defines at most one of the macros USE_ISOC_THREADS, USE_POSIX_THREADS,
390dnl USE_ISOC_AND_POSIX_THREADS, USE_WINDOWS_THREADS.
391dnl The choice --enable-threads=isoc+posix is available only on platforms that
392dnl have both the ISO C and the POSIX threads APIs. It has the effect of using
393dnl the ISO C API for most things and the POSIX API only for creating and
394dnl controlling threads (because there is no equivalent to pthread_atfork in
395dnl the ISO C API).
396dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
397dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
398dnl libtool).
399dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
400dnl programs that really need multithread functionality. The difference
401dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
402dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
403dnl Sets the variable THREADLIB_CPPFLAGS the flag -D_REENTRANT or
404dnl -D_THREAD_SAFE if needed for multithread-safe programs and adds
405dnl THREADLIB_CPPFLAGS to CPPFLAGS.
406dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the
407dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the
408dnl same value. Only system libraries are needed.
409
410AC_DEFUN([gl_THREADLIB_EARLY],
411[
412  AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
413])
414
415dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
416
417AC_DEFUN([gl_THREADLIB_EARLY_BODY],
418[
419  dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
420  dnl influences the result of the autoconf tests that test for *_unlocked
421  dnl declarations, on AIX 5 at least. Therefore it must come early.
422  AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
423  AC_BEFORE([$0], [gl_ARGP])dnl
424
425  AC_REQUIRE([AC_CANONICAL_HOST])
426  dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
427  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
428  dnl Check for multithreading.
429  THREADLIB_CPPFLAGS=""
430  m4_ifdef([gl_THREADLIB_DEFAULT_NO],
431    [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
432    [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
433  m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=])
434  AC_ARG_ENABLE([threads],
435AS_HELP_STRING([--enable-threads={isoc|posix|isoc+posix|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
436AS_HELP_STRING([--disable-threads], [build without multithread safety])]),
437    [gl_use_threads=$enableval],
438    [if test -n "$gl_use_threads_default"; then
439       gl_use_threads="$gl_use_threads_default"
440     else
441changequote(,)dnl
442       case "$host_os" in
443         dnl Disable multithreading by default on OSF/1, because it interferes
444         dnl with fork()/exec(): When msgexec is linked with -lpthread, its
445         dnl child process gets an endless segmentation fault inside execvp().
446         osf*) gl_use_threads=no ;;
447         dnl Disable multithreading by default on Cygwin 1.5.x, because it has
448         dnl bugs that lead to endless loops or crashes. See
449         dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
450         cygwin*)
451               case `uname -r` in
452                 1.[0-5].*) gl_use_threads=no ;;
453                 *)         gl_use_threads=yes ;;
454               esac
455               ;;
456         dnl Obey gl_AVOID_WINPTHREAD on mingw.
457         mingw*)
458               case "$gl_use_winpthreads_default" in
459                 yes) gl_use_threads=posix ;;
460                 no)  gl_use_threads=windows ;;
461                 *)   gl_use_threads=yes ;;
462               esac
463               ;;
464         *)    gl_use_threads=yes ;;
465       esac
466changequote([,])dnl
467     fi
468    ])
469  if test "$gl_use_threads" = yes \
470     || test "$gl_use_threads" = isoc \
471     || test "$gl_use_threads" = posix \
472     || test "$gl_use_threads" = isoc+posix; then
473    # For using <threads.h> or <pthread.h>:
474    gl_ANYTHREADLIB_EARLY
475  fi
476])
477
478dnl The guts of gl_THREADLIB. Needs to be expanded only once.
479
480AC_DEFUN([gl_THREADLIB_BODY],
481[
482  AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
483  gl_threads_api=none
484  LIBTHREAD=
485  LTLIBTHREAD=
486  LIBMULTITHREAD=
487  LTLIBMULTITHREAD=
488  if test "$gl_use_threads" != no; then
489    dnl Check whether the compiler and linker support weak declarations.
490    gl_WEAK_SYMBOLS
491    if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
492      dnl If we use weak symbols to implement pthread_in_use / pth_in_use /
493      dnl thread_in_use, we also need to test whether the ISO C 11 thrd_create
494      dnl facility is in use.
495      AC_CHECK_HEADERS_ONCE([threads.h])
496      :
497    fi
498    if test "$gl_use_threads" = isoc || test "$gl_use_threads" = isoc+posix; then
499      AC_CHECK_HEADERS_ONCE([threads.h])
500      gl_have_isoc_threads="$ac_cv_header_threads_h"
501    fi
502    if test "$gl_use_threads" = yes \
503       || test "$gl_use_threads" = posix \
504       || test "$gl_use_threads" = isoc+posix; then
505      gl_PTHREADLIB_BODY
506      LIBTHREAD=$LIBPTHREAD LTLIBTHREAD=$LIBPTHREAD
507      LIBMULTITHREAD=$LIBPMULTITHREAD LTLIBMULTITHREAD=$LIBPMULTITHREAD
508      if test $gl_pthread_api = yes; then
509        if test "$gl_use_threads" = isoc+posix && test "$gl_have_isoc_threads" = yes; then
510          gl_threads_api='isoc+posix'
511          AC_DEFINE([USE_ISOC_AND_POSIX_THREADS], [1],
512            [Define if the combination of the ISO C and POSIX multithreading APIs can be used.])
513          LIBTHREAD= LTLIBTHREAD=
514        else
515          gl_threads_api=posix
516          AC_DEFINE([USE_POSIX_THREADS], [1],
517            [Define if the POSIX multithreading library can be used.])
518          if test -z "$LIBMULTITHREAD" && test -z "$LTLIBMULTITHREAD"; then
519            AC_DEFINE([USE_POSIX_THREADS_FROM_LIBC], [1],
520              [Define if references to the POSIX multithreading library are satisfied by libc.])
521          else
522            if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
523              AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
524                [Define if references to the POSIX multithreading library should be made weak.])
525              LIBTHREAD= LTLIBTHREAD=
526            else
527              case "$host_os" in
528                freebsd* | dragonfly* | midnightbsd*)
529                  if test "x$LIBTHREAD" != "x$LIBMULTITHREAD"; then
530                    dnl If weak symbols can't tell whether pthread_create(), pthread_key_create()
531                    dnl etc. will succeed, we need a runtime test.
532                    AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
533                      [Define if the pthread_in_use() detection is hard.])
534                  fi
535                  ;;
536              esac
537            fi
538          fi
539        fi
540      fi
541    fi
542    if test $gl_threads_api = none; then
543      if test "$gl_use_threads" = isoc && test "$gl_have_isoc_threads" = yes; then
544        gl_STDTHREADLIB_BODY
545        LIBTHREAD=$LIBSTDTHREAD LTLIBTHREAD=$LIBSTDTHREAD
546        LIBMULTITHREAD=$LIBSTDTHREAD LTLIBMULTITHREAD=$LIBSTDTHREAD
547        gl_threads_api=isoc
548        AC_DEFINE([USE_ISOC_THREADS], [1],
549          [Define if the ISO C multithreading library can be used.])
550      fi
551    fi
552    if test $gl_threads_api = none; then
553      case "$gl_use_threads" in
554        yes | windows | win32) # The 'win32' is for backward compatibility.
555          if { case "$host_os" in
556                 mingw*) true;;
557                 *) false;;
558               esac
559             }; then
560            gl_threads_api=windows
561            AC_DEFINE([USE_WINDOWS_THREADS], [1],
562              [Define if the native Windows multithreading API can be used.])
563          fi
564          ;;
565      esac
566    fi
567  fi
568  AC_MSG_CHECKING([for multithread API to use])
569  AC_MSG_RESULT([$gl_threads_api])
570  AC_SUBST([LIBTHREAD])
571  AC_SUBST([LTLIBTHREAD])
572  AC_SUBST([LIBMULTITHREAD])
573  AC_SUBST([LTLIBMULTITHREAD])
574])
575
576AC_DEFUN([gl_THREADLIB],
577[
578  AC_REQUIRE([gl_THREADLIB_EARLY])
579  AC_REQUIRE([gl_THREADLIB_BODY])
580])
581
582
583dnl gl_DISABLE_THREADS
584dnl ------------------
585dnl Sets the gl_THREADLIB default so that threads are not used by default.
586dnl The user can still override it at installation time, by using the
587dnl configure option '--enable-threads'.
588
589AC_DEFUN([gl_DISABLE_THREADS], [
590  m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
591])
592
593
594dnl gl_AVOID_WINPTHREAD
595dnl -------------------
596dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the
597dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided.
598dnl The user can still override it at installation time, by using the
599dnl configure option '--enable-threads'.
600
601AC_DEFUN([gl_AVOID_WINPTHREAD], [
602  m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no])
603])
604
605
606dnl ============================================================================
607
608
609dnl Survey of platforms:
610dnl
611dnl Platform           Available  Compiler    Supports   test-lock
612dnl                    flavours   option      weak       result
613dnl ---------------    ---------  ---------   --------   ---------
614dnl Linux 2.4/glibc    posix      -lpthread       Y      OK
615dnl
616dnl Linux/glibc 2.34   posix                      Y      OK
617dnl
618dnl GNU Hurd/glibc     posix      -lpthread       Y      OK
619dnl
620dnl Ubuntu 14.04       posix      -pthread        Y      OK
621dnl
622dnl FreeBSD 5.3        posix      -lc_r           Y
623dnl                    posix      -lkse ?         Y
624dnl                    posix      -lpthread ?     Y
625dnl                    posix      -lthr           Y
626dnl
627dnl FreeBSD 5.2        posix      -lc_r           Y
628dnl                    posix      -lkse           Y
629dnl                    posix      -lthr           Y
630dnl
631dnl FreeBSD 4.0,4.10   posix      -lc_r           Y      OK
632dnl
633dnl NetBSD 1.6         --
634dnl
635dnl OpenBSD 3.4        posix      -lpthread       Y      OK
636dnl
637dnl Mac OS X 10.[123]  posix      -lpthread       Y      OK
638dnl
639dnl Solaris 7,8,9      posix      -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
640dnl
641dnl HP-UX 11           posix      -lpthread       N (cc) OK
642dnl                                               Y (gcc)
643dnl
644dnl IRIX 6.5           posix      -lpthread       Y      0.5
645dnl
646dnl AIX 4.3,5.1        posix      -lpthread       N      AIX 4: 0.5; AIX 5: OK
647dnl
648dnl OSF/1 4.0,5.1      posix      -pthread (cc)   N      OK
649dnl                               -lpthread (gcc) Y
650dnl
651dnl Cygwin             posix      -lpthread       Y      OK
652dnl
653dnl Mingw              windows                    N      OK
654dnl
655dnl BeOS 5             --
656dnl
657dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
658dnl turned off:
659dnl   OK if all three tests terminate OK,
660dnl   0.5 if the first test terminates OK but the second one loops endlessly,
661dnl   0.0 if the first test already loops endlessly.
662