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