immutable: Add tests.
[gnulib.git] / m4 / threads.m4
blobf8e16e6be05904b69e21c93c8bb46679e683cf59
1 # threads.m4 serial 7
2 dnl Copyright (C) 2019-2021 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.
7 dnl Tests whether the <threads.h> facility is available.
8 dnl Sets the variable LIBSTDTHREAD to the linker options for use in a Makefile
9 dnl for a program that uses the <threads.h> functions.
10 dnl Sets the variable LIBTHREADLOCAL to the linker options for use in a Makefile
11 dnl for a program that uses the 'thread_local' macro.
12 AC_DEFUN([gl_THREADS_H],
14   AC_REQUIRE([gl_THREADS_H_DEFAULTS])
15   AC_REQUIRE([AC_CANONICAL_HOST])
16   AC_REQUIRE([gl_STDTHREADLIB])
18   gl_CHECK_NEXT_HEADERS([threads.h])
19   if test $ac_cv_header_threads_h = yes; then
20     HAVE_THREADS_H=1
21   else
22     HAVE_THREADS_H=0
23   fi
24   AC_SUBST([HAVE_THREADS_H])
26   if test $HAVE_THREADS_H = 1; then
27     dnl AIX 7.1..7.2 defines thrd_start_t incorrectly, namely as
28     dnl 'void * (*) (void *)' instead of 'int (*) (void *)'.
29     AC_CACHE_CHECK([whether thrd_start_t is correct],
30       [gl_cv_thrd_start_t_correct],
31       [AC_COMPILE_IFELSE(
32          [AC_LANG_PROGRAM([[
33             #include <threads.h>
34             #ifdef __cplusplus
35             extern "C" {
36             #endif
37             extern void foo (thrd_start_t);
38             extern void foo (int (*) (void *));
39             #ifdef __cplusplus
40             }
41             #endif
42           ]], [[]])],
43          [gl_cv_thrd_start_t_correct=yes],
44          [gl_cv_thrd_start_t_correct=no])
45       ])
46     if test $gl_cv_thrd_start_t_correct != yes; then
47       BROKEN_THRD_START_T=1
48     fi
49   fi
51   dnl Define _Thread_local.
52   dnl GCC, for example, supports '__thread' since version 3.3, but it supports
53   dnl '_Thread_local' only starting with version 4.9.
54   AH_VERBATIM([thread_local], gl_THREAD_LOCAL_DEFINITION)
56   dnl Test whether _Thread_local is supported in the compiler and linker.
57   AC_CACHE_CHECK([whether _Thread_local works],
58     [gl_cv_thread_local_works],
59     [dnl On AIX 7.1 with GCC 4.8.1, this test program compiles fine, but the
60      dnl 'test-thread-local' test misbehaves.
61      dnl On Android 4.3, this test program compiles fine, but the
62      dnl 'test-thread-local' test crashes.
63      if case "$host_os" in
64           aix*) test -n "$GCC" ;;
65           linux*-android*) true ;;
66           *) false ;;
67         esac
68      then
69        gl_cv_thread_local_works="guessing no"
70      else
71        AC_COMPILE_IFELSE(
72          [AC_LANG_PROGRAM([gl_THREAD_LOCAL_DEFINITION[
73             int _Thread_local x;
74           ]], [[
75             x = 42;
76           ]])],
77          [if test "$cross_compiling" != yes; then
78             gl_cv_thread_local_works=yes
79           else
80             gl_cv_thread_local_works="guessing yes"
81           fi
82          ],
83          [if test "$cross_compiling" != yes; then
84             gl_cv_thread_local_works=no
85           else
86             gl_cv_thread_local_works="guessing no"
87           fi
88          ])
89      fi
90     ])
91   case "$gl_cv_thread_local_works" in
92     *yes) ;;
93     *) HAVE_THREAD_LOCAL=0 ;;
94   esac
96   dnl Determine the link dependencies of '_Thread_local'.
97   LIBTHREADLOCAL=
98   dnl On AIX 7.2 with "xlc -qthreaded -qtls", programs that use _Thread_local
99   dnl as defined above produce link errors regarding the symbols
100   dnl '.__tls_get_mod' and '__tls_get_addr'. Similarly, on AIX 7.2 with gcc,
101   dnl 32-bit programs that use _Thread_local produce link errors regarding the
102   dnl symbol '__get_tpointer'. The fix is to link with -lpthread.
103   case "$host_os" in
104     aix*)
105       LIBTHREADLOCAL=-lpthread
106       ;;
107   esac
108   AC_SUBST([LIBTHREADLOCAL])
110   dnl Check for declarations of anything we want to poison if the
111   dnl corresponding gnulib module is not in use, and which is not
112   dnl guaranteed by C89.
113   gl_WARN_ON_USE_PREPARE([[#include <threads.h>
114     ]], [call_once
115     cnd_broadcast cnd_destroy cnd_init cnd_signal cnd_timedwait cnd_wait
116     mtx_destroy mtx_init mtx_lock mtx_timedlock mtx_trylock mtx_unlock
117     thrd_create thrd_current thrd_detach thrd_equal thrd_exit thrd_join
118     thrd_sleep thrd_yield
119     tss_create tss_delete tss_get tss_set])
122 dnl Expands to C preprocessor statements that define _Thread_local.
123 AC_DEFUN([gl_THREAD_LOCAL_DEFINITION],
124 [[/* The _Thread_local keyword of C11.  */
125 /* GNU C: <https://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html> */
126 /* ARM C: <https://developer.arm.com/docs/dui0472/latest/compiler-specific-features/__declspecthread> */
127 /* IBM C: supported only with compiler option -qtls, see
128    <https://www.ibm.com/support/knowledgecenter/SSGH2K_12.1.0/com.ibm.xlc121.aix.doc/compiler_ref/opt_tls.html> */
129 /* Oracle Solaris Studio C: <https://docs.oracle.com/cd/E18659_01/html/821-1384/bjabr.html> */
130 /* MSVC: <https://docs.microsoft.com/en-us/cpp/parallel/thread-local-storage-tls> */
131 #ifndef _Thread_local
132 # if defined __GNUC__ || defined __CC_ARM || defined __xlC__ || defined __SUNPRO_C
133 #  define _Thread_local __thread
134 # elif defined _MSC_VER
135 #  define _Thread_local __declspec (thread)
136 # endif
137 #endif
140 AC_DEFUN([gl_THREADS_MODULE_INDICATOR],
142   dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
143   AC_REQUIRE([gl_THREADS_H_DEFAULTS])
144   gl_MODULE_INDICATOR_SET_VARIABLE([$1])
145   dnl Define it also as a C macro, for the benefit of the unit tests.
146   gl_MODULE_INDICATOR_FOR_TESTS([$1])
149 AC_DEFUN([gl_THREADS_H_DEFAULTS],
151   GNULIB_CND=0;           AC_SUBST([GNULIB_CND])
152   GNULIB_MTX=0;           AC_SUBST([GNULIB_MTX])
153   GNULIB_THRD=0;          AC_SUBST([GNULIB_THRD])
154   GNULIB_TSS=0;           AC_SUBST([GNULIB_TSS])
155   dnl Assume proper GNU behavior unless another module says otherwise.
156   HAVE_THREAD_LOCAL=1;    AC_SUBST([HAVE_THREAD_LOCAL])
157   BROKEN_THRD_START_T=0;  AC_SUBST([BROKEN_THRD_START_T])
158   REPLACE_THRD_CREATE=0;  AC_SUBST([REPLACE_THRD_CREATE])
159   REPLACE_THRD_CURRENT=0; AC_SUBST([REPLACE_THRD_CURRENT])
160   REPLACE_THRD_DETACH=0;  AC_SUBST([REPLACE_THRD_DETACH])
161   REPLACE_THRD_EQUAL=0;   AC_SUBST([REPLACE_THRD_EQUAL])
162   REPLACE_THRD_JOIN=0;    AC_SUBST([REPLACE_THRD_JOIN])