# threads_h.m4 serial 11 dnl Copyright (C) 2019-2023 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 facility is available. dnl Sets the variable LIBSTDTHREAD to the linker options for use in a Makefile dnl for a program that uses the 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 #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 ]], [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: */ /* ARM C: */ /* IBM C: supported only with compiler option -qtls, see */ /* Oracle Solaris Studio C: */ /* MSVC: */ #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_CALL_ONCE]) 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]) ])