Chris@19: dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) Chris@19: dnl @summary figure out how to build C programs using POSIX threads Chris@19: dnl @category InstalledPackages Chris@19: dnl Chris@19: dnl This macro figures out how to build C programs using POSIX Chris@19: dnl threads. It sets the PTHREAD_LIBS output variable to the threads Chris@19: dnl library and linker flags, and the PTHREAD_CFLAGS output variable Chris@19: dnl to any special C compiler flags that are needed. (The user can also Chris@19: dnl force certain compiler flags/libs to be tested by setting these Chris@19: dnl environment variables.) Chris@19: dnl Chris@19: dnl Also sets PTHREAD_CC to any special C compiler that is needed for Chris@19: dnl multi-threaded programs (defaults to the value of CC otherwise). Chris@19: dnl (This is necessary on AIX to use the special cc_r compiler alias.) Chris@19: dnl Chris@19: dnl NOTE: You are assumed to not only compile your program with these Chris@19: dnl flags, but also link it with them as well. e.g. you should link Chris@19: dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS Chris@19: dnl Chris@19: dnl If you are only building threads programs, you may wish to Chris@19: dnl use these variables in your default LIBS, CFLAGS, and CC: Chris@19: dnl Chris@19: dnl LIBS="$PTHREAD_LIBS $LIBS" Chris@19: dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS" Chris@19: dnl CC="$PTHREAD_CC" Chris@19: dnl Chris@19: dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute Chris@19: dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE Chris@19: dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). Chris@19: dnl Chris@19: dnl ACTION-IF-FOUND is a list of shell commands to run if a threads Chris@19: dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands Chris@19: dnl to run it if it is not found. If ACTION-IF-FOUND is not specified, Chris@19: dnl the default action will define HAVE_PTHREAD. Chris@19: dnl Chris@19: dnl Please let the authors know if this macro fails on any platform, Chris@19: dnl or if you have any other suggestions or comments. This macro was Chris@19: dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org) Chris@19: dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread Chris@19: dnl macros posted by Alejandro Forero Cuervo to the autoconf macro Chris@19: dnl repository. We are also grateful for the helpful feedback of Chris@19: dnl numerous users. Chris@19: dnl Chris@19: dnl @version 2006-09-15 Chris@19: dnl @license GPLWithACException Chris@19: dnl @author Steven G. Johnson Chris@19: Chris@19: AC_DEFUN([ACX_PTHREAD], [ Chris@19: AC_REQUIRE([AC_CANONICAL_HOST]) Chris@19: AC_LANG_SAVE Chris@19: AC_LANG_C Chris@19: acx_pthread_ok=no Chris@19: Chris@19: # We used to check for pthread.h first, but this fails if pthread.h Chris@19: # requires special compiler flags (e.g. on True64 or Sequent). Chris@19: # It gets checked for in the link test anyway. Chris@19: Chris@19: # First of all, check if the user has set any of the PTHREAD_LIBS, Chris@19: # etcetera environment variables, and if threads linking works using Chris@19: # them: Chris@19: if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then Chris@19: save_CFLAGS="$CFLAGS" Chris@19: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" Chris@19: save_LIBS="$LIBS" Chris@19: LIBS="$PTHREAD_LIBS $LIBS" Chris@19: AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) Chris@19: AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes) Chris@19: AC_MSG_RESULT($acx_pthread_ok) Chris@19: if test x"$acx_pthread_ok" = xno; then Chris@19: PTHREAD_LIBS="" Chris@19: PTHREAD_CFLAGS="" Chris@19: fi Chris@19: LIBS="$save_LIBS" Chris@19: CFLAGS="$save_CFLAGS" Chris@19: fi Chris@19: Chris@19: # We must check for the threads library under a number of different Chris@19: # names; the ordering is very important because some systems Chris@19: # (e.g. DEC) have both -lpthread and -lpthreads, where one of the Chris@19: # libraries is broken (non-POSIX). Chris@19: Chris@19: # Create a list of thread flags to try. Items starting with a "-" are Chris@19: # C compiler flags, and other items are library names, except for "none" Chris@19: # which indicates that we try without any flags at all, and "pthread-config" Chris@19: # which is a program returning the flags for the Pth emulation library. Chris@19: Chris@19: acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mt -mthreads pthread --thread-safe pthread-config" Chris@19: Chris@19: # The ordering *is* (sometimes) important. Some notes on the Chris@19: # individual items follow: Chris@19: Chris@19: # pthreads: AIX (must check this before -lpthread) Chris@19: # none: in case threads are in libc; should be tried before -Kthread and Chris@19: # other compiler flags to prevent continual compiler warnings Chris@19: # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) Chris@19: # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) Chris@19: # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) Chris@19: # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) Chris@19: # -pthreads: Solaris/gcc Chris@19: # -mthreads: Mingw32/gcc, Lynx/gcc Chris@19: # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it Chris@19: # doesn't hurt to check since this sometimes defines pthreads too; Chris@19: # also defines -D_REENTRANT) Chris@19: # ... -mt is also the pthreads flag for HP/aCC Chris@19: # (where it should come before -mthreads to avoid spurious warnings) Chris@19: # pthread: Linux, etcetera Chris@19: # --thread-safe: KAI C++ Chris@19: # pthread-config: use pthread-config program (for GNU Pth library) Chris@19: Chris@19: case "${host_cpu}-${host_os}" in Chris@19: *solaris*) Chris@19: Chris@19: # On Solaris (at least, for some versions), libc contains stubbed Chris@19: # (non-functional) versions of the pthreads routines, so link-based Chris@19: # tests will erroneously succeed. (We need to link with -pthreads/-mt/ Chris@19: # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather Chris@19: # a function called by this macro, so we could check for that, but Chris@19: # who knows whether they'll stub that too in a future libc.) So, Chris@19: # we'll just look for -pthreads and -lpthread first: Chris@19: Chris@19: acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags" Chris@19: ;; Chris@19: esac Chris@19: Chris@19: if test x"$acx_pthread_ok" = xno; then Chris@19: for flag in $acx_pthread_flags; do Chris@19: Chris@19: case $flag in Chris@19: none) Chris@19: AC_MSG_CHECKING([whether pthreads work without any flags]) Chris@19: ;; Chris@19: Chris@19: -*) Chris@19: AC_MSG_CHECKING([whether pthreads work with $flag]) Chris@19: PTHREAD_CFLAGS="$flag" Chris@19: ;; Chris@19: Chris@19: pthread-config) Chris@19: AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no) Chris@19: if test x"$acx_pthread_config" = xno; then continue; fi Chris@19: PTHREAD_CFLAGS="`pthread-config --cflags`" Chris@19: PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" Chris@19: ;; Chris@19: Chris@19: *) Chris@19: AC_MSG_CHECKING([for the pthreads library -l$flag]) Chris@19: PTHREAD_LIBS="-l$flag" Chris@19: ;; Chris@19: esac Chris@19: Chris@19: save_LIBS="$LIBS" Chris@19: save_CFLAGS="$CFLAGS" Chris@19: LIBS="$PTHREAD_LIBS $LIBS" Chris@19: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" Chris@19: Chris@19: # Check for various functions. We must include pthread.h, Chris@19: # since some functions may be macros. (On the Sequent, we Chris@19: # need a special flag -Kthread to make this header compile.) Chris@19: # We check for pthread_join because it is in -lpthread on IRIX Chris@19: # while pthread_create is in libc. We check for pthread_attr_init Chris@19: # due to DEC craziness with -lpthreads. We check for Chris@19: # pthread_cleanup_push because it is one of the few pthread Chris@19: # functions on Solaris that doesn't have a non-functional libc stub. Chris@19: # We try pthread_create on general principles. Chris@19: AC_TRY_LINK([#include ], Chris@19: [pthread_t th; pthread_join(th, (void**) 0); Chris@19: pthread_attr_init((pthread_attr_t*) 0); Chris@19: pthread_cleanup_push((void(*)(void *)) 0, (void*) 0); Chris@19: pthread_create((pthread_t*) 0, (pthread_attr_t*) 0, Chris@19: (void*(*)(void *)) 0, (void*) 0); Chris@19: pthread_cleanup_pop(0); ], Chris@19: [acx_pthread_ok=yes]) Chris@19: Chris@19: LIBS="$save_LIBS" Chris@19: CFLAGS="$save_CFLAGS" Chris@19: Chris@19: AC_MSG_RESULT($acx_pthread_ok) Chris@19: if test "x$acx_pthread_ok" = xyes; then Chris@19: break; Chris@19: fi Chris@19: Chris@19: PTHREAD_LIBS="" Chris@19: PTHREAD_CFLAGS="" Chris@19: done Chris@19: fi Chris@19: Chris@19: # Various other checks: Chris@19: if test "x$acx_pthread_ok" = xyes; then Chris@19: save_LIBS="$LIBS" Chris@19: LIBS="$PTHREAD_LIBS $LIBS" Chris@19: save_CFLAGS="$CFLAGS" Chris@19: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" Chris@19: Chris@19: # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. Chris@19: AC_MSG_CHECKING([for joinable pthread attribute]) Chris@19: attr_name=unknown Chris@19: for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do Chris@19: AC_TRY_LINK([#include ], [int attr=$attr; return attr;], Chris@19: [attr_name=$attr; break]) Chris@19: done Chris@19: AC_MSG_RESULT($attr_name) Chris@19: if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then Chris@19: AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name, Chris@19: [Define to necessary symbol if this constant Chris@19: uses a non-standard name on your system.]) Chris@19: fi Chris@19: Chris@19: AC_MSG_CHECKING([if more special flags are required for pthreads]) Chris@19: flag=no Chris@19: case "${host_cpu}-${host_os}" in Chris@19: *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";; Chris@19: *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; Chris@19: esac Chris@19: AC_MSG_RESULT(${flag}) Chris@19: if test "x$flag" != xno; then Chris@19: PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" Chris@19: fi Chris@19: Chris@19: LIBS="$save_LIBS" Chris@19: CFLAGS="$save_CFLAGS" Chris@19: Chris@19: # More AIX lossage: must compile with xlc_r or cc_r Chris@19: if test x"$GCC" != xyes; then Chris@19: AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC}) Chris@19: else Chris@19: PTHREAD_CC=$CC Chris@19: fi Chris@19: else Chris@19: PTHREAD_CC="$CC" Chris@19: fi Chris@19: Chris@19: AC_SUBST(PTHREAD_LIBS) Chris@19: AC_SUBST(PTHREAD_CFLAGS) Chris@19: AC_SUBST(PTHREAD_CC) Chris@19: Chris@19: # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: Chris@19: if test x"$acx_pthread_ok" = xyes; then Chris@19: ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) Chris@19: : Chris@19: else Chris@19: acx_pthread_ok=no Chris@19: $2 Chris@19: fi Chris@19: AC_LANG_RESTORE Chris@19: ])dnl ACX_PTHREAD