"Fossies" - the Fresh Open Source Software Archive

Member "nano-4.5/m4/threadlib.m4" (18 Aug 2019, 14205 Bytes) of package /linux/misc/nano-4.5.tar.xz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "threadlib.m4": 4.3_vs_4.4.

    1 # threadlib.m4 serial 20
    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.
    6 
    7 dnl From Bruno Haible.
    8 
    9 AC_PREREQ([2.60])
   10 
   11 dnl gl_THREADLIB
   12 dnl ------------
   13 dnl Tests for a multithreading library to be used.
   14 dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
   15 dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
   16 dnl default is 'no', otherwise it is system dependent. In both cases, the user
   17 dnl can change the choice through the options --enable-threads=choice or
   18 dnl --disable-threads.
   19 dnl Defines at most one of the macros USE_POSIX_THREADS, USE_WINDOWS_THREADS.
   20 dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
   21 dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
   22 dnl libtool).
   23 dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
   24 dnl programs that really need multithread functionality. The difference
   25 dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
   26 dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
   27 dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
   28 dnl multithread-safe programs.
   29 dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the
   30 dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the
   31 dnl same value. Only system libraries are needed.
   32 
   33 AC_DEFUN([gl_THREADLIB_EARLY],
   34 [
   35   AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
   36 ])
   37 
   38 dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
   39 
   40 AC_DEFUN([gl_THREADLIB_EARLY_BODY],
   41 [
   42   dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
   43   dnl influences the result of the autoconf tests that test for *_unlocked
   44   dnl declarations, on AIX 5 at least. Therefore it must come early.
   45   AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
   46   AC_BEFORE([$0], [gl_ARGP])dnl
   47 
   48   AC_REQUIRE([AC_CANONICAL_HOST])
   49   dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
   50   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
   51   dnl Check for multithreading.
   52   m4_ifdef([gl_THREADLIB_DEFAULT_NO],
   53     [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
   54     [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
   55   m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=])
   56   AC_ARG_ENABLE([threads],
   57 AC_HELP_STRING([--enable-threads={posix|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
   58 AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
   59     [gl_use_threads=$enableval],
   60     [if test -n "$gl_use_threads_default"; then
   61        gl_use_threads="$gl_use_threads_default"
   62      else
   63 changequote(,)dnl
   64        case "$host_os" in
   65          dnl Disable multithreading by default on OSF/1, because it interferes
   66          dnl with fork()/exec(): When msgexec is linked with -lpthread, its
   67          dnl child process gets an endless segmentation fault inside execvp().
   68          osf*) gl_use_threads=no ;;
   69          dnl Disable multithreading by default on Cygwin 1.5.x, because it has
   70          dnl bugs that lead to endless loops or crashes. See
   71          dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
   72          cygwin*)
   73                case `uname -r` in
   74                  1.[0-5].*) gl_use_threads=no ;;
   75                  *)         gl_use_threads=yes ;;
   76                esac
   77                ;;
   78          dnl Obey gl_AVOID_WINPTHREAD on mingw.
   79          mingw*)
   80                case "$gl_use_winpthreads_default" in
   81                  yes) gl_use_threads=posix ;;
   82                  no)  gl_use_threads=windows ;;
   83                  *)   gl_use_threads=yes ;;
   84                esac
   85                ;;
   86          *)    gl_use_threads=yes ;;
   87        esac
   88 changequote([,])dnl
   89      fi
   90     ])
   91   if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
   92     # For using <pthread.h>:
   93     case "$host_os" in
   94       osf*)
   95         # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
   96         # groks <pthread.h>. cc also understands the flag -pthread, but
   97         # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
   98         # 2. putting a flag into CPPFLAGS that has an effect on the linker
   99         # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
  100         # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
  101         CPPFLAGS="$CPPFLAGS -D_REENTRANT"
  102         ;;
  103     esac
  104     # Some systems optimize for single-threaded programs by default, and
  105     # need special flags to disable these optimizations. For example, the
  106     # definition of 'errno' in <errno.h>.
  107     case "$host_os" in
  108       aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
  109       solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
  110     esac
  111   fi
  112 ])
  113 
  114 dnl The guts of gl_THREADLIB. Needs to be expanded only once.
  115 
  116 AC_DEFUN([gl_THREADLIB_BODY],
  117 [
  118   AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
  119   gl_threads_api=none
  120   LIBTHREAD=
  121   LTLIBTHREAD=
  122   LIBMULTITHREAD=
  123   LTLIBMULTITHREAD=
  124   if test "$gl_use_threads" != no; then
  125     dnl Check whether the compiler and linker support weak declarations.
  126     AC_CACHE_CHECK([whether imported symbols can be declared weak],
  127       [gl_cv_have_weak],
  128       [gl_cv_have_weak=no
  129        dnl First, test whether the compiler accepts it syntactically.
  130        AC_LINK_IFELSE(
  131          [AC_LANG_PROGRAM(
  132             [[extern void xyzzy ();
  133 #pragma weak xyzzy]],
  134             [[xyzzy();]])],
  135          [gl_cv_have_weak=maybe])
  136        if test $gl_cv_have_weak = maybe; then
  137          dnl Second, test whether it actually works. On Cygwin 1.7.2, with
  138          dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
  139          AC_RUN_IFELSE(
  140            [AC_LANG_SOURCE([[
  141 #include <stdio.h>
  142 #pragma weak fputs
  143 int main ()
  144 {
  145   return (fputs == NULL);
  146 }]])],
  147            [gl_cv_have_weak=yes],
  148            [gl_cv_have_weak=no],
  149            [dnl When cross-compiling, assume that only ELF platforms support
  150             dnl weak symbols.
  151             AC_EGREP_CPP([Extensible Linking Format],
  152               [#ifdef __ELF__
  153                Extensible Linking Format
  154                #endif
  155               ],
  156               [gl_cv_have_weak="guessing yes"],
  157               [gl_cv_have_weak="guessing no"])
  158            ])
  159        fi
  160        dnl But when linking statically, weak symbols don't work.
  161        case " $LDFLAGS " in
  162          *" -static "*) gl_cv_have_weak=no ;;
  163        esac
  164       ])
  165     if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  166       dnl If we use weak symbols to implement pthread_in_use / pth_in_use /
  167       dnl thread_in_use, we also need to test whether the ISO C 11 thrd_create
  168       dnl facility is in use.
  169       AC_CHECK_HEADERS_ONCE([threads.h])
  170       :
  171     fi
  172     if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
  173       # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
  174       # it groks <pthread.h>. It's added above, in gl_THREADLIB_EARLY_BODY.
  175       AC_CHECK_HEADER([pthread.h],
  176         [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
  177       if test "$gl_have_pthread_h" = yes; then
  178         # Other possible tests:
  179         #   -lpthreads (FSU threads, PCthreads)
  180         #   -lgthreads
  181         gl_have_pthread=
  182         # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
  183         # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
  184         # the second one only in libpthread, and lock.c needs it.
  185         #
  186         # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
  187         # needs -pthread for some reason.  See:
  188         # https://lists.gnu.org/r/bug-gnulib/2014-09/msg00023.html
  189         save_LIBS=$LIBS
  190         for gl_pthread in '' '-pthread'; do
  191           LIBS="$LIBS $gl_pthread"
  192           AC_LINK_IFELSE(
  193             [AC_LANG_PROGRAM(
  194                [[#include <pthread.h>
  195                  pthread_mutex_t m;
  196                  pthread_mutexattr_t ma;
  197                ]],
  198                [[pthread_mutex_lock (&m);
  199                  pthread_mutexattr_init (&ma);]])],
  200             [gl_have_pthread=yes
  201              LIBTHREAD=$gl_pthread LTLIBTHREAD=$gl_pthread
  202              LIBMULTITHREAD=$gl_pthread LTLIBMULTITHREAD=$gl_pthread])
  203           LIBS=$save_LIBS
  204           test -n "$gl_have_pthread" && break
  205         done
  206 
  207         # Test for libpthread by looking for pthread_kill. (Not pthread_self,
  208         # since it is defined as a macro on OSF/1.)
  209         if test -n "$gl_have_pthread" && test -z "$LIBTHREAD"; then
  210           # The program links fine without libpthread. But it may actually
  211           # need to link with libpthread in order to create multiple threads.
  212           AC_CHECK_LIB([pthread], [pthread_kill],
  213             [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread
  214              # On Solaris and HP-UX, most pthread functions exist also in libc.
  215              # Therefore pthread_in_use() needs to actually try to create a
  216              # thread: pthread_create from libc will fail, whereas
  217              # pthread_create will actually create a thread.
  218              # On Solaris 10 or newer, this test is no longer needed, because
  219              # libc contains the fully functional pthread functions.
  220              case "$host_os" in
  221                solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
  222                  AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
  223                    [Define if the pthread_in_use() detection is hard.])
  224              esac
  225             ])
  226         elif test -z "$gl_have_pthread"; then
  227           # Some library is needed. Try libpthread and libc_r.
  228           AC_CHECK_LIB([pthread], [pthread_kill],
  229             [gl_have_pthread=yes
  230              LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
  231              LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
  232           if test -z "$gl_have_pthread"; then
  233             # For FreeBSD 4.
  234             AC_CHECK_LIB([c_r], [pthread_kill],
  235               [gl_have_pthread=yes
  236                LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
  237                LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
  238           fi
  239         fi
  240         if test -n "$gl_have_pthread"; then
  241           gl_threads_api=posix
  242           AC_DEFINE([USE_POSIX_THREADS], [1],
  243             [Define if the POSIX multithreading library can be used.])
  244           if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  245             if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  246               AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
  247                 [Define if references to the POSIX multithreading library should be made weak.])
  248               LIBTHREAD=
  249               LTLIBTHREAD=
  250             fi
  251           fi
  252         fi
  253       fi
  254     fi
  255     if test -z "$gl_have_pthread"; then
  256       case "$gl_use_threads" in
  257         yes | windows | win32) # The 'win32' is for backward compatibility.
  258           if { case "$host_os" in
  259                  mingw*) true;;
  260                  *) false;;
  261                esac
  262              }; then
  263             gl_threads_api=windows
  264             AC_DEFINE([USE_WINDOWS_THREADS], [1],
  265               [Define if the native Windows multithreading API can be used.])
  266           fi
  267           ;;
  268       esac
  269     fi
  270   fi
  271   AC_MSG_CHECKING([for multithread API to use])
  272   AC_MSG_RESULT([$gl_threads_api])
  273   AC_SUBST([LIBTHREAD])
  274   AC_SUBST([LTLIBTHREAD])
  275   AC_SUBST([LIBMULTITHREAD])
  276   AC_SUBST([LTLIBMULTITHREAD])
  277 ])
  278 
  279 AC_DEFUN([gl_THREADLIB],
  280 [
  281   AC_REQUIRE([gl_THREADLIB_EARLY])
  282   AC_REQUIRE([gl_THREADLIB_BODY])
  283 ])
  284 
  285 
  286 dnl gl_DISABLE_THREADS
  287 dnl ------------------
  288 dnl Sets the gl_THREADLIB default so that threads are not used by default.
  289 dnl The user can still override it at installation time, by using the
  290 dnl configure option '--enable-threads'.
  291 
  292 AC_DEFUN([gl_DISABLE_THREADS], [
  293   m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
  294 ])
  295 
  296 
  297 dnl gl_AVOID_WINPTHREAD
  298 dnl -------------------
  299 dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the
  300 dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided.
  301 dnl The user can still override it at installation time, by using the
  302 dnl configure option '--enable-threads'.
  303 
  304 AC_DEFUN([gl_AVOID_WINPTHREAD], [
  305   m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no])
  306 ])
  307 
  308 
  309 dnl Survey of platforms:
  310 dnl
  311 dnl Platform           Available  Compiler    Supports   test-lock
  312 dnl                    flavours   option      weak       result
  313 dnl ---------------    ---------  ---------   --------   ---------
  314 dnl Linux 2.4/glibc    posix      -lpthread       Y      OK
  315 dnl
  316 dnl GNU Hurd/glibc     posix
  317 dnl
  318 dnl Ubuntu 14.04       posix      -pthread        Y      OK
  319 dnl
  320 dnl FreeBSD 5.3        posix      -lc_r           Y
  321 dnl                    posix      -lkse ?         Y
  322 dnl                    posix      -lpthread ?     Y
  323 dnl                    posix      -lthr           Y
  324 dnl
  325 dnl FreeBSD 5.2        posix      -lc_r           Y
  326 dnl                    posix      -lkse           Y
  327 dnl                    posix      -lthr           Y
  328 dnl
  329 dnl FreeBSD 4.0,4.10   posix      -lc_r           Y      OK
  330 dnl
  331 dnl NetBSD 1.6         --
  332 dnl
  333 dnl OpenBSD 3.4        posix      -lpthread       Y      OK
  334 dnl
  335 dnl Mac OS X 10.[123]  posix      -lpthread       Y      OK
  336 dnl
  337 dnl Solaris 7,8,9      posix      -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
  338 dnl
  339 dnl HP-UX 11           posix      -lpthread       N (cc) OK
  340 dnl                                               Y (gcc)
  341 dnl
  342 dnl IRIX 6.5           posix      -lpthread       Y      0.5
  343 dnl
  344 dnl AIX 4.3,5.1        posix      -lpthread       N      AIX 4: 0.5; AIX 5: OK
  345 dnl
  346 dnl OSF/1 4.0,5.1      posix      -pthread (cc)   N      OK
  347 dnl                               -lpthread (gcc) Y
  348 dnl
  349 dnl Cygwin             posix      -lpthread       Y      OK
  350 dnl
  351 dnl Mingw              windows                    N      OK
  352 dnl
  353 dnl BeOS 5             --
  354 dnl
  355 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
  356 dnl turned off:
  357 dnl   OK if all three tests terminate OK,
  358 dnl   0.5 if the first test terminates OK but the second one loops endlessly,
  359 dnl   0.0 if the first test already loops endlessly.