summaryrefslogtreecommitdiff
path: root/m4/threads_h.m4
blob: fc707e43facabfeac0e0795815c6789633212f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# threads_h.m4 serial 10
dnl Copyright (C) 2019-2022 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl Tests whether the <threads.h> facility is available.
dnl Sets the variable LIBSTDTHREAD to the linker options for use in a Makefile
dnl for a program that uses the <threads.h> functions.
dnl Sets the variable LIBTHREADLOCAL to the linker options for use in a Makefile
dnl for a program that uses the 'thread_local' macro.
AC_DEFUN_ONCE([gl_THREADS_H],
[
  AC_REQUIRE([gl_THREADS_H_DEFAULTS])
  AC_REQUIRE([AC_CANONICAL_HOST])
  AC_REQUIRE([gl_STDTHREADLIB])

  gl_CHECK_NEXT_HEADERS([threads.h])
  if test $ac_cv_header_threads_h = yes; then
    HAVE_THREADS_H=1
  else
    HAVE_THREADS_H=0
  fi
  AC_SUBST([HAVE_THREADS_H])

  if test $HAVE_THREADS_H = 1; then
    dnl AIX 7.1..7.2 defines thrd_start_t incorrectly, namely as
    dnl 'void * (*) (void *)' instead of 'int (*) (void *)'.
    AC_CACHE_CHECK([whether thrd_start_t is correct],
      [gl_cv_thrd_start_t_correct],
      [AC_COMPILE_IFELSE(
         [AC_LANG_PROGRAM([[
            #include <threads.h>
            #ifdef __cplusplus
            extern "C" {
            #endif
            extern void foo (thrd_start_t);
            extern void foo (int (*) (void *));
            #ifdef __cplusplus
            }
            #endif
          ]], [[]])],
         [gl_cv_thrd_start_t_correct=yes],
         [gl_cv_thrd_start_t_correct=no])
      ])
    if test $gl_cv_thrd_start_t_correct != yes; then
      BROKEN_THRD_START_T=1
    fi
  fi

  dnl Define _Thread_local.
  dnl GCC, for example, supports '__thread' since version 3.3, but it supports
  dnl '_Thread_local' only starting with version 4.9.
  AH_VERBATIM([thread_local], gl_THREAD_LOCAL_DEFINITION)

  dnl Test whether _Thread_local is supported in the compiler and linker.
  AC_CACHE_CHECK([whether _Thread_local works],
    [gl_cv_thread_local_works],
    [dnl On AIX 7.1 with GCC 4.8.1, this test program compiles fine, but the
     dnl 'test-thread-local' test misbehaves.
     dnl On Android 4.3, this test program compiles fine, but the
     dnl 'test-thread-local' test crashes.
     if case "$host_os" in
          aix*) test -n "$GCC" ;;
          linux*-android*) true ;;
          *) false ;;
        esac
     then
       gl_cv_thread_local_works="guessing no"
     else
       AC_COMPILE_IFELSE(
         [AC_LANG_PROGRAM([gl_THREAD_LOCAL_DEFINITION[
            int _Thread_local x;
          ]], [[
            x = 42;
          ]])],
         [if test "$cross_compiling" != yes; then
            gl_cv_thread_local_works=yes
          else
            gl_cv_thread_local_works="guessing yes"
          fi
         ],
         [if test "$cross_compiling" != yes; then
            gl_cv_thread_local_works=no
          else
            gl_cv_thread_local_works="guessing no"
          fi
         ])
     fi
    ])
  case "$gl_cv_thread_local_works" in
    *yes) ;;
    *) HAVE_THREAD_LOCAL=0 ;;
  esac

  dnl Determine the link dependencies of '_Thread_local'.
  LIBTHREADLOCAL=
  dnl On AIX 7.2 with "xlc -qthreaded -qtls", programs that use _Thread_local
  dnl as defined above produce link errors regarding the symbols
  dnl '.__tls_get_mod' and '__tls_get_addr'. Similarly, on AIX 7.2 with gcc,
  dnl 32-bit programs that use _Thread_local produce link errors regarding the
  dnl symbol '__get_tpointer'. The fix is to link with -lpthread.
  case "$host_os" in
    aix*)
      LIBTHREADLOCAL=-lpthread
      ;;
  esac
  AC_SUBST([LIBTHREADLOCAL])

  dnl Check for declarations of anything we want to poison if the
  dnl corresponding gnulib module is not in use, and which is not
  dnl guaranteed by C89.
  gl_WARN_ON_USE_PREPARE([[#include <threads.h>
    ]], [call_once
    cnd_broadcast cnd_destroy cnd_init cnd_signal cnd_timedwait cnd_wait
    mtx_destroy mtx_init mtx_lock mtx_timedlock mtx_trylock mtx_unlock
    thrd_create thrd_current thrd_detach thrd_equal thrd_exit thrd_join
    thrd_sleep thrd_yield
    tss_create tss_delete tss_get tss_set])
])

dnl Expands to C preprocessor statements that define _Thread_local.
AC_DEFUN([gl_THREAD_LOCAL_DEFINITION],
[[/* The _Thread_local keyword of C11.  */
/* GNU C: <https://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html> */
/* ARM C: <https://developer.arm.com/docs/dui0472/latest/compiler-specific-features/__declspecthread> */
/* IBM C: supported only with compiler option -qtls, see
   <https://www.ibm.com/support/knowledgecenter/SSGH2K_12.1.0/com.ibm.xlc121.aix.doc/compiler_ref/opt_tls.html> */
/* Oracle Solaris Studio C: <https://docs.oracle.com/cd/E18659_01/html/821-1384/bjabr.html> */
/* MSVC: <https://docs.microsoft.com/en-us/cpp/parallel/thread-local-storage-tls> */
#ifndef _Thread_local
# if defined __GNUC__ || defined __CC_ARM || defined __xlC__ || defined __SUNPRO_C
#  define _Thread_local __thread
# elif defined _MSC_VER
#  define _Thread_local __declspec (thread)
# endif
#endif
]])

# gl_THREADS_MODULE_INDICATOR([modulename])
# sets the shell variable that indicates the presence of the given module
# to a C preprocessor expression that will evaluate to 1.
# This macro invocation must not occur in macros that are AC_REQUIREd.
AC_DEFUN([gl_THREADS_MODULE_INDICATOR],
[
  dnl Ensure to expand the default settings once only.
  gl_THREADS_H_REQUIRE_DEFAULTS
  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
  dnl Define it also as a C macro, for the benefit of the unit tests.
  gl_MODULE_INDICATOR_FOR_TESTS([$1])
])

# Initializes the default values for AC_SUBSTed shell variables.
# This macro must not be AC_REQUIREd.  It must only be invoked, and only
# outside of macros or in macros that are not AC_REQUIREd.
AC_DEFUN([gl_THREADS_H_REQUIRE_DEFAULTS],
[
  m4_defun(GL_MODULE_INDICATOR_PREFIX[_THREADS_H_MODULE_INDICATOR_DEFAULTS], [
    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_CND])
    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MTX])
    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_THRD])
    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TSS])
  ])
  m4_require(GL_MODULE_INDICATOR_PREFIX[_THREADS_H_MODULE_INDICATOR_DEFAULTS])
  AC_REQUIRE([gl_THREADS_H_DEFAULTS])
])

AC_DEFUN([gl_THREADS_H_DEFAULTS],
[
  dnl Assume proper GNU behavior unless another module says otherwise.
  HAVE_THREAD_LOCAL=1;    AC_SUBST([HAVE_THREAD_LOCAL])
  BROKEN_THRD_START_T=0;  AC_SUBST([BROKEN_THRD_START_T])
  REPLACE_THRD_CREATE=0;  AC_SUBST([REPLACE_THRD_CREATE])
  REPLACE_THRD_CURRENT=0; AC_SUBST([REPLACE_THRD_CURRENT])
  REPLACE_THRD_DETACH=0;  AC_SUBST([REPLACE_THRD_DETACH])
  REPLACE_THRD_EQUAL=0;   AC_SUBST([REPLACE_THRD_EQUAL])
  REPLACE_THRD_JOIN=0;    AC_SUBST([REPLACE_THRD_JOIN])
])