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