11b5d61b8Smrg# =========================================================================== 21b5d61b8Smrg# http://www.gnu.org/software/autoconf-archive/ax_pthread.html 31b5d61b8Smrg# =========================================================================== 41b5d61b8Smrg# 51b5d61b8Smrg# SYNOPSIS 61b5d61b8Smrg# 71b5d61b8Smrg# AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) 81b5d61b8Smrg# 91b5d61b8Smrg# DESCRIPTION 101b5d61b8Smrg# 111b5d61b8Smrg# This macro figures out how to build C programs using POSIX threads. It 121b5d61b8Smrg# sets the PTHREAD_LIBS output variable to the threads library and linker 131b5d61b8Smrg# flags, and the PTHREAD_CFLAGS output variable to any special C compiler 141b5d61b8Smrg# flags that are needed. (The user can also force certain compiler 151b5d61b8Smrg# flags/libs to be tested by setting these environment variables.) 161b5d61b8Smrg# 171b5d61b8Smrg# Also sets PTHREAD_CC to any special C compiler that is needed for 181b5d61b8Smrg# multi-threaded programs (defaults to the value of CC otherwise). (This 191b5d61b8Smrg# is necessary on AIX to use the special cc_r compiler alias.) 201b5d61b8Smrg# 211b5d61b8Smrg# NOTE: You are assumed to not only compile your program with these flags, 221b5d61b8Smrg# but also link it with them as well. e.g. you should link with 231b5d61b8Smrg# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS 241b5d61b8Smrg# 251b5d61b8Smrg# If you are only building threads programs, you may wish to use these 261b5d61b8Smrg# variables in your default LIBS, CFLAGS, and CC: 271b5d61b8Smrg# 281b5d61b8Smrg# LIBS="$PTHREAD_LIBS $LIBS" 291b5d61b8Smrg# CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 301b5d61b8Smrg# CC="$PTHREAD_CC" 311b5d61b8Smrg# 321b5d61b8Smrg# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant 331b5d61b8Smrg# has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name 341b5d61b8Smrg# (e.g. PTHREAD_CREATE_UNDETACHED on AIX). 351b5d61b8Smrg# 361b5d61b8Smrg# Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the 371b5d61b8Smrg# PTHREAD_PRIO_INHERIT symbol is defined when compiling with 381b5d61b8Smrg# PTHREAD_CFLAGS. 391b5d61b8Smrg# 401b5d61b8Smrg# ACTION-IF-FOUND is a list of shell commands to run if a threads library 411b5d61b8Smrg# is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it 421b5d61b8Smrg# is not found. If ACTION-IF-FOUND is not specified, the default action 431b5d61b8Smrg# will define HAVE_PTHREAD. 441b5d61b8Smrg# 451b5d61b8Smrg# Please let the authors know if this macro fails on any platform, or if 461b5d61b8Smrg# you have any other suggestions or comments. This macro was based on work 471b5d61b8Smrg# by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help 481b5d61b8Smrg# from M. Frigo), as well as ac_pthread and hb_pthread macros posted by 491b5d61b8Smrg# Alejandro Forero Cuervo to the autoconf macro repository. We are also 501b5d61b8Smrg# grateful for the helpful feedback of numerous users. 511b5d61b8Smrg# 521b5d61b8Smrg# Updated for Autoconf 2.68 by Daniel Richard G. 531b5d61b8Smrg# 541b5d61b8Smrg# LICENSE 551b5d61b8Smrg# 561b5d61b8Smrg# Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu> 571b5d61b8Smrg# Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG> 581b5d61b8Smrg# 591b5d61b8Smrg# This program is free software: you can redistribute it and/or modify it 601b5d61b8Smrg# under the terms of the GNU General Public License as published by the 611b5d61b8Smrg# Free Software Foundation, either version 3 of the License, or (at your 621b5d61b8Smrg# option) any later version. 631b5d61b8Smrg# 641b5d61b8Smrg# This program is distributed in the hope that it will be useful, but 651b5d61b8Smrg# WITHOUT ANY WARRANTY; without even the implied warranty of 661b5d61b8Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 671b5d61b8Smrg# Public License for more details. 681b5d61b8Smrg# 691b5d61b8Smrg# You should have received a copy of the GNU General Public License along 701b5d61b8Smrg# with this program. If not, see <http://www.gnu.org/licenses/>. 711b5d61b8Smrg# 721b5d61b8Smrg# As a special exception, the respective Autoconf Macro's copyright owner 731b5d61b8Smrg# gives unlimited permission to copy, distribute and modify the configure 741b5d61b8Smrg# scripts that are the output of Autoconf when processing the Macro. You 751b5d61b8Smrg# need not follow the terms of the GNU General Public License when using 761b5d61b8Smrg# or distributing such scripts, even though portions of the text of the 771b5d61b8Smrg# Macro appear in them. The GNU General Public License (GPL) does govern 781b5d61b8Smrg# all other use of the material that constitutes the Autoconf Macro. 791b5d61b8Smrg# 801b5d61b8Smrg# This special exception to the GPL applies to versions of the Autoconf 811b5d61b8Smrg# Macro released by the Autoconf Archive. When you make and distribute a 821b5d61b8Smrg# modified version of the Autoconf Macro, you may extend this special 831b5d61b8Smrg# exception to the GPL to apply to your modified version as well. 841b5d61b8Smrg 851b5d61b8Smrg#serial 21 861b5d61b8Smrg 871b5d61b8SmrgAU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) 881b5d61b8SmrgAC_DEFUN([AX_PTHREAD], [ 891b5d61b8SmrgAC_REQUIRE([AC_CANONICAL_HOST]) 901b5d61b8SmrgAC_LANG_PUSH([C]) 911b5d61b8Smrgax_pthread_ok=no 921b5d61b8Smrg 931b5d61b8Smrg# We used to check for pthread.h first, but this fails if pthread.h 941b5d61b8Smrg# requires special compiler flags (e.g. on True64 or Sequent). 951b5d61b8Smrg# It gets checked for in the link test anyway. 961b5d61b8Smrg 971b5d61b8Smrg# First of all, check if the user has set any of the PTHREAD_LIBS, 981b5d61b8Smrg# etcetera environment variables, and if threads linking works using 991b5d61b8Smrg# them: 1001b5d61b8Smrgif test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then 1011b5d61b8Smrg save_CFLAGS="$CFLAGS" 1021b5d61b8Smrg CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 1031b5d61b8Smrg save_LIBS="$LIBS" 1041b5d61b8Smrg LIBS="$PTHREAD_LIBS $LIBS" 1051b5d61b8Smrg AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) 1061b5d61b8Smrg AC_TRY_LINK_FUNC([pthread_join], [ax_pthread_ok=yes]) 1071b5d61b8Smrg AC_MSG_RESULT([$ax_pthread_ok]) 1081b5d61b8Smrg if test x"$ax_pthread_ok" = xno; then 1091b5d61b8Smrg PTHREAD_LIBS="" 1101b5d61b8Smrg PTHREAD_CFLAGS="" 1111b5d61b8Smrg fi 1121b5d61b8Smrg LIBS="$save_LIBS" 1131b5d61b8Smrg CFLAGS="$save_CFLAGS" 1141b5d61b8Smrgfi 1151b5d61b8Smrg 1161b5d61b8Smrg# We must check for the threads library under a number of different 1171b5d61b8Smrg# names; the ordering is very important because some systems 1181b5d61b8Smrg# (e.g. DEC) have both -lpthread and -lpthreads, where one of the 1191b5d61b8Smrg# libraries is broken (non-POSIX). 1201b5d61b8Smrg 1211b5d61b8Smrg# Create a list of thread flags to try. Items starting with a "-" are 1221b5d61b8Smrg# C compiler flags, and other items are library names, except for "none" 1231b5d61b8Smrg# which indicates that we try without any flags at all, and "pthread-config" 1241b5d61b8Smrg# which is a program returning the flags for the Pth emulation library. 1251b5d61b8Smrg 1261b5d61b8Smrgax_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" 1271b5d61b8Smrg 1281b5d61b8Smrg# The ordering *is* (sometimes) important. Some notes on the 1291b5d61b8Smrg# individual items follow: 1301b5d61b8Smrg 1311b5d61b8Smrg# pthreads: AIX (must check this before -lpthread) 1321b5d61b8Smrg# none: in case threads are in libc; should be tried before -Kthread and 1331b5d61b8Smrg# other compiler flags to prevent continual compiler warnings 1341b5d61b8Smrg# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) 1351b5d61b8Smrg# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) 1361b5d61b8Smrg# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) 1371b5d61b8Smrg# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) 1381b5d61b8Smrg# -pthreads: Solaris/gcc 1391b5d61b8Smrg# -mthreads: Mingw32/gcc, Lynx/gcc 1401b5d61b8Smrg# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it 1411b5d61b8Smrg# doesn't hurt to check since this sometimes defines pthreads too; 1421b5d61b8Smrg# also defines -D_REENTRANT) 1431b5d61b8Smrg# ... -mt is also the pthreads flag for HP/aCC 1441b5d61b8Smrg# pthread: Linux, etcetera 1451b5d61b8Smrg# --thread-safe: KAI C++ 1461b5d61b8Smrg# pthread-config: use pthread-config program (for GNU Pth library) 1471b5d61b8Smrg 1481b5d61b8Smrgcase ${host_os} in 1491b5d61b8Smrg solaris*) 1501b5d61b8Smrg 1511b5d61b8Smrg # On Solaris (at least, for some versions), libc contains stubbed 1521b5d61b8Smrg # (non-functional) versions of the pthreads routines, so link-based 1531b5d61b8Smrg # tests will erroneously succeed. (We need to link with -pthreads/-mt/ 1541b5d61b8Smrg # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather 1551b5d61b8Smrg # a function called by this macro, so we could check for that, but 1561b5d61b8Smrg # who knows whether they'll stub that too in a future libc.) So, 1571b5d61b8Smrg # we'll just look for -pthreads and -lpthread first: 1581b5d61b8Smrg 1591b5d61b8Smrg ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags" 1601b5d61b8Smrg ;; 1611b5d61b8Smrg 1621b5d61b8Smrg darwin*) 1631b5d61b8Smrg ax_pthread_flags="-pthread $ax_pthread_flags" 1641b5d61b8Smrg ;; 1651b5d61b8Smrg netbsd*) 1661b5d61b8Smrg # use libc stubs, don't link against libpthread, to allow 1671b5d61b8Smrg # dynamic loading 1681b5d61b8Smrg ax_pthread_flags="" 1691b5d61b8Smrg ;; 1701b5d61b8Smrgesac 1711b5d61b8Smrg 1721b5d61b8Smrg# Clang doesn't consider unrecognized options an error unless we specify 1731b5d61b8Smrg# -Werror. We throw in some extra Clang-specific options to ensure that 1741b5d61b8Smrg# this doesn't happen for GCC, which also accepts -Werror. 1751b5d61b8Smrg 1761b5d61b8SmrgAC_MSG_CHECKING([if compiler needs -Werror to reject unknown flags]) 1771b5d61b8Smrgsave_CFLAGS="$CFLAGS" 1781b5d61b8Smrgax_pthread_extra_flags="-Werror" 1791b5d61b8SmrgCFLAGS="$CFLAGS $ax_pthread_extra_flags -Wunknown-warning-option -Wsizeof-array-argument" 1801b5d61b8SmrgAC_COMPILE_IFELSE([AC_LANG_PROGRAM([int foo(void);],[foo()])], 1811b5d61b8Smrg [AC_MSG_RESULT([yes])], 1821b5d61b8Smrg [ax_pthread_extra_flags= 1831b5d61b8Smrg AC_MSG_RESULT([no])]) 1841b5d61b8SmrgCFLAGS="$save_CFLAGS" 1851b5d61b8Smrg 1861b5d61b8Smrgif test x"$ax_pthread_ok" = xno; then 1871b5d61b8Smrgfor flag in $ax_pthread_flags; do 1881b5d61b8Smrg 1891b5d61b8Smrg case $flag in 1901b5d61b8Smrg none) 1911b5d61b8Smrg AC_MSG_CHECKING([whether pthreads work without any flags]) 1921b5d61b8Smrg ;; 1931b5d61b8Smrg 1941b5d61b8Smrg -*) 1951b5d61b8Smrg AC_MSG_CHECKING([whether pthreads work with $flag]) 1961b5d61b8Smrg PTHREAD_CFLAGS="$flag" 1971b5d61b8Smrg ;; 1981b5d61b8Smrg 1991b5d61b8Smrg pthread-config) 2001b5d61b8Smrg AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no]) 2011b5d61b8Smrg if test x"$ax_pthread_config" = xno; then continue; fi 2021b5d61b8Smrg PTHREAD_CFLAGS="`pthread-config --cflags`" 2031b5d61b8Smrg PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" 2041b5d61b8Smrg ;; 2051b5d61b8Smrg 2061b5d61b8Smrg *) 2071b5d61b8Smrg AC_MSG_CHECKING([for the pthreads library -l$flag]) 2081b5d61b8Smrg PTHREAD_LIBS="-l$flag" 2091b5d61b8Smrg ;; 2101b5d61b8Smrg esac 2111b5d61b8Smrg 2121b5d61b8Smrg save_LIBS="$LIBS" 2131b5d61b8Smrg save_CFLAGS="$CFLAGS" 2141b5d61b8Smrg LIBS="$PTHREAD_LIBS $LIBS" 2151b5d61b8Smrg CFLAGS="$CFLAGS $PTHREAD_CFLAGS $ax_pthread_extra_flags" 2161b5d61b8Smrg 2171b5d61b8Smrg # Check for various functions. We must include pthread.h, 2181b5d61b8Smrg # since some functions may be macros. (On the Sequent, we 2191b5d61b8Smrg # need a special flag -Kthread to make this header compile.) 2201b5d61b8Smrg # We check for pthread_join because it is in -lpthread on IRIX 2211b5d61b8Smrg # while pthread_create is in libc. We check for pthread_attr_init 2221b5d61b8Smrg # due to DEC craziness with -lpthreads. We check for 2231b5d61b8Smrg # pthread_cleanup_push because it is one of the few pthread 2241b5d61b8Smrg # functions on Solaris that doesn't have a non-functional libc stub. 2251b5d61b8Smrg # We try pthread_create on general principles. 2261b5d61b8Smrg AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h> 2271b5d61b8Smrg static void routine(void *a) { a = 0; } 2281b5d61b8Smrg static void *start_routine(void *a) { return a; }], 2291b5d61b8Smrg [pthread_t th; pthread_attr_t attr; 2301b5d61b8Smrg pthread_create(&th, 0, start_routine, 0); 2311b5d61b8Smrg pthread_join(th, 0); 2321b5d61b8Smrg pthread_attr_init(&attr); 2331b5d61b8Smrg pthread_cleanup_push(routine, 0); 2341b5d61b8Smrg pthread_cleanup_pop(0) /* ; */])], 2351b5d61b8Smrg [ax_pthread_ok=yes], 2361b5d61b8Smrg []) 2371b5d61b8Smrg 2381b5d61b8Smrg LIBS="$save_LIBS" 2391b5d61b8Smrg CFLAGS="$save_CFLAGS" 2401b5d61b8Smrg 2411b5d61b8Smrg AC_MSG_RESULT([$ax_pthread_ok]) 2421b5d61b8Smrg if test "x$ax_pthread_ok" = xyes; then 2431b5d61b8Smrg break; 2441b5d61b8Smrg fi 2451b5d61b8Smrg 2461b5d61b8Smrg PTHREAD_LIBS="" 2471b5d61b8Smrg PTHREAD_CFLAGS="" 2481b5d61b8Smrgdone 2491b5d61b8Smrgfi 2501b5d61b8Smrg 2511b5d61b8Smrg# Various other checks: 2521b5d61b8Smrgif test "x$ax_pthread_ok" = xyes; then 2531b5d61b8Smrg save_LIBS="$LIBS" 2541b5d61b8Smrg LIBS="$PTHREAD_LIBS $LIBS" 2551b5d61b8Smrg save_CFLAGS="$CFLAGS" 2561b5d61b8Smrg CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 2571b5d61b8Smrg 2581b5d61b8Smrg # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. 2591b5d61b8Smrg AC_MSG_CHECKING([for joinable pthread attribute]) 2601b5d61b8Smrg attr_name=unknown 2611b5d61b8Smrg for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do 2621b5d61b8Smrg AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>], 2631b5d61b8Smrg [int attr = $attr; return attr /* ; */])], 2641b5d61b8Smrg [attr_name=$attr; break], 2651b5d61b8Smrg []) 2661b5d61b8Smrg done 2671b5d61b8Smrg AC_MSG_RESULT([$attr_name]) 2681b5d61b8Smrg if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then 2691b5d61b8Smrg AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], [$attr_name], 2701b5d61b8Smrg [Define to necessary symbol if this constant 2711b5d61b8Smrg uses a non-standard name on your system.]) 2721b5d61b8Smrg fi 2731b5d61b8Smrg 2741b5d61b8Smrg AC_MSG_CHECKING([if more special flags are required for pthreads]) 2751b5d61b8Smrg flag=no 2761b5d61b8Smrg case ${host_os} in 2771b5d61b8Smrg aix* | freebsd* | darwin*) flag="-D_THREAD_SAFE";; 2781b5d61b8Smrg osf* | hpux*) flag="-D_REENTRANT";; 2791b5d61b8Smrg solaris*) 2801b5d61b8Smrg if test "$GCC" = "yes"; then 2811b5d61b8Smrg flag="-D_REENTRANT" 2821b5d61b8Smrg else 2831b5d61b8Smrg # TODO: What about Clang on Solaris? 2841b5d61b8Smrg flag="-mt -D_REENTRANT" 2851b5d61b8Smrg fi 2861b5d61b8Smrg ;; 2871b5d61b8Smrg esac 2881b5d61b8Smrg AC_MSG_RESULT([$flag]) 2891b5d61b8Smrg if test "x$flag" != xno; then 2901b5d61b8Smrg PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" 2911b5d61b8Smrg fi 2921b5d61b8Smrg 2931b5d61b8Smrg AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT], 2941b5d61b8Smrg [ax_cv_PTHREAD_PRIO_INHERIT], [ 2951b5d61b8Smrg AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]], 2961b5d61b8Smrg [[int i = PTHREAD_PRIO_INHERIT;]])], 2971b5d61b8Smrg [ax_cv_PTHREAD_PRIO_INHERIT=yes], 2981b5d61b8Smrg [ax_cv_PTHREAD_PRIO_INHERIT=no]) 2991b5d61b8Smrg ]) 3001b5d61b8Smrg AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"], 3011b5d61b8Smrg [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])]) 3021b5d61b8Smrg 3031b5d61b8Smrg LIBS="$save_LIBS" 3041b5d61b8Smrg CFLAGS="$save_CFLAGS" 3051b5d61b8Smrg 3061b5d61b8Smrg # More AIX lossage: compile with *_r variant 3071b5d61b8Smrg if test "x$GCC" != xyes; then 3081b5d61b8Smrg case $host_os in 3091b5d61b8Smrg aix*) 3101b5d61b8Smrg AS_CASE(["x/$CC"], 3111b5d61b8Smrg [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6], 3121b5d61b8Smrg [#handle absolute path differently from PATH based program lookup 3131b5d61b8Smrg AS_CASE(["x$CC"], 3141b5d61b8Smrg [x/*], 3151b5d61b8Smrg [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])], 3161b5d61b8Smrg [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])]) 3171b5d61b8Smrg ;; 3181b5d61b8Smrg esac 3191b5d61b8Smrg fi 3201b5d61b8Smrgfi 3211b5d61b8Smrg 3221b5d61b8Smrgtest -n "$PTHREAD_CC" || PTHREAD_CC="$CC" 3231b5d61b8Smrg 3241b5d61b8SmrgAC_SUBST([PTHREAD_LIBS]) 3251b5d61b8SmrgAC_SUBST([PTHREAD_CFLAGS]) 3261b5d61b8SmrgAC_SUBST([PTHREAD_CC]) 3271b5d61b8Smrg 3281b5d61b8Smrg# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: 3291b5d61b8Smrgif test x"$ax_pthread_ok" = xyes; then 3301b5d61b8Smrg ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1]) 3311b5d61b8Smrg : 3321b5d61b8Smrgelse 3331b5d61b8Smrg ax_pthread_ok=no 3341b5d61b8Smrg $2 3351b5d61b8Smrgfi 3361b5d61b8SmrgAC_LANG_POP 3371b5d61b8Smrg])dnl AX_PTHREAD 338