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