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