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