Home | History | Annotate | Line # | Download | only in m4
      1  1.1  christos ##### http://autoconf-archive.cryp.to/acx_pthread.html
      2  1.1  christos #
      3  1.1  christos # SYNOPSIS
      4  1.1  christos #
      5  1.1  christos #   ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
      6  1.1  christos #
      7  1.1  christos # DESCRIPTION
      8  1.1  christos #
      9  1.1  christos #   This macro figures out how to build C programs using POSIX threads.
     10  1.1  christos #   It sets the PTHREAD_LIBS output variable to the threads library and
     11  1.1  christos #   linker flags, and the PTHREAD_CFLAGS output variable to any special
     12  1.1  christos #   C compiler flags that are needed. (The user can also force certain
     13  1.1  christos #   compiler flags/libs to be tested by setting these environment
     14  1.1  christos #   variables.)
     15  1.1  christos #
     16  1.1  christos #   Also sets PTHREAD_CC to any special C compiler that is needed for
     17  1.1  christos #   multi-threaded programs (defaults to the value of CC otherwise).
     18  1.1  christos #   (This is necessary on AIX to use the special cc_r compiler alias.)
     19  1.1  christos #
     20  1.1  christos #   NOTE: You are assumed to not only compile your program with these
     21  1.1  christos #   flags, but also link it with them as well. e.g. you should link
     22  1.1  christos #   with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
     23  1.1  christos #   $LIBS
     24  1.1  christos #
     25  1.1  christos #   If you are only building threads programs, you may wish to use
     26  1.1  christos #   these variables in your default LIBS, CFLAGS, and CC:
     27  1.1  christos #
     28  1.1  christos #          LIBS="$PTHREAD_LIBS $LIBS"
     29  1.1  christos #          CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
     30  1.1  christos #          CC="$PTHREAD_CC"
     31  1.1  christos #
     32  1.1  christos #   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
     33  1.1  christos #   constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
     34  1.1  christos #   that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
     35  1.1  christos #
     36  1.1  christos #   ACTION-IF-FOUND is a list of shell commands to run if a threads
     37  1.1  christos #   library is found, and ACTION-IF-NOT-FOUND is a list of commands to
     38  1.1  christos #   run it if it is not found. If ACTION-IF-FOUND is not specified, the
     39  1.1  christos #   default action will define HAVE_PTHREAD.
     40  1.1  christos #
     41  1.1  christos #   Please let the authors know if this macro fails on any platform, or
     42  1.1  christos #   if you have any other suggestions or comments. This macro was based
     43  1.1  christos #   on work by SGJ on autoconf scripts for FFTW (http://www.fftw.org/)
     44  1.1  christos #   (with help from M. Frigo), as well as ac_pthread and hb_pthread
     45  1.1  christos #   macros posted by Alejandro Forero Cuervo to the autoconf macro
     46  1.1  christos #   repository. We are also grateful for the helpful feedback of
     47  1.1  christos #   numerous users.
     48  1.1  christos #
     49  1.1  christos # LAST MODIFICATION
     50  1.1  christos #
     51  1.1  christos #   2007-07-29
     52  1.1  christos #
     53  1.1  christos # COPYLEFT
     54  1.1  christos #
     55  1.1  christos #   Copyright (c) 2007 Steven G. Johnson <stevenj (at] alum.mit.edu>
     56  1.1  christos #
     57  1.1  christos #   This program is free software: you can redistribute it and/or
     58  1.1  christos #   modify it under the terms of the GNU General Public License as
     59  1.1  christos #   published by the Free Software Foundation, either version 3 of the
     60  1.1  christos #   License, or (at your option) any later version.
     61  1.1  christos #
     62  1.1  christos #   This program is distributed in the hope that it will be useful, but
     63  1.1  christos #   WITHOUT ANY WARRANTY; without even the implied warranty of
     64  1.1  christos #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     65  1.1  christos #   General Public License for more details.
     66  1.1  christos #
     67  1.1  christos #   You should have received a copy of the GNU General Public License
     68  1.1  christos #   along with this program. If not, see
     69  1.1  christos #   <http://www.gnu.org/licenses/>.
     70  1.1  christos #
     71  1.1  christos #   As a special exception, the respective Autoconf Macro's copyright
     72  1.1  christos #   owner gives unlimited permission to copy, distribute and modify the
     73  1.1  christos #   configure scripts that are the output of Autoconf when processing
     74  1.1  christos #   the Macro. You need not follow the terms of the GNU General Public
     75  1.1  christos #   License when using or distributing such scripts, even though
     76  1.1  christos #   portions of the text of the Macro appear in them. The GNU General
     77  1.1  christos #   Public License (GPL) does govern all other use of the material that
     78  1.1  christos #   constitutes the Autoconf Macro.
     79  1.1  christos #
     80  1.1  christos #   This special exception to the GPL applies to versions of the
     81  1.1  christos #   Autoconf Macro released by the Autoconf Macro Archive. When you
     82  1.1  christos #   make and distribute a modified version of the Autoconf Macro, you
     83  1.1  christos #   may extend this special exception to the GPL to apply to your
     84  1.1  christos #   modified version as well.
     85  1.1  christos 
     86  1.1  christos AC_DEFUN([ACX_PTHREAD], [
     87  1.1  christos AC_REQUIRE([AC_CANONICAL_HOST])
     88  1.1  christos AC_LANG_SAVE
     89  1.1  christos AC_LANG_C
     90  1.1  christos acx_pthread_ok=no
     91  1.1  christos 
     92  1.1  christos # We used to check for pthread.h first, but this fails if pthread.h
     93  1.1  christos # requires special compiler flags (e.g. on True64 or Sequent).
     94  1.1  christos # It gets checked for in the link test anyway.
     95  1.1  christos 
     96  1.1  christos # First of all, check if the user has set any of the PTHREAD_LIBS,
     97  1.1  christos # etcetera environment variables, and if threads linking works using
     98  1.1  christos # them:
     99  1.1  christos if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
    100  1.1  christos         save_CFLAGS="$CFLAGS"
    101  1.1  christos         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
    102  1.1  christos         save_LIBS="$LIBS"
    103  1.1  christos         LIBS="$PTHREAD_LIBS $LIBS"
    104  1.1  christos         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
    105  1.1  christos         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
    106  1.1  christos         AC_MSG_RESULT($acx_pthread_ok)
    107  1.1  christos         if test x"$acx_pthread_ok" = xno; then
    108  1.1  christos                 PTHREAD_LIBS=""
    109  1.1  christos                 PTHREAD_CFLAGS=""
    110  1.1  christos         fi
    111  1.1  christos         LIBS="$save_LIBS"
    112  1.1  christos         CFLAGS="$save_CFLAGS"
    113  1.1  christos fi
    114  1.1  christos 
    115  1.1  christos # We must check for the threads library under a number of different
    116  1.1  christos # names; the ordering is very important because some systems
    117  1.1  christos # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
    118  1.1  christos # libraries is broken (non-POSIX).
    119  1.1  christos 
    120  1.1  christos # Create a list of thread flags to try.  Items starting with a "-" are
    121  1.1  christos # C compiler flags, and other items are library names, except for "none"
    122  1.1  christos # which indicates that we try without any flags at all, and "pthread-config"
    123  1.1  christos # which is a program returning the flags for the Pth emulation library.
    124  1.1  christos 
    125  1.1  christos acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
    126  1.1  christos 
    127  1.1  christos # The ordering *is* (sometimes) important.  Some notes on the
    128  1.1  christos # individual items follow:
    129  1.1  christos 
    130  1.1  christos # pthreads: AIX (must check this before -lpthread)
    131  1.1  christos # none: in case threads are in libc; should be tried before -Kthread and
    132  1.1  christos #       other compiler flags to prevent continual compiler warnings
    133  1.1  christos # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
    134  1.1  christos # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
    135  1.1  christos # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
    136  1.1  christos # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
    137  1.1  christos # -pthreads: Solaris/gcc
    138  1.1  christos # -mthreads: Mingw32/gcc, Lynx/gcc
    139  1.1  christos # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
    140  1.1  christos #      doesn't hurt to check since this sometimes defines pthreads too;
    141  1.1  christos #      also defines -D_REENTRANT)
    142  1.1  christos #      ... -mt is also the pthreads flag for HP/aCC
    143  1.1  christos # pthread: Linux, etcetera
    144  1.1  christos # --thread-safe: KAI C++
    145  1.1  christos # pthread-config: use pthread-config program (for GNU Pth library)
    146  1.1  christos 
    147  1.1  christos case "${host_cpu}-${host_os}" in
    148  1.1  christos         *solaris*)
    149  1.1  christos 
    150  1.1  christos         # On Solaris (at least, for some versions), libc contains stubbed
    151  1.1  christos         # (non-functional) versions of the pthreads routines, so link-based
    152  1.1  christos         # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
    153  1.1  christos         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
    154  1.1  christos         # a function called by this macro, so we could check for that, but
    155  1.1  christos         # who knows whether they'll stub that too in a future libc.)  So,
    156  1.1  christos         # we'll just look for -pthreads and -lpthread first:
    157  1.1  christos 
    158  1.1  christos         acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
    159  1.1  christos         ;;
    160  1.1  christos esac
    161  1.1  christos 
    162  1.1  christos if test x"$acx_pthread_ok" = xno; then
    163  1.1  christos for flag in $acx_pthread_flags; do
    164  1.1  christos 
    165  1.1  christos         case $flag in
    166  1.1  christos                 none)
    167  1.1  christos                 AC_MSG_CHECKING([whether pthreads work without any flags])
    168  1.1  christos                 ;;
    169  1.1  christos 
    170  1.1  christos                 -*)
    171  1.1  christos                 AC_MSG_CHECKING([whether pthreads work with $flag])
    172  1.1  christos                 PTHREAD_CFLAGS="$flag"
    173  1.1  christos                 ;;
    174  1.1  christos 
    175  1.1  christos 		pthread-config)
    176  1.1  christos 		AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
    177  1.1  christos 		if test x"$acx_pthread_config" = xno; then continue; fi
    178  1.1  christos 		PTHREAD_CFLAGS="`pthread-config --cflags`"
    179  1.1  christos 		PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
    180  1.1  christos 		;;
    181  1.1  christos 
    182  1.1  christos                 *)
    183  1.1  christos                 AC_MSG_CHECKING([for the pthreads library -l$flag])
    184  1.1  christos                 PTHREAD_LIBS="-l$flag"
    185  1.1  christos                 ;;
    186  1.1  christos         esac
    187  1.1  christos 
    188  1.1  christos         save_LIBS="$LIBS"
    189  1.1  christos         save_CFLAGS="$CFLAGS"
    190  1.1  christos         LIBS="$PTHREAD_LIBS $LIBS"
    191  1.1  christos         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
    192  1.1  christos 
    193  1.1  christos         # Check for various functions.  We must include pthread.h,
    194  1.1  christos         # since some functions may be macros.  (On the Sequent, we
    195  1.1  christos         # need a special flag -Kthread to make this header compile.)
    196  1.1  christos         # We check for pthread_join because it is in -lpthread on IRIX
    197  1.1  christos         # while pthread_create is in libc.  We check for pthread_attr_init
    198  1.1  christos         # due to DEC craziness with -lpthreads.  We check for
    199  1.1  christos         # pthread_cleanup_push because it is one of the few pthread
    200  1.1  christos         # functions on Solaris that doesn't have a non-functional libc stub.
    201  1.1  christos         # We try pthread_create on general principles.
    202  1.1  christos         AC_TRY_LINK([#include <pthread.h>],
    203  1.1  christos                     [pthread_t th; pthread_join(th, 0);
    204  1.1  christos                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
    205  1.1  christos                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
    206  1.1  christos                     [acx_pthread_ok=yes])
    207  1.1  christos 
    208  1.1  christos         LIBS="$save_LIBS"
    209  1.1  christos         CFLAGS="$save_CFLAGS"
    210  1.1  christos 
    211  1.1  christos         AC_MSG_RESULT($acx_pthread_ok)
    212  1.1  christos         if test "x$acx_pthread_ok" = xyes; then
    213  1.1  christos                 break;
    214  1.1  christos         fi
    215  1.1  christos 
    216  1.1  christos         PTHREAD_LIBS=""
    217  1.1  christos         PTHREAD_CFLAGS=""
    218  1.1  christos done
    219  1.1  christos fi
    220  1.1  christos 
    221  1.1  christos # Various other checks:
    222  1.1  christos if test "x$acx_pthread_ok" = xyes; then
    223  1.1  christos         save_LIBS="$LIBS"
    224  1.1  christos         LIBS="$PTHREAD_LIBS $LIBS"
    225  1.1  christos         save_CFLAGS="$CFLAGS"
    226  1.1  christos         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
    227  1.1  christos 
    228  1.1  christos         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
    229  1.1  christos 	AC_MSG_CHECKING([for joinable pthread attribute])
    230  1.1  christos 	attr_name=unknown
    231  1.1  christos 	for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
    232  1.1  christos 	    AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
    233  1.1  christos                         [attr_name=$attr; break])
    234  1.1  christos 	done
    235  1.1  christos         AC_MSG_RESULT($attr_name)
    236  1.1  christos         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
    237  1.1  christos             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
    238  1.1  christos                                [Define to necessary symbol if this constant
    239  1.1  christos                                 uses a non-standard name on your system.])
    240  1.1  christos         fi
    241  1.1  christos 
    242  1.1  christos         AC_MSG_CHECKING([if more special flags are required for pthreads])
    243  1.1  christos         flag=no
    244  1.1  christos         case "${host_cpu}-${host_os}" in
    245  1.1  christos             *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
    246  1.1  christos             *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
    247  1.1  christos         esac
    248  1.1  christos         AC_MSG_RESULT(${flag})
    249  1.1  christos         if test "x$flag" != xno; then
    250  1.1  christos             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
    251  1.1  christos         fi
    252  1.1  christos 
    253  1.1  christos         LIBS="$save_LIBS"
    254  1.1  christos         CFLAGS="$save_CFLAGS"
    255  1.1  christos 
    256  1.1  christos         # More AIX lossage: must compile with xlc_r or cc_r
    257  1.1  christos 	if test x"$GCC" != xyes; then
    258  1.1  christos           AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
    259  1.1  christos         else
    260  1.1  christos           PTHREAD_CC=$CC
    261  1.1  christos 	fi
    262  1.1  christos else
    263  1.1  christos         PTHREAD_CC="$CC"
    264  1.1  christos fi
    265  1.1  christos 
    266  1.1  christos AC_SUBST(PTHREAD_LIBS)
    267  1.1  christos AC_SUBST(PTHREAD_CFLAGS)
    268  1.1  christos AC_SUBST(PTHREAD_CC)
    269  1.1  christos 
    270  1.1  christos # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
    271  1.1  christos if test x"$acx_pthread_ok" = xyes; then
    272  1.1  christos         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
    273  1.1  christos         :
    274  1.1  christos else
    275  1.1  christos         acx_pthread_ok=no
    276  1.1  christos         $2
    277  1.1  christos fi
    278  1.1  christos AC_LANG_RESTORE
    279  1.1  christos ])dnl ACX_PTHREAD
    280