xref: /freebsd/contrib/unbound/ax_pthread.m4 (revision 335c7cda12138f2aefa41fb739707612cc12a9be)
117d15b25SDag-Erling Smørgrav# ===========================================================================
2*335c7cdaSCy Schubert#        https://www.gnu.org/software/autoconf-archive/ax_pthread.html
317d15b25SDag-Erling Smørgrav# ===========================================================================
417d15b25SDag-Erling Smørgrav#
517d15b25SDag-Erling Smørgrav# SYNOPSIS
617d15b25SDag-Erling Smørgrav#
717d15b25SDag-Erling Smørgrav#   AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
817d15b25SDag-Erling Smørgrav#
917d15b25SDag-Erling Smørgrav# DESCRIPTION
1017d15b25SDag-Erling Smørgrav#
1117d15b25SDag-Erling Smørgrav#   This macro figures out how to build C programs using POSIX threads. It
1217d15b25SDag-Erling Smørgrav#   sets the PTHREAD_LIBS output variable to the threads library and linker
1317d15b25SDag-Erling Smørgrav#   flags, and the PTHREAD_CFLAGS output variable to any special C compiler
1417d15b25SDag-Erling Smørgrav#   flags that are needed. (The user can also force certain compiler
1517d15b25SDag-Erling Smørgrav#   flags/libs to be tested by setting these environment variables.)
1617d15b25SDag-Erling Smørgrav#
17*335c7cdaSCy Schubert#   Also sets PTHREAD_CC and PTHREAD_CXX to any special C compiler that is
18*335c7cdaSCy Schubert#   needed for multi-threaded programs (defaults to the value of CC
19*335c7cdaSCy Schubert#   respectively CXX otherwise). (This is necessary on e.g. AIX to use the
20*335c7cdaSCy Schubert#   special cc_r/CC_r compiler alias.)
2117d15b25SDag-Erling Smørgrav#
2217d15b25SDag-Erling Smørgrav#   NOTE: You are assumed to not only compile your program with these flags,
23*335c7cdaSCy Schubert#   but also to link with them as well. For example, you might link with
2417d15b25SDag-Erling Smørgrav#   $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
25*335c7cdaSCy Schubert#   $PTHREAD_CXX $CXXFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
2617d15b25SDag-Erling Smørgrav#
27*335c7cdaSCy Schubert#   If you are only building threaded programs, you may wish to use these
2817d15b25SDag-Erling Smørgrav#   variables in your default LIBS, CFLAGS, and CC:
2917d15b25SDag-Erling Smørgrav#
3017d15b25SDag-Erling Smørgrav#     LIBS="$PTHREAD_LIBS $LIBS"
3117d15b25SDag-Erling Smørgrav#     CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
32*335c7cdaSCy Schubert#     CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
3317d15b25SDag-Erling Smørgrav#     CC="$PTHREAD_CC"
34*335c7cdaSCy Schubert#     CXX="$PTHREAD_CXX"
3517d15b25SDag-Erling Smørgrav#
3617d15b25SDag-Erling Smørgrav#   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
37*335c7cdaSCy Schubert#   has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to
38*335c7cdaSCy Schubert#   that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
3917d15b25SDag-Erling Smørgrav#
4017d15b25SDag-Erling Smørgrav#   Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
4117d15b25SDag-Erling Smørgrav#   PTHREAD_PRIO_INHERIT symbol is defined when compiling with
4217d15b25SDag-Erling Smørgrav#   PTHREAD_CFLAGS.
4317d15b25SDag-Erling Smørgrav#
4417d15b25SDag-Erling Smørgrav#   ACTION-IF-FOUND is a list of shell commands to run if a threads library
4517d15b25SDag-Erling Smørgrav#   is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
4617d15b25SDag-Erling Smørgrav#   is not found. If ACTION-IF-FOUND is not specified, the default action
4717d15b25SDag-Erling Smørgrav#   will define HAVE_PTHREAD.
4817d15b25SDag-Erling Smørgrav#
4917d15b25SDag-Erling Smørgrav#   Please let the authors know if this macro fails on any platform, or if
5017d15b25SDag-Erling Smørgrav#   you have any other suggestions or comments. This macro was based on work
5117d15b25SDag-Erling Smørgrav#   by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
5217d15b25SDag-Erling Smørgrav#   from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
5317d15b25SDag-Erling Smørgrav#   Alejandro Forero Cuervo to the autoconf macro repository. We are also
5417d15b25SDag-Erling Smørgrav#   grateful for the helpful feedback of numerous users.
5517d15b25SDag-Erling Smørgrav#
5617d15b25SDag-Erling Smørgrav#   Updated for Autoconf 2.68 by Daniel Richard G.
5717d15b25SDag-Erling Smørgrav#
5817d15b25SDag-Erling Smørgrav# LICENSE
5917d15b25SDag-Erling Smørgrav#
6017d15b25SDag-Erling Smørgrav#   Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
6117d15b25SDag-Erling Smørgrav#   Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
62*335c7cdaSCy Schubert#   Copyright (c) 2019 Marc Stevens <marc.stevens@cwi.nl>
6317d15b25SDag-Erling Smørgrav#
6417d15b25SDag-Erling Smørgrav#   This program is free software: you can redistribute it and/or modify it
6517d15b25SDag-Erling Smørgrav#   under the terms of the GNU General Public License as published by the
6617d15b25SDag-Erling Smørgrav#   Free Software Foundation, either version 3 of the License, or (at your
6717d15b25SDag-Erling Smørgrav#   option) any later version.
6817d15b25SDag-Erling Smørgrav#
6917d15b25SDag-Erling Smørgrav#   This program is distributed in the hope that it will be useful, but
7017d15b25SDag-Erling Smørgrav#   WITHOUT ANY WARRANTY; without even the implied warranty of
7117d15b25SDag-Erling Smørgrav#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
7217d15b25SDag-Erling Smørgrav#   Public License for more details.
7317d15b25SDag-Erling Smørgrav#
7417d15b25SDag-Erling Smørgrav#   You should have received a copy of the GNU General Public License along
75*335c7cdaSCy Schubert#   with this program. If not, see <https://www.gnu.org/licenses/>.
7617d15b25SDag-Erling Smørgrav#
7717d15b25SDag-Erling Smørgrav#   As a special exception, the respective Autoconf Macro's copyright owner
7817d15b25SDag-Erling Smørgrav#   gives unlimited permission to copy, distribute and modify the configure
7917d15b25SDag-Erling Smørgrav#   scripts that are the output of Autoconf when processing the Macro. You
8017d15b25SDag-Erling Smørgrav#   need not follow the terms of the GNU General Public License when using
8117d15b25SDag-Erling Smørgrav#   or distributing such scripts, even though portions of the text of the
8217d15b25SDag-Erling Smørgrav#   Macro appear in them. The GNU General Public License (GPL) does govern
8317d15b25SDag-Erling Smørgrav#   all other use of the material that constitutes the Autoconf Macro.
8417d15b25SDag-Erling Smørgrav#
8517d15b25SDag-Erling Smørgrav#   This special exception to the GPL applies to versions of the Autoconf
8617d15b25SDag-Erling Smørgrav#   Macro released by the Autoconf Archive. When you make and distribute a
8717d15b25SDag-Erling Smørgrav#   modified version of the Autoconf Macro, you may extend this special
8817d15b25SDag-Erling Smørgrav#   exception to the GPL to apply to your modified version as well.
8917d15b25SDag-Erling Smørgrav
90*335c7cdaSCy Schubert#serial 31
9117d15b25SDag-Erling Smørgrav
9217d15b25SDag-Erling SmørgravAU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
9317d15b25SDag-Erling SmørgravAC_DEFUN([AX_PTHREAD], [
9417d15b25SDag-Erling SmørgravAC_REQUIRE([AC_CANONICAL_HOST])
95*335c7cdaSCy SchubertAC_REQUIRE([AC_PROG_CC])
96*335c7cdaSCy SchubertAC_REQUIRE([AC_PROG_SED])
9717d15b25SDag-Erling SmørgravAC_LANG_PUSH([C])
9817d15b25SDag-Erling Smørgravax_pthread_ok=no
9917d15b25SDag-Erling Smørgrav
10017d15b25SDag-Erling Smørgrav# We used to check for pthread.h first, but this fails if pthread.h
101*335c7cdaSCy Schubert# requires special compiler flags (e.g. on Tru64 or Sequent).
10217d15b25SDag-Erling Smørgrav# It gets checked for in the link test anyway.
10317d15b25SDag-Erling Smørgrav
10417d15b25SDag-Erling Smørgrav# First of all, check if the user has set any of the PTHREAD_LIBS,
10517d15b25SDag-Erling Smørgrav# etcetera environment variables, and if threads linking works using
10617d15b25SDag-Erling Smørgrav# them:
107*335c7cdaSCy Schubertif test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then
108*335c7cdaSCy Schubert        ax_pthread_save_CC="$CC"
109*335c7cdaSCy Schubert        ax_pthread_save_CFLAGS="$CFLAGS"
110*335c7cdaSCy Schubert        ax_pthread_save_LIBS="$LIBS"
111*335c7cdaSCy Schubert        AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"])
112*335c7cdaSCy Schubert        AS_IF([test "x$PTHREAD_CXX" != "x"], [CXX="$PTHREAD_CXX"])
11317d15b25SDag-Erling Smørgrav        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
11417d15b25SDag-Erling Smørgrav        LIBS="$PTHREAD_LIBS $LIBS"
115*335c7cdaSCy Schubert        AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS])
116*335c7cdaSCy Schubert        AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes])
117f61ef7f6SDag-Erling Smørgrav        AC_MSG_RESULT([$ax_pthread_ok])
118*335c7cdaSCy Schubert        if test "x$ax_pthread_ok" = "xno"; then
11917d15b25SDag-Erling Smørgrav                PTHREAD_LIBS=""
12017d15b25SDag-Erling Smørgrav                PTHREAD_CFLAGS=""
12117d15b25SDag-Erling Smørgrav        fi
122*335c7cdaSCy Schubert        CC="$ax_pthread_save_CC"
123*335c7cdaSCy Schubert        CFLAGS="$ax_pthread_save_CFLAGS"
124*335c7cdaSCy Schubert        LIBS="$ax_pthread_save_LIBS"
12517d15b25SDag-Erling Smørgravfi
12617d15b25SDag-Erling Smørgrav
12717d15b25SDag-Erling Smørgrav# We must check for the threads library under a number of different
12817d15b25SDag-Erling Smørgrav# names; the ordering is very important because some systems
12917d15b25SDag-Erling Smørgrav# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
13017d15b25SDag-Erling Smørgrav# libraries is broken (non-POSIX).
13117d15b25SDag-Erling Smørgrav
132*335c7cdaSCy Schubert# Create a list of thread flags to try. Items with a "," contain both
133*335c7cdaSCy Schubert# C compiler flags (before ",") and linker flags (after ","). Other items
134*335c7cdaSCy Schubert# starting with a "-" are C compiler flags, and remaining items are
135*335c7cdaSCy Schubert# library names, except for "none" which indicates that we try without
136*335c7cdaSCy Schubert# any flags at all, and "pthread-config" which is a program returning
137*335c7cdaSCy Schubert# the flags for the Pth emulation library.
13817d15b25SDag-Erling Smørgrav
139*335c7cdaSCy Schubertax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
14017d15b25SDag-Erling Smørgrav
14117d15b25SDag-Erling Smørgrav# The ordering *is* (sometimes) important.  Some notes on the
14217d15b25SDag-Erling Smørgrav# individual items follow:
14317d15b25SDag-Erling Smørgrav
14417d15b25SDag-Erling Smørgrav# pthreads: AIX (must check this before -lpthread)
14517d15b25SDag-Erling Smørgrav# none: in case threads are in libc; should be tried before -Kthread and
14617d15b25SDag-Erling Smørgrav#       other compiler flags to prevent continual compiler warnings
14717d15b25SDag-Erling Smørgrav# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
148*335c7cdaSCy Schubert# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64
149*335c7cdaSCy Schubert#           (Note: HP C rejects this with "bad form for `-t' option")
150*335c7cdaSCy Schubert# -pthreads: Solaris/gcc (Note: HP C also rejects)
15117d15b25SDag-Erling Smørgrav# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
152*335c7cdaSCy Schubert#      doesn't hurt to check since this sometimes defines pthreads and
153*335c7cdaSCy Schubert#      -D_REENTRANT too), HP C (must be checked before -lpthread, which
154*335c7cdaSCy Schubert#      is present but should not be used directly; and before -mthreads,
155*335c7cdaSCy Schubert#      because the compiler interprets this as "-mt" + "-hreads")
156*335c7cdaSCy Schubert# -mthreads: Mingw32/gcc, Lynx/gcc
15717d15b25SDag-Erling Smørgrav# pthread: Linux, etcetera
15817d15b25SDag-Erling Smørgrav# --thread-safe: KAI C++
15917d15b25SDag-Erling Smørgrav# pthread-config: use pthread-config program (for GNU Pth library)
16017d15b25SDag-Erling Smørgrav
161*335c7cdaSCy Schubertcase $host_os in
162*335c7cdaSCy Schubert
163*335c7cdaSCy Schubert        freebsd*)
164*335c7cdaSCy Schubert
165*335c7cdaSCy Schubert        # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
166*335c7cdaSCy Schubert        # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
167*335c7cdaSCy Schubert
168*335c7cdaSCy Schubert        ax_pthread_flags="-kthread lthread $ax_pthread_flags"
169*335c7cdaSCy Schubert        ;;
170*335c7cdaSCy Schubert
171*335c7cdaSCy Schubert        hpux*)
172*335c7cdaSCy Schubert
173*335c7cdaSCy Schubert        # From the cc(1) man page: "[-mt] Sets various -D flags to enable
174*335c7cdaSCy Schubert        # multi-threading and also sets -lpthread."
175*335c7cdaSCy Schubert
176*335c7cdaSCy Schubert        ax_pthread_flags="-mt -pthread pthread $ax_pthread_flags"
177*335c7cdaSCy Schubert        ;;
178*335c7cdaSCy Schubert
179*335c7cdaSCy Schubert        openedition*)
180*335c7cdaSCy Schubert
181*335c7cdaSCy Schubert        # IBM z/OS requires a feature-test macro to be defined in order to
182*335c7cdaSCy Schubert        # enable POSIX threads at all, so give the user a hint if this is
183*335c7cdaSCy Schubert        # not set. (We don't define these ourselves, as they can affect
184*335c7cdaSCy Schubert        # other portions of the system API in unpredictable ways.)
185*335c7cdaSCy Schubert
186*335c7cdaSCy Schubert        AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING],
187*335c7cdaSCy Schubert            [
188*335c7cdaSCy Schubert#            if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS)
189*335c7cdaSCy Schubert             AX_PTHREAD_ZOS_MISSING
190*335c7cdaSCy Schubert#            endif
191*335c7cdaSCy Schubert            ],
192*335c7cdaSCy Schubert            [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])])
193*335c7cdaSCy Schubert        ;;
194*335c7cdaSCy Schubert
19517d15b25SDag-Erling Smørgrav        solaris*)
19617d15b25SDag-Erling Smørgrav
19717d15b25SDag-Erling Smørgrav        # On Solaris (at least, for some versions), libc contains stubbed
19817d15b25SDag-Erling Smørgrav        # (non-functional) versions of the pthreads routines, so link-based
199*335c7cdaSCy Schubert        # tests will erroneously succeed. (N.B.: The stubs are missing
200*335c7cdaSCy Schubert        # pthread_cleanup_push, or rather a function called by this macro,
201*335c7cdaSCy Schubert        # so we could check for that, but who knows whether they'll stub
202*335c7cdaSCy Schubert        # that too in a future libc.)  So we'll check first for the
203*335c7cdaSCy Schubert        # standard Solaris way of linking pthreads (-mt -lpthread).
20417d15b25SDag-Erling Smørgrav
205*335c7cdaSCy Schubert        ax_pthread_flags="-mt,-lpthread pthread $ax_pthread_flags"
20617d15b25SDag-Erling Smørgrav        ;;
20717d15b25SDag-Erling Smørgravesac
20817d15b25SDag-Erling Smørgrav
209*335c7cdaSCy Schubert# Are we compiling with Clang?
210f61ef7f6SDag-Erling Smørgrav
211*335c7cdaSCy SchubertAC_CACHE_CHECK([whether $CC is Clang],
212*335c7cdaSCy Schubert    [ax_cv_PTHREAD_CLANG],
213*335c7cdaSCy Schubert    [ax_cv_PTHREAD_CLANG=no
214*335c7cdaSCy Schubert     # Note that Autoconf sets GCC=yes for Clang as well as GCC
215*335c7cdaSCy Schubert     if test "x$GCC" = "xyes"; then
216*335c7cdaSCy Schubert        AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG],
217*335c7cdaSCy Schubert            [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */
218*335c7cdaSCy Schubert#            if defined(__clang__) && defined(__llvm__)
219*335c7cdaSCy Schubert             AX_PTHREAD_CC_IS_CLANG
220*335c7cdaSCy Schubert#            endif
221*335c7cdaSCy Schubert            ],
222*335c7cdaSCy Schubert            [ax_cv_PTHREAD_CLANG=yes])
223*335c7cdaSCy Schubert     fi
224*335c7cdaSCy Schubert    ])
225*335c7cdaSCy Schubertax_pthread_clang="$ax_cv_PTHREAD_CLANG"
226f61ef7f6SDag-Erling Smørgrav
22717d15b25SDag-Erling Smørgrav
228*335c7cdaSCy Schubert# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC)
229*335c7cdaSCy Schubert
230*335c7cdaSCy Schubert# Note that for GCC and Clang -pthread generally implies -lpthread,
231*335c7cdaSCy Schubert# except when -nostdlib is passed.
232*335c7cdaSCy Schubert# This is problematic using libtool to build C++ shared libraries with pthread:
233*335c7cdaSCy Schubert# [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25460
234*335c7cdaSCy Schubert# [2] https://bugzilla.redhat.com/show_bug.cgi?id=661333
235*335c7cdaSCy Schubert# [3] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=468555
236*335c7cdaSCy Schubert# To solve this, first try -pthread together with -lpthread for GCC
237*335c7cdaSCy Schubert
238*335c7cdaSCy SchubertAS_IF([test "x$GCC" = "xyes"],
239*335c7cdaSCy Schubert      [ax_pthread_flags="-pthread,-lpthread -pthread -pthreads $ax_pthread_flags"])
240*335c7cdaSCy Schubert
241*335c7cdaSCy Schubert# Clang takes -pthread (never supported any other flag), but we'll try with -lpthread first
242*335c7cdaSCy Schubert
243*335c7cdaSCy SchubertAS_IF([test "x$ax_pthread_clang" = "xyes"],
244*335c7cdaSCy Schubert      [ax_pthread_flags="-pthread,-lpthread -pthread"])
245*335c7cdaSCy Schubert
246*335c7cdaSCy Schubert
247*335c7cdaSCy Schubert# The presence of a feature test macro requesting re-entrant function
248*335c7cdaSCy Schubert# definitions is, on some systems, a strong hint that pthreads support is
249*335c7cdaSCy Schubert# correctly enabled
250*335c7cdaSCy Schubert
251*335c7cdaSCy Schubertcase $host_os in
252*335c7cdaSCy Schubert        darwin* | hpux* | linux* | osf* | solaris*)
253*335c7cdaSCy Schubert        ax_pthread_check_macro="_REENTRANT"
254*335c7cdaSCy Schubert        ;;
255*335c7cdaSCy Schubert
256*335c7cdaSCy Schubert        aix*)
257*335c7cdaSCy Schubert        ax_pthread_check_macro="_THREAD_SAFE"
258*335c7cdaSCy Schubert        ;;
259*335c7cdaSCy Schubert
260*335c7cdaSCy Schubert        *)
261*335c7cdaSCy Schubert        ax_pthread_check_macro="--"
262*335c7cdaSCy Schubert        ;;
263*335c7cdaSCy Schubertesac
264*335c7cdaSCy SchubertAS_IF([test "x$ax_pthread_check_macro" = "x--"],
265*335c7cdaSCy Schubert      [ax_pthread_check_cond=0],
266*335c7cdaSCy Schubert      [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"])
267*335c7cdaSCy Schubert
268*335c7cdaSCy Schubert
269*335c7cdaSCy Schubertif test "x$ax_pthread_ok" = "xno"; then
270*335c7cdaSCy Schubertfor ax_pthread_try_flag in $ax_pthread_flags; do
271*335c7cdaSCy Schubert
272*335c7cdaSCy Schubert        case $ax_pthread_try_flag in
27317d15b25SDag-Erling Smørgrav                none)
27417d15b25SDag-Erling Smørgrav                AC_MSG_CHECKING([whether pthreads work without any flags])
27517d15b25SDag-Erling Smørgrav                ;;
27617d15b25SDag-Erling Smørgrav
277*335c7cdaSCy Schubert                *,*)
278*335c7cdaSCy Schubert                PTHREAD_CFLAGS=`echo $ax_pthread_try_flag | sed "s/^\(.*\),\(.*\)$/\1/"`
279*335c7cdaSCy Schubert                PTHREAD_LIBS=`echo $ax_pthread_try_flag | sed "s/^\(.*\),\(.*\)$/\2/"`
280*335c7cdaSCy Schubert                AC_MSG_CHECKING([whether pthreads work with "$PTHREAD_CFLAGS" and "$PTHREAD_LIBS"])
281*335c7cdaSCy Schubert                ;;
282*335c7cdaSCy Schubert
28317d15b25SDag-Erling Smørgrav                -*)
284*335c7cdaSCy Schubert                AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag])
285*335c7cdaSCy Schubert                PTHREAD_CFLAGS="$ax_pthread_try_flag"
28617d15b25SDag-Erling Smørgrav                ;;
28717d15b25SDag-Erling Smørgrav
28817d15b25SDag-Erling Smørgrav                pthread-config)
289f61ef7f6SDag-Erling Smørgrav                AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
290*335c7cdaSCy Schubert                AS_IF([test "x$ax_pthread_config" = "xno"], [continue])
29117d15b25SDag-Erling Smørgrav                PTHREAD_CFLAGS="`pthread-config --cflags`"
29217d15b25SDag-Erling Smørgrav                PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
29317d15b25SDag-Erling Smørgrav                ;;
29417d15b25SDag-Erling Smørgrav
29517d15b25SDag-Erling Smørgrav                *)
296*335c7cdaSCy Schubert                AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag])
297*335c7cdaSCy Schubert                PTHREAD_LIBS="-l$ax_pthread_try_flag"
29817d15b25SDag-Erling Smørgrav                ;;
29917d15b25SDag-Erling Smørgrav        esac
30017d15b25SDag-Erling Smørgrav
301*335c7cdaSCy Schubert        ax_pthread_save_CFLAGS="$CFLAGS"
302*335c7cdaSCy Schubert        ax_pthread_save_LIBS="$LIBS"
303*335c7cdaSCy Schubert        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
30417d15b25SDag-Erling Smørgrav        LIBS="$PTHREAD_LIBS $LIBS"
30517d15b25SDag-Erling Smørgrav
30617d15b25SDag-Erling Smørgrav        # Check for various functions.  We must include pthread.h,
30717d15b25SDag-Erling Smørgrav        # since some functions may be macros.  (On the Sequent, we
30817d15b25SDag-Erling Smørgrav        # need a special flag -Kthread to make this header compile.)
30917d15b25SDag-Erling Smørgrav        # We check for pthread_join because it is in -lpthread on IRIX
31017d15b25SDag-Erling Smørgrav        # while pthread_create is in libc.  We check for pthread_attr_init
31117d15b25SDag-Erling Smørgrav        # due to DEC craziness with -lpthreads.  We check for
31217d15b25SDag-Erling Smørgrav        # pthread_cleanup_push because it is one of the few pthread
31317d15b25SDag-Erling Smørgrav        # functions on Solaris that doesn't have a non-functional libc stub.
31417d15b25SDag-Erling Smørgrav        # We try pthread_create on general principles.
315*335c7cdaSCy Schubert
31617d15b25SDag-Erling Smørgrav        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
317*335c7cdaSCy Schubert#                       if $ax_pthread_check_cond
318*335c7cdaSCy Schubert#                        error "$ax_pthread_check_macro must be defined"
319*335c7cdaSCy Schubert#                       endif
320*335c7cdaSCy Schubert                        static void *some_global = NULL;
321*335c7cdaSCy Schubert                        static void routine(void *a)
322*335c7cdaSCy Schubert                          {
323*335c7cdaSCy Schubert                             /* To avoid any unused-parameter or
324*335c7cdaSCy Schubert                                unused-but-set-parameter warning.  */
325*335c7cdaSCy Schubert                             some_global = a;
326*335c7cdaSCy Schubert                          }
32717d15b25SDag-Erling Smørgrav                        static void *start_routine(void *a) { return a; }],
32817d15b25SDag-Erling Smørgrav                       [pthread_t th; pthread_attr_t attr;
32917d15b25SDag-Erling Smørgrav                        pthread_create(&th, 0, start_routine, 0);
33017d15b25SDag-Erling Smørgrav                        pthread_join(th, 0);
33117d15b25SDag-Erling Smørgrav                        pthread_attr_init(&attr);
33217d15b25SDag-Erling Smørgrav                        pthread_cleanup_push(routine, 0);
33317d15b25SDag-Erling Smørgrav                        pthread_cleanup_pop(0) /* ; */])],
33417d15b25SDag-Erling Smørgrav            [ax_pthread_ok=yes],
33517d15b25SDag-Erling Smørgrav            [])
33617d15b25SDag-Erling Smørgrav
337*335c7cdaSCy Schubert        CFLAGS="$ax_pthread_save_CFLAGS"
338*335c7cdaSCy Schubert        LIBS="$ax_pthread_save_LIBS"
33917d15b25SDag-Erling Smørgrav
340f61ef7f6SDag-Erling Smørgrav        AC_MSG_RESULT([$ax_pthread_ok])
341*335c7cdaSCy Schubert        AS_IF([test "x$ax_pthread_ok" = "xyes"], [break])
34217d15b25SDag-Erling Smørgrav
34317d15b25SDag-Erling Smørgrav        PTHREAD_LIBS=""
34417d15b25SDag-Erling Smørgrav        PTHREAD_CFLAGS=""
34517d15b25SDag-Erling Smørgravdone
34617d15b25SDag-Erling Smørgravfi
34717d15b25SDag-Erling Smørgrav
348*335c7cdaSCy Schubert
349*335c7cdaSCy Schubert# Clang needs special handling, because older versions handle the -pthread
350*335c7cdaSCy Schubert# option in a rather... idiosyncratic way
351*335c7cdaSCy Schubert
352*335c7cdaSCy Schubertif test "x$ax_pthread_clang" = "xyes"; then
353*335c7cdaSCy Schubert
354*335c7cdaSCy Schubert        # Clang takes -pthread; it has never supported any other flag
355*335c7cdaSCy Schubert
356*335c7cdaSCy Schubert        # (Note 1: This will need to be revisited if a system that Clang
357*335c7cdaSCy Schubert        # supports has POSIX threads in a separate library.  This tends not
358*335c7cdaSCy Schubert        # to be the way of modern systems, but it's conceivable.)
359*335c7cdaSCy Schubert
360*335c7cdaSCy Schubert        # (Note 2: On some systems, notably Darwin, -pthread is not needed
361*335c7cdaSCy Schubert        # to get POSIX threads support; the API is always present and
362*335c7cdaSCy Schubert        # active.  We could reasonably leave PTHREAD_CFLAGS empty.  But
363*335c7cdaSCy Schubert        # -pthread does define _REENTRANT, and while the Darwin headers
364*335c7cdaSCy Schubert        # ignore this macro, third-party headers might not.)
365*335c7cdaSCy Schubert
366*335c7cdaSCy Schubert        # However, older versions of Clang make a point of warning the user
367*335c7cdaSCy Schubert        # that, in an invocation where only linking and no compilation is
368*335c7cdaSCy Schubert        # taking place, the -pthread option has no effect ("argument unused
369*335c7cdaSCy Schubert        # during compilation").  They expect -pthread to be passed in only
370*335c7cdaSCy Schubert        # when source code is being compiled.
371*335c7cdaSCy Schubert        #
372*335c7cdaSCy Schubert        # Problem is, this is at odds with the way Automake and most other
373*335c7cdaSCy Schubert        # C build frameworks function, which is that the same flags used in
374*335c7cdaSCy Schubert        # compilation (CFLAGS) are also used in linking.  Many systems
375*335c7cdaSCy Schubert        # supported by AX_PTHREAD require exactly this for POSIX threads
376*335c7cdaSCy Schubert        # support, and in fact it is often not straightforward to specify a
377*335c7cdaSCy Schubert        # flag that is used only in the compilation phase and not in
378*335c7cdaSCy Schubert        # linking.  Such a scenario is extremely rare in practice.
379*335c7cdaSCy Schubert        #
380*335c7cdaSCy Schubert        # Even though use of the -pthread flag in linking would only print
381*335c7cdaSCy Schubert        # a warning, this can be a nuisance for well-run software projects
382*335c7cdaSCy Schubert        # that build with -Werror.  So if the active version of Clang has
383*335c7cdaSCy Schubert        # this misfeature, we search for an option to squash it.
384*335c7cdaSCy Schubert
385*335c7cdaSCy Schubert        AC_CACHE_CHECK([whether Clang needs flag to prevent "argument unused" warning when linking with -pthread],
386*335c7cdaSCy Schubert            [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG],
387*335c7cdaSCy Schubert            [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown
388*335c7cdaSCy Schubert             # Create an alternate version of $ac_link that compiles and
389*335c7cdaSCy Schubert             # links in two steps (.c -> .o, .o -> exe) instead of one
390*335c7cdaSCy Schubert             # (.c -> exe), because the warning occurs only in the second
391*335c7cdaSCy Schubert             # step
392*335c7cdaSCy Schubert             ax_pthread_save_ac_link="$ac_link"
393*335c7cdaSCy Schubert             ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g'
394*335c7cdaSCy Schubert             ax_pthread_link_step=`AS_ECHO(["$ac_link"]) | sed "$ax_pthread_sed"`
395*335c7cdaSCy Schubert             ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)"
396*335c7cdaSCy Schubert             ax_pthread_save_CFLAGS="$CFLAGS"
397*335c7cdaSCy Schubert             for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do
398*335c7cdaSCy Schubert                AS_IF([test "x$ax_pthread_try" = "xunknown"], [break])
399*335c7cdaSCy Schubert                CFLAGS="-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS"
400*335c7cdaSCy Schubert                ac_link="$ax_pthread_save_ac_link"
401*335c7cdaSCy Schubert                AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],
402*335c7cdaSCy Schubert                    [ac_link="$ax_pthread_2step_ac_link"
403*335c7cdaSCy Schubert                     AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],
404*335c7cdaSCy Schubert                         [break])
405*335c7cdaSCy Schubert                    ])
406*335c7cdaSCy Schubert             done
407*335c7cdaSCy Schubert             ac_link="$ax_pthread_save_ac_link"
408*335c7cdaSCy Schubert             CFLAGS="$ax_pthread_save_CFLAGS"
409*335c7cdaSCy Schubert             AS_IF([test "x$ax_pthread_try" = "x"], [ax_pthread_try=no])
410*335c7cdaSCy Schubert             ax_cv_PTHREAD_CLANG_NO_WARN_FLAG="$ax_pthread_try"
411*335c7cdaSCy Schubert            ])
412*335c7cdaSCy Schubert
413*335c7cdaSCy Schubert        case "$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG" in
414*335c7cdaSCy Schubert                no | unknown) ;;
415*335c7cdaSCy Schubert                *) PTHREAD_CFLAGS="$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS" ;;
416*335c7cdaSCy Schubert        esac
417*335c7cdaSCy Schubert
418*335c7cdaSCy Schubertfi # $ax_pthread_clang = yes
419*335c7cdaSCy Schubert
420*335c7cdaSCy Schubert
421*335c7cdaSCy Schubert
42217d15b25SDag-Erling Smørgrav# Various other checks:
423*335c7cdaSCy Schubertif test "x$ax_pthread_ok" = "xyes"; then
424*335c7cdaSCy Schubert        ax_pthread_save_CFLAGS="$CFLAGS"
425*335c7cdaSCy Schubert        ax_pthread_save_LIBS="$LIBS"
42617d15b25SDag-Erling Smørgrav        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
427*335c7cdaSCy Schubert        LIBS="$PTHREAD_LIBS $LIBS"
42817d15b25SDag-Erling Smørgrav
42917d15b25SDag-Erling Smørgrav        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
430*335c7cdaSCy Schubert        AC_CACHE_CHECK([for joinable pthread attribute],
431*335c7cdaSCy Schubert            [ax_cv_PTHREAD_JOINABLE_ATTR],
432*335c7cdaSCy Schubert            [ax_cv_PTHREAD_JOINABLE_ATTR=unknown
433*335c7cdaSCy Schubert             for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
43417d15b25SDag-Erling Smørgrav                 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
435*335c7cdaSCy Schubert                                                 [int attr = $ax_pthread_attr; return attr /* ; */])],
436*335c7cdaSCy Schubert                                [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break],
43717d15b25SDag-Erling Smørgrav                                [])
43817d15b25SDag-Erling Smørgrav             done
439*335c7cdaSCy Schubert            ])
440*335c7cdaSCy Schubert        AS_IF([test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xunknown" && \
441*335c7cdaSCy Schubert               test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xPTHREAD_CREATE_JOINABLE" && \
442*335c7cdaSCy Schubert               test "x$ax_pthread_joinable_attr_defined" != "xyes"],
443*335c7cdaSCy Schubert              [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE],
444*335c7cdaSCy Schubert                                  [$ax_cv_PTHREAD_JOINABLE_ATTR],
44517d15b25SDag-Erling Smørgrav                                  [Define to necessary symbol if this constant
44617d15b25SDag-Erling Smørgrav                                   uses a non-standard name on your system.])
447*335c7cdaSCy Schubert               ax_pthread_joinable_attr_defined=yes
448*335c7cdaSCy Schubert              ])
44917d15b25SDag-Erling Smørgrav
450*335c7cdaSCy Schubert        AC_CACHE_CHECK([whether more special flags are required for pthreads],
451*335c7cdaSCy Schubert            [ax_cv_PTHREAD_SPECIAL_FLAGS],
452*335c7cdaSCy Schubert            [ax_cv_PTHREAD_SPECIAL_FLAGS=no
453*335c7cdaSCy Schubert             case $host_os in
45417d15b25SDag-Erling Smørgrav             solaris*)
455*335c7cdaSCy Schubert             ax_cv_PTHREAD_SPECIAL_FLAGS="-D_POSIX_PTHREAD_SEMANTICS"
45617d15b25SDag-Erling Smørgrav             ;;
45717d15b25SDag-Erling Smørgrav             esac
458*335c7cdaSCy Schubert            ])
459*335c7cdaSCy Schubert        AS_IF([test "x$ax_cv_PTHREAD_SPECIAL_FLAGS" != "xno" && \
460*335c7cdaSCy Schubert               test "x$ax_pthread_special_flags_added" != "xyes"],
461*335c7cdaSCy Schubert              [PTHREAD_CFLAGS="$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS"
462*335c7cdaSCy Schubert               ax_pthread_special_flags_added=yes])
46317d15b25SDag-Erling Smørgrav
46417d15b25SDag-Erling Smørgrav        AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
465*335c7cdaSCy Schubert            [ax_cv_PTHREAD_PRIO_INHERIT],
466*335c7cdaSCy Schubert            [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],
467*335c7cdaSCy Schubert                                             [[int i = PTHREAD_PRIO_INHERIT;
468*335c7cdaSCy Schubert                                               return i;]])],
46917d15b25SDag-Erling Smørgrav                            [ax_cv_PTHREAD_PRIO_INHERIT=yes],
47017d15b25SDag-Erling Smørgrav                            [ax_cv_PTHREAD_PRIO_INHERIT=no])
47117d15b25SDag-Erling Smørgrav            ])
472*335c7cdaSCy Schubert        AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes" && \
473*335c7cdaSCy Schubert               test "x$ax_pthread_prio_inherit_defined" != "xyes"],
474*335c7cdaSCy Schubert              [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])
475*335c7cdaSCy Schubert               ax_pthread_prio_inherit_defined=yes
476*335c7cdaSCy Schubert              ])
47717d15b25SDag-Erling Smørgrav
478*335c7cdaSCy Schubert        CFLAGS="$ax_pthread_save_CFLAGS"
479*335c7cdaSCy Schubert        LIBS="$ax_pthread_save_LIBS"
48017d15b25SDag-Erling Smørgrav
48117d15b25SDag-Erling Smørgrav        # More AIX lossage: compile with *_r variant
482*335c7cdaSCy Schubert        if test "x$GCC" != "xyes"; then
48317d15b25SDag-Erling Smørgrav            case $host_os in
48417d15b25SDag-Erling Smørgrav                aix*)
48517d15b25SDag-Erling Smørgrav                AS_CASE(["x/$CC"],
48617d15b25SDag-Erling Smørgrav                    [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6],
48717d15b25SDag-Erling Smørgrav                    [#handle absolute path differently from PATH based program lookup
48817d15b25SDag-Erling Smørgrav                     AS_CASE(["x$CC"],
48917d15b25SDag-Erling Smørgrav                         [x/*],
490*335c7cdaSCy Schubert                         [
491*335c7cdaSCy Schubert			   AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])
492*335c7cdaSCy Schubert			   AS_IF([test "x${CXX}" != "x"], [AS_IF([AS_EXECUTABLE_P([${CXX}_r])],[PTHREAD_CXX="${CXX}_r"])])
493*335c7cdaSCy Schubert			 ],
494*335c7cdaSCy Schubert                         [
495*335c7cdaSCy Schubert			   AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])
496*335c7cdaSCy Schubert			   AS_IF([test "x${CXX}" != "x"], [AC_CHECK_PROGS([PTHREAD_CXX],[${CXX}_r],[$CXX])])
497*335c7cdaSCy Schubert			 ]
498*335c7cdaSCy Schubert                     )
499*335c7cdaSCy Schubert                    ])
50017d15b25SDag-Erling Smørgrav                ;;
50117d15b25SDag-Erling Smørgrav            esac
50217d15b25SDag-Erling Smørgrav        fi
50317d15b25SDag-Erling Smørgravfi
50417d15b25SDag-Erling Smørgrav
50517d15b25SDag-Erling Smørgravtest -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
506*335c7cdaSCy Schuberttest -n "$PTHREAD_CXX" || PTHREAD_CXX="$CXX"
50717d15b25SDag-Erling Smørgrav
508f61ef7f6SDag-Erling SmørgravAC_SUBST([PTHREAD_LIBS])
509f61ef7f6SDag-Erling SmørgravAC_SUBST([PTHREAD_CFLAGS])
510f61ef7f6SDag-Erling SmørgravAC_SUBST([PTHREAD_CC])
511*335c7cdaSCy SchubertAC_SUBST([PTHREAD_CXX])
51217d15b25SDag-Erling Smørgrav
51317d15b25SDag-Erling Smørgrav# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
514*335c7cdaSCy Schubertif test "x$ax_pthread_ok" = "xyes"; then
515f61ef7f6SDag-Erling Smørgrav        ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1])
51617d15b25SDag-Erling Smørgrav        :
51717d15b25SDag-Erling Smørgravelse
51817d15b25SDag-Erling Smørgrav        ax_pthread_ok=no
51917d15b25SDag-Erling Smørgrav        $2
52017d15b25SDag-Erling Smørgravfi
52117d15b25SDag-Erling SmørgravAC_LANG_POP
52217d15b25SDag-Erling Smørgrav])dnl AX_PTHREAD
523