1281348Scy##### http://autoconf-archive.cryp.to/acx_pthread.html 2281348Scy# 3281348Scy# SYNOPSIS 4281348Scy# 5281348Scy# ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) 6281348Scy# 7281348Scy# DESCRIPTION 8281348Scy# 9281348Scy# This macro figures out how to build C programs using POSIX threads. 10281348Scy# It sets the PTHREAD_LIBS output variable to the threads library and 11281348Scy# linker flags, and the PTHREAD_CFLAGS output variable to any special 12281348Scy# C compiler flags that are needed. (The user can also force certain 13281348Scy# compiler flags/libs to be tested by setting these environment 14281348Scy# variables.) 15281348Scy# 16281348Scy# Also sets PTHREAD_CC to any special C compiler that is needed for 17281348Scy# multi-threaded programs (defaults to the value of CC otherwise). 18281348Scy# (This is necessary on AIX to use the special cc_r compiler alias.) 19281348Scy# 20281348Scy# NOTE: You are assumed to not only compile your program with these 21281348Scy# flags, but also link it with them as well. e.g. you should link 22281348Scy# with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS 23281348Scy# $LIBS 24281348Scy# 25281348Scy# If you are only building threads programs, you may wish to use 26281348Scy# these variables in your default LIBS, CFLAGS, and CC: 27281348Scy# 28281348Scy# LIBS="$PTHREAD_LIBS $LIBS" 29281348Scy# CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 30281348Scy# CC="$PTHREAD_CC" 31281348Scy# 32281348Scy# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute 33281348Scy# constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to 34281348Scy# that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). 35281348Scy# 36281348Scy# ACTION-IF-FOUND is a list of shell commands to run if a threads 37281348Scy# library is found, and ACTION-IF-NOT-FOUND is a list of commands to 38281348Scy# run it if it is not found. If ACTION-IF-FOUND is not specified, the 39281348Scy# default action will define HAVE_PTHREAD. 40281348Scy# 41281348Scy# Please let the authors know if this macro fails on any platform, or 42281348Scy# if you have any other suggestions or comments. This macro was based 43281348Scy# on work by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) 44281348Scy# (with help from M. Frigo), as well as ac_pthread and hb_pthread 45281348Scy# macros posted by Alejandro Forero Cuervo to the autoconf macro 46281348Scy# repository. We are also grateful for the helpful feedback of 47281348Scy# numerous users. 48281348Scy# 49281348Scy# LAST MODIFICATION 50281348Scy# 51281348Scy# 2007-07-29 52281348Scy# 53281348Scy# COPYLEFT 54281348Scy# 55281348Scy# Copyright (c) 2007 Steven G. Johnson <stevenj@alum.mit.edu> 56281348Scy# 57281348Scy# This program is free software: you can redistribute it and/or 58281348Scy# modify it under the terms of the GNU General Public License as 59281348Scy# published by the Free Software Foundation, either version 3 of the 60281348Scy# License, or (at your option) any later version. 61281348Scy# 62281348Scy# This program is distributed in the hope that it will be useful, but 63281348Scy# WITHOUT ANY WARRANTY; without even the implied warranty of 64281348Scy# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 65281348Scy# General Public License for more details. 66281348Scy# 67281348Scy# You should have received a copy of the GNU General Public License 68281348Scy# along with this program. If not, see 69281348Scy# <http://www.gnu.org/licenses/>. 70281348Scy# 71281348Scy# As a special exception, the respective Autoconf Macro's copyright 72281348Scy# owner gives unlimited permission to copy, distribute and modify the 73281348Scy# configure scripts that are the output of Autoconf when processing 74281348Scy# the Macro. You need not follow the terms of the GNU General Public 75281348Scy# License when using or distributing such scripts, even though 76281348Scy# portions of the text of the Macro appear in them. The GNU General 77281348Scy# Public License (GPL) does govern all other use of the material that 78281348Scy# constitutes the Autoconf Macro. 79281348Scy# 80281348Scy# This special exception to the GPL applies to versions of the 81281348Scy# Autoconf Macro released by the Autoconf Macro Archive. When you 82281348Scy# make and distribute a modified version of the Autoconf Macro, you 83281348Scy# may extend this special exception to the GPL to apply to your 84281348Scy# modified version as well. 85281348Scy 86281348ScyAC_DEFUN([ACX_PTHREAD], [ 87281348ScyAC_REQUIRE([AC_CANONICAL_HOST]) 88281348ScyAC_LANG_SAVE 89281348ScyAC_LANG_C 90281348Scyacx_pthread_ok=no 91281348Scy 92281348Scy# We used to check for pthread.h first, but this fails if pthread.h 93281348Scy# requires special compiler flags (e.g. on True64 or Sequent). 94281348Scy# It gets checked for in the link test anyway. 95281348Scy 96281348Scy# First of all, check if the user has set any of the PTHREAD_LIBS, 97281348Scy# etcetera environment variables, and if threads linking works using 98281348Scy# them: 99281348Scyif test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then 100281348Scy save_CFLAGS="$CFLAGS" 101281348Scy CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 102281348Scy save_LIBS="$LIBS" 103281348Scy LIBS="$PTHREAD_LIBS $LIBS" 104281348Scy AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) 105281348Scy AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes) 106281348Scy AC_MSG_RESULT($acx_pthread_ok) 107281348Scy if test x"$acx_pthread_ok" = xno; then 108281348Scy PTHREAD_LIBS="" 109281348Scy PTHREAD_CFLAGS="" 110281348Scy fi 111281348Scy LIBS="$save_LIBS" 112281348Scy CFLAGS="$save_CFLAGS" 113281348Scyfi 114281348Scy 115281348Scy# We must check for the threads library under a number of different 116281348Scy# names; the ordering is very important because some systems 117281348Scy# (e.g. DEC) have both -lpthread and -lpthreads, where one of the 118281348Scy# libraries is broken (non-POSIX). 119281348Scy 120281348Scy# Create a list of thread flags to try. Items starting with a "-" are 121281348Scy# C compiler flags, and other items are library names, except for "none" 122281348Scy# which indicates that we try without any flags at all, and "pthread-config" 123281348Scy# which is a program returning the flags for the Pth emulation library. 124281348Scy 125281348Scyacx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" 126281348Scy 127281348Scy# The ordering *is* (sometimes) important. Some notes on the 128281348Scy# individual items follow: 129281348Scy 130281348Scy# pthreads: AIX (must check this before -lpthread) 131281348Scy# none: in case threads are in libc; should be tried before -Kthread and 132281348Scy# other compiler flags to prevent continual compiler warnings 133281348Scy# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) 134281348Scy# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) 135281348Scy# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) 136281348Scy# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) 137281348Scy# -pthreads: Solaris/gcc 138281348Scy# -mthreads: Mingw32/gcc, Lynx/gcc 139281348Scy# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it 140281348Scy# doesn't hurt to check since this sometimes defines pthreads too; 141281348Scy# also defines -D_REENTRANT) 142281348Scy# ... -mt is also the pthreads flag for HP/aCC 143281348Scy# pthread: Linux, etcetera 144281348Scy# --thread-safe: KAI C++ 145281348Scy# pthread-config: use pthread-config program (for GNU Pth library) 146281348Scy 147281348Scycase "${host_cpu}-${host_os}" in 148281348Scy *solaris*) 149281348Scy 150281348Scy # On Solaris (at least, for some versions), libc contains stubbed 151281348Scy # (non-functional) versions of the pthreads routines, so link-based 152281348Scy # tests will erroneously succeed. (We need to link with -pthreads/-mt/ 153281348Scy # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather 154281348Scy # a function called by this macro, so we could check for that, but 155281348Scy # who knows whether they'll stub that too in a future libc.) So, 156281348Scy # we'll just look for -pthreads and -lpthread first: 157281348Scy 158281348Scy acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags" 159281348Scy ;; 160281348Scyesac 161281348Scy 162281348Scyif test x"$acx_pthread_ok" = xno; then 163281348Scyfor flag in $acx_pthread_flags; do 164281348Scy 165281348Scy case $flag in 166281348Scy none) 167281348Scy AC_MSG_CHECKING([whether pthreads work without any flags]) 168281348Scy ;; 169281348Scy 170281348Scy -*) 171281348Scy AC_MSG_CHECKING([whether pthreads work with $flag]) 172281348Scy PTHREAD_CFLAGS="$flag" 173281348Scy ;; 174281348Scy 175281348Scy pthread-config) 176281348Scy AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no) 177281348Scy if test x"$acx_pthread_config" = xno; then continue; fi 178281348Scy PTHREAD_CFLAGS="`pthread-config --cflags`" 179281348Scy PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" 180281348Scy ;; 181281348Scy 182281348Scy *) 183281348Scy AC_MSG_CHECKING([for the pthreads library -l$flag]) 184281348Scy PTHREAD_LIBS="-l$flag" 185281348Scy ;; 186281348Scy esac 187281348Scy 188281348Scy save_LIBS="$LIBS" 189281348Scy save_CFLAGS="$CFLAGS" 190281348Scy LIBS="$PTHREAD_LIBS $LIBS" 191281348Scy CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 192281348Scy 193281348Scy # Check for various functions. We must include pthread.h, 194281348Scy # since some functions may be macros. (On the Sequent, we 195281348Scy # need a special flag -Kthread to make this header compile.) 196281348Scy # We check for pthread_join because it is in -lpthread on IRIX 197281348Scy # while pthread_create is in libc. We check for pthread_attr_init 198281348Scy # due to DEC craziness with -lpthreads. We check for 199281348Scy # pthread_cleanup_push because it is one of the few pthread 200281348Scy # functions on Solaris that doesn't have a non-functional libc stub. 201281348Scy # We try pthread_create on general principles. 202281348Scy AC_TRY_LINK([#include <pthread.h>], 203281348Scy [pthread_t th; pthread_join(th, 0); 204281348Scy pthread_attr_init(0); pthread_cleanup_push(0, 0); 205281348Scy pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], 206281348Scy [acx_pthread_ok=yes]) 207281348Scy 208281348Scy LIBS="$save_LIBS" 209281348Scy CFLAGS="$save_CFLAGS" 210281348Scy 211281348Scy AC_MSG_RESULT($acx_pthread_ok) 212281348Scy if test "x$acx_pthread_ok" = xyes; then 213281348Scy break; 214281348Scy fi 215281348Scy 216281348Scy PTHREAD_LIBS="" 217281348Scy PTHREAD_CFLAGS="" 218281348Scydone 219281348Scyfi 220281348Scy 221281348Scy# Various other checks: 222281348Scyif test "x$acx_pthread_ok" = xyes; then 223281348Scy save_LIBS="$LIBS" 224281348Scy LIBS="$PTHREAD_LIBS $LIBS" 225281348Scy save_CFLAGS="$CFLAGS" 226281348Scy CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 227281348Scy 228281348Scy # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. 229281348Scy AC_MSG_CHECKING([for joinable pthread attribute]) 230281348Scy attr_name=unknown 231281348Scy for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do 232281348Scy AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;], 233281348Scy [attr_name=$attr; break]) 234281348Scy done 235281348Scy AC_MSG_RESULT($attr_name) 236281348Scy if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then 237281348Scy AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name, 238281348Scy [Define to necessary symbol if this constant 239281348Scy uses a non-standard name on your system.]) 240281348Scy fi 241281348Scy 242281348Scy AC_MSG_CHECKING([if more special flags are required for pthreads]) 243281348Scy flag=no 244281348Scy case "${host_cpu}-${host_os}" in 245281348Scy *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";; 246281348Scy *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; 247281348Scy esac 248281348Scy AC_MSG_RESULT(${flag}) 249281348Scy if test "x$flag" != xno; then 250281348Scy PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" 251281348Scy fi 252281348Scy 253281348Scy LIBS="$save_LIBS" 254281348Scy CFLAGS="$save_CFLAGS" 255281348Scy 256281348Scy # More AIX lossage: must compile with xlc_r or cc_r 257281348Scy if test x"$GCC" != xyes; then 258281348Scy AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC}) 259281348Scy else 260281348Scy PTHREAD_CC=$CC 261281348Scy fi 262281348Scyelse 263281348Scy PTHREAD_CC="$CC" 264281348Scyfi 265281348Scy 266281348ScyAC_SUBST(PTHREAD_LIBS) 267281348ScyAC_SUBST(PTHREAD_CFLAGS) 268281348ScyAC_SUBST(PTHREAD_CC) 269281348Scy 270281348Scy# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: 271281348Scyif test x"$acx_pthread_ok" = xyes; then 272281348Scy ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) 273281348Scy : 274281348Scyelse 275281348Scy acx_pthread_ok=no 276281348Scy $2 277281348Scyfi 278281348ScyAC_LANG_RESTORE 279281348Scy])dnl ACX_PTHREAD 280