xref: /freebsd/sys/contrib/libsodium/m4/ax_pthread.m4 (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
1*0ac341f1SConrad Meyer# ===========================================================================
2*0ac341f1SConrad Meyer#        https://www.gnu.org/software/autoconf-archive/ax_pthread.html
3*0ac341f1SConrad Meyer# ===========================================================================
4*0ac341f1SConrad Meyer#
5*0ac341f1SConrad Meyer# SYNOPSIS
6*0ac341f1SConrad Meyer#
7*0ac341f1SConrad Meyer#   AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
8*0ac341f1SConrad Meyer#
9*0ac341f1SConrad Meyer# DESCRIPTION
10*0ac341f1SConrad Meyer#
11*0ac341f1SConrad Meyer#   This macro figures out how to build C programs using POSIX threads. It
12*0ac341f1SConrad Meyer#   sets the PTHREAD_LIBS output variable to the threads library and linker
13*0ac341f1SConrad Meyer#   flags, and the PTHREAD_CFLAGS output variable to any special C compiler
14*0ac341f1SConrad Meyer#   flags that are needed. (The user can also force certain compiler
15*0ac341f1SConrad Meyer#   flags/libs to be tested by setting these environment variables.)
16*0ac341f1SConrad Meyer#
17*0ac341f1SConrad Meyer#   Also sets PTHREAD_CC to any special C compiler that is needed for
18*0ac341f1SConrad Meyer#   multi-threaded programs (defaults to the value of CC otherwise). (This
19*0ac341f1SConrad Meyer#   is necessary on AIX to use the special cc_r compiler alias.)
20*0ac341f1SConrad Meyer#
21*0ac341f1SConrad Meyer#   NOTE: You are assumed to not only compile your program with these flags,
22*0ac341f1SConrad Meyer#   but also to link with them as well. For example, you might link with
23*0ac341f1SConrad Meyer#   $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
24*0ac341f1SConrad Meyer#
25*0ac341f1SConrad Meyer#   If you are only building threaded programs, you may wish to use these
26*0ac341f1SConrad Meyer#   variables in your default LIBS, CFLAGS, and CC:
27*0ac341f1SConrad Meyer#
28*0ac341f1SConrad Meyer#     LIBS="$PTHREAD_LIBS $LIBS"
29*0ac341f1SConrad Meyer#     CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
30*0ac341f1SConrad Meyer#     CC="$PTHREAD_CC"
31*0ac341f1SConrad Meyer#
32*0ac341f1SConrad Meyer#   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
33*0ac341f1SConrad Meyer#   has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to
34*0ac341f1SConrad Meyer#   that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
35*0ac341f1SConrad Meyer#
36*0ac341f1SConrad Meyer#   Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
37*0ac341f1SConrad Meyer#   PTHREAD_PRIO_INHERIT symbol is defined when compiling with
38*0ac341f1SConrad Meyer#   PTHREAD_CFLAGS.
39*0ac341f1SConrad Meyer#
40*0ac341f1SConrad Meyer#   ACTION-IF-FOUND is a list of shell commands to run if a threads library
41*0ac341f1SConrad Meyer#   is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
42*0ac341f1SConrad Meyer#   is not found. If ACTION-IF-FOUND is not specified, the default action
43*0ac341f1SConrad Meyer#   will define HAVE_PTHREAD.
44*0ac341f1SConrad Meyer#
45*0ac341f1SConrad Meyer#   Please let the authors know if this macro fails on any platform, or if
46*0ac341f1SConrad Meyer#   you have any other suggestions or comments. This macro was based on work
47*0ac341f1SConrad Meyer#   by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
48*0ac341f1SConrad Meyer#   from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
49*0ac341f1SConrad Meyer#   Alejandro Forero Cuervo to the autoconf macro repository. We are also
50*0ac341f1SConrad Meyer#   grateful for the helpful feedback of numerous users.
51*0ac341f1SConrad Meyer#
52*0ac341f1SConrad Meyer#   Updated for Autoconf 2.68 by Daniel Richard G.
53*0ac341f1SConrad Meyer#
54*0ac341f1SConrad Meyer# LICENSE
55*0ac341f1SConrad Meyer#
56*0ac341f1SConrad Meyer#   Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
57*0ac341f1SConrad Meyer#   Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
58*0ac341f1SConrad Meyer#
59*0ac341f1SConrad Meyer#   This program is free software: you can redistribute it and/or modify it
60*0ac341f1SConrad Meyer#   under the terms of the GNU General Public License as published by the
61*0ac341f1SConrad Meyer#   Free Software Foundation, either version 3 of the License, or (at your
62*0ac341f1SConrad Meyer#   option) any later version.
63*0ac341f1SConrad Meyer#
64*0ac341f1SConrad Meyer#   This program is distributed in the hope that it will be useful, but
65*0ac341f1SConrad Meyer#   WITHOUT ANY WARRANTY; without even the implied warranty of
66*0ac341f1SConrad Meyer#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
67*0ac341f1SConrad Meyer#   Public License for more details.
68*0ac341f1SConrad Meyer#
69*0ac341f1SConrad Meyer#   You should have received a copy of the GNU General Public License along
70*0ac341f1SConrad Meyer#   with this program. If not, see <https://www.gnu.org/licenses/>.
71*0ac341f1SConrad Meyer#
72*0ac341f1SConrad Meyer#   As a special exception, the respective Autoconf Macro's copyright owner
73*0ac341f1SConrad Meyer#   gives unlimited permission to copy, distribute and modify the configure
74*0ac341f1SConrad Meyer#   scripts that are the output of Autoconf when processing the Macro. You
75*0ac341f1SConrad Meyer#   need not follow the terms of the GNU General Public License when using
76*0ac341f1SConrad Meyer#   or distributing such scripts, even though portions of the text of the
77*0ac341f1SConrad Meyer#   Macro appear in them. The GNU General Public License (GPL) does govern
78*0ac341f1SConrad Meyer#   all other use of the material that constitutes the Autoconf Macro.
79*0ac341f1SConrad Meyer#
80*0ac341f1SConrad Meyer#   This special exception to the GPL applies to versions of the Autoconf
81*0ac341f1SConrad Meyer#   Macro released by the Autoconf Archive. When you make and distribute a
82*0ac341f1SConrad Meyer#   modified version of the Autoconf Macro, you may extend this special
83*0ac341f1SConrad Meyer#   exception to the GPL to apply to your modified version as well.
84*0ac341f1SConrad Meyer
85*0ac341f1SConrad Meyer#serial 24
86*0ac341f1SConrad Meyer
87*0ac341f1SConrad MeyerAU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
88*0ac341f1SConrad MeyerAC_DEFUN([AX_PTHREAD], [
89*0ac341f1SConrad MeyerAC_REQUIRE([AC_CANONICAL_HOST])
90*0ac341f1SConrad MeyerAC_REQUIRE([AC_PROG_CC])
91*0ac341f1SConrad MeyerAC_REQUIRE([AC_PROG_SED])
92*0ac341f1SConrad MeyerAC_LANG_PUSH([C])
93*0ac341f1SConrad Meyerax_pthread_ok=no
94*0ac341f1SConrad Meyer
95*0ac341f1SConrad Meyer# We used to check for pthread.h first, but this fails if pthread.h
96*0ac341f1SConrad Meyer# requires special compiler flags (e.g. on Tru64 or Sequent).
97*0ac341f1SConrad Meyer# It gets checked for in the link test anyway.
98*0ac341f1SConrad Meyer
99*0ac341f1SConrad Meyer# First of all, check if the user has set any of the PTHREAD_LIBS,
100*0ac341f1SConrad Meyer# etcetera environment variables, and if threads linking works using
101*0ac341f1SConrad Meyer# them:
102*0ac341f1SConrad Meyerif test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then
103*0ac341f1SConrad Meyer        ax_pthread_save_CC="$CC"
104*0ac341f1SConrad Meyer        ax_pthread_save_CFLAGS="$CFLAGS"
105*0ac341f1SConrad Meyer        ax_pthread_save_LIBS="$LIBS"
106*0ac341f1SConrad Meyer        AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"])
107*0ac341f1SConrad Meyer        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
108*0ac341f1SConrad Meyer        LIBS="$PTHREAD_LIBS $LIBS"
109*0ac341f1SConrad Meyer        AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS])
110*0ac341f1SConrad Meyer        AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes])
111*0ac341f1SConrad Meyer        AC_MSG_RESULT([$ax_pthread_ok])
112*0ac341f1SConrad Meyer        if test "x$ax_pthread_ok" = "xno"; then
113*0ac341f1SConrad Meyer                PTHREAD_LIBS=""
114*0ac341f1SConrad Meyer                PTHREAD_CFLAGS=""
115*0ac341f1SConrad Meyer        fi
116*0ac341f1SConrad Meyer        CC="$ax_pthread_save_CC"
117*0ac341f1SConrad Meyer        CFLAGS="$ax_pthread_save_CFLAGS"
118*0ac341f1SConrad Meyer        LIBS="$ax_pthread_save_LIBS"
119*0ac341f1SConrad Meyerfi
120*0ac341f1SConrad Meyer
121*0ac341f1SConrad Meyer# We must check for the threads library under a number of different
122*0ac341f1SConrad Meyer# names; the ordering is very important because some systems
123*0ac341f1SConrad Meyer# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
124*0ac341f1SConrad Meyer# libraries is broken (non-POSIX).
125*0ac341f1SConrad Meyer
126*0ac341f1SConrad Meyer# Create a list of thread flags to try.  Items starting with a "-" are
127*0ac341f1SConrad Meyer# C compiler flags, and other items are library names, except for "none"
128*0ac341f1SConrad Meyer# which indicates that we try without any flags at all, and "pthread-config"
129*0ac341f1SConrad Meyer# which is a program returning the flags for the Pth emulation library.
130*0ac341f1SConrad Meyer
131*0ac341f1SConrad Meyerax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
132*0ac341f1SConrad Meyer
133*0ac341f1SConrad Meyer# The ordering *is* (sometimes) important.  Some notes on the
134*0ac341f1SConrad Meyer# individual items follow:
135*0ac341f1SConrad Meyer
136*0ac341f1SConrad Meyer# pthreads: AIX (must check this before -lpthread)
137*0ac341f1SConrad Meyer# none: in case threads are in libc; should be tried before -Kthread and
138*0ac341f1SConrad Meyer#       other compiler flags to prevent continual compiler warnings
139*0ac341f1SConrad Meyer# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
140*0ac341f1SConrad Meyer# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64
141*0ac341f1SConrad Meyer#           (Note: HP C rejects this with "bad form for `-t' option")
142*0ac341f1SConrad Meyer# -pthreads: Solaris/gcc (Note: HP C also rejects)
143*0ac341f1SConrad Meyer# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
144*0ac341f1SConrad Meyer#      doesn't hurt to check since this sometimes defines pthreads and
145*0ac341f1SConrad Meyer#      -D_REENTRANT too), HP C (must be checked before -lpthread, which
146*0ac341f1SConrad Meyer#      is present but should not be used directly; and before -mthreads,
147*0ac341f1SConrad Meyer#      because the compiler interprets this as "-mt" + "-hreads")
148*0ac341f1SConrad Meyer# -mthreads: Mingw32/gcc, Lynx/gcc
149*0ac341f1SConrad Meyer# pthread: Linux, etcetera
150*0ac341f1SConrad Meyer# --thread-safe: KAI C++
151*0ac341f1SConrad Meyer# pthread-config: use pthread-config program (for GNU Pth library)
152*0ac341f1SConrad Meyer
153*0ac341f1SConrad Meyercase $host_os in
154*0ac341f1SConrad Meyer
155*0ac341f1SConrad Meyer        freebsd*)
156*0ac341f1SConrad Meyer
157*0ac341f1SConrad Meyer        # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
158*0ac341f1SConrad Meyer        # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
159*0ac341f1SConrad Meyer
160*0ac341f1SConrad Meyer        ax_pthread_flags="-kthread lthread $ax_pthread_flags"
161*0ac341f1SConrad Meyer        ;;
162*0ac341f1SConrad Meyer
163*0ac341f1SConrad Meyer        hpux*)
164*0ac341f1SConrad Meyer
165*0ac341f1SConrad Meyer        # From the cc(1) man page: "[-mt] Sets various -D flags to enable
166*0ac341f1SConrad Meyer        # multi-threading and also sets -lpthread."
167*0ac341f1SConrad Meyer
168*0ac341f1SConrad Meyer        ax_pthread_flags="-mt -pthread pthread $ax_pthread_flags"
169*0ac341f1SConrad Meyer        ;;
170*0ac341f1SConrad Meyer
171*0ac341f1SConrad Meyer        openedition*)
172*0ac341f1SConrad Meyer
173*0ac341f1SConrad Meyer        # IBM z/OS requires a feature-test macro to be defined in order to
174*0ac341f1SConrad Meyer        # enable POSIX threads at all, so give the user a hint if this is
175*0ac341f1SConrad Meyer        # not set. (We don't define these ourselves, as they can affect
176*0ac341f1SConrad Meyer        # other portions of the system API in unpredictable ways.)
177*0ac341f1SConrad Meyer
178*0ac341f1SConrad Meyer        AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING],
179*0ac341f1SConrad Meyer            [
180*0ac341f1SConrad Meyer#            if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS)
181*0ac341f1SConrad Meyer             AX_PTHREAD_ZOS_MISSING
182*0ac341f1SConrad Meyer#            endif
183*0ac341f1SConrad Meyer            ],
184*0ac341f1SConrad Meyer            [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])])
185*0ac341f1SConrad Meyer        ;;
186*0ac341f1SConrad Meyer
187*0ac341f1SConrad Meyer        solaris*)
188*0ac341f1SConrad Meyer
189*0ac341f1SConrad Meyer        # On Solaris (at least, for some versions), libc contains stubbed
190*0ac341f1SConrad Meyer        # (non-functional) versions of the pthreads routines, so link-based
191*0ac341f1SConrad Meyer        # tests will erroneously succeed. (N.B.: The stubs are missing
192*0ac341f1SConrad Meyer        # pthread_cleanup_push, or rather a function called by this macro,
193*0ac341f1SConrad Meyer        # so we could check for that, but who knows whether they'll stub
194*0ac341f1SConrad Meyer        # that too in a future libc.)  So we'll check first for the
195*0ac341f1SConrad Meyer        # standard Solaris way of linking pthreads (-mt -lpthread).
196*0ac341f1SConrad Meyer
197*0ac341f1SConrad Meyer        ax_pthread_flags="-mt,pthread pthread $ax_pthread_flags"
198*0ac341f1SConrad Meyer        ;;
199*0ac341f1SConrad Meyeresac
200*0ac341f1SConrad Meyer
201*0ac341f1SConrad Meyer# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC)
202*0ac341f1SConrad Meyer
203*0ac341f1SConrad MeyerAS_IF([test "x$GCC" = "xyes"],
204*0ac341f1SConrad Meyer      [ax_pthread_flags="-pthread -pthreads $ax_pthread_flags"])
205*0ac341f1SConrad Meyer
206*0ac341f1SConrad Meyer# The presence of a feature test macro requesting re-entrant function
207*0ac341f1SConrad Meyer# definitions is, on some systems, a strong hint that pthreads support is
208*0ac341f1SConrad Meyer# correctly enabled
209*0ac341f1SConrad Meyer
210*0ac341f1SConrad Meyercase $host_os in
211*0ac341f1SConrad Meyer        darwin* | hpux* | linux* | osf* | solaris*)
212*0ac341f1SConrad Meyer        ax_pthread_check_macro="_REENTRANT"
213*0ac341f1SConrad Meyer        ;;
214*0ac341f1SConrad Meyer
215*0ac341f1SConrad Meyer        aix*)
216*0ac341f1SConrad Meyer        ax_pthread_check_macro="_THREAD_SAFE"
217*0ac341f1SConrad Meyer        ;;
218*0ac341f1SConrad Meyer
219*0ac341f1SConrad Meyer        *)
220*0ac341f1SConrad Meyer        ax_pthread_check_macro="--"
221*0ac341f1SConrad Meyer        ;;
222*0ac341f1SConrad Meyeresac
223*0ac341f1SConrad MeyerAS_IF([test "x$ax_pthread_check_macro" = "x--"],
224*0ac341f1SConrad Meyer      [ax_pthread_check_cond=0],
225*0ac341f1SConrad Meyer      [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"])
226*0ac341f1SConrad Meyer
227*0ac341f1SConrad Meyer# Are we compiling with Clang?
228*0ac341f1SConrad Meyer
229*0ac341f1SConrad MeyerAC_CACHE_CHECK([whether $CC is Clang],
230*0ac341f1SConrad Meyer    [ax_cv_PTHREAD_CLANG],
231*0ac341f1SConrad Meyer    [ax_cv_PTHREAD_CLANG=no
232*0ac341f1SConrad Meyer     # Note that Autoconf sets GCC=yes for Clang as well as GCC
233*0ac341f1SConrad Meyer     if test "x$GCC" = "xyes"; then
234*0ac341f1SConrad Meyer        AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG],
235*0ac341f1SConrad Meyer            [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */
236*0ac341f1SConrad Meyer#            if defined(__clang__) && defined(__llvm__)
237*0ac341f1SConrad Meyer             AX_PTHREAD_CC_IS_CLANG
238*0ac341f1SConrad Meyer#            endif
239*0ac341f1SConrad Meyer            ],
240*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_CLANG=yes])
241*0ac341f1SConrad Meyer     fi
242*0ac341f1SConrad Meyer    ])
243*0ac341f1SConrad Meyerax_pthread_clang="$ax_cv_PTHREAD_CLANG"
244*0ac341f1SConrad Meyer
245*0ac341f1SConrad Meyerax_pthread_clang_warning=no
246*0ac341f1SConrad Meyer
247*0ac341f1SConrad Meyer# Clang needs special handling, because older versions handle the -pthread
248*0ac341f1SConrad Meyer# option in a rather... idiosyncratic way
249*0ac341f1SConrad Meyer
250*0ac341f1SConrad Meyerif test "x$ax_pthread_clang" = "xyes"; then
251*0ac341f1SConrad Meyer
252*0ac341f1SConrad Meyer        # Clang takes -pthread; it has never supported any other flag
253*0ac341f1SConrad Meyer
254*0ac341f1SConrad Meyer        # (Note 1: This will need to be revisited if a system that Clang
255*0ac341f1SConrad Meyer        # supports has POSIX threads in a separate library.  This tends not
256*0ac341f1SConrad Meyer        # to be the way of modern systems, but it's conceivable.)
257*0ac341f1SConrad Meyer
258*0ac341f1SConrad Meyer        # (Note 2: On some systems, notably Darwin, -pthread is not needed
259*0ac341f1SConrad Meyer        # to get POSIX threads support; the API is always present and
260*0ac341f1SConrad Meyer        # active.  We could reasonably leave PTHREAD_CFLAGS empty.  But
261*0ac341f1SConrad Meyer        # -pthread does define _REENTRANT, and while the Darwin headers
262*0ac341f1SConrad Meyer        # ignore this macro, third-party headers might not.)
263*0ac341f1SConrad Meyer
264*0ac341f1SConrad Meyer        PTHREAD_CFLAGS="-pthread"
265*0ac341f1SConrad Meyer        PTHREAD_LIBS=
266*0ac341f1SConrad Meyer
267*0ac341f1SConrad Meyer        ax_pthread_ok=yes
268*0ac341f1SConrad Meyer
269*0ac341f1SConrad Meyer        # However, older versions of Clang make a point of warning the user
270*0ac341f1SConrad Meyer        # that, in an invocation where only linking and no compilation is
271*0ac341f1SConrad Meyer        # taking place, the -pthread option has no effect ("argument unused
272*0ac341f1SConrad Meyer        # during compilation").  They expect -pthread to be passed in only
273*0ac341f1SConrad Meyer        # when source code is being compiled.
274*0ac341f1SConrad Meyer        #
275*0ac341f1SConrad Meyer        # Problem is, this is at odds with the way Automake and most other
276*0ac341f1SConrad Meyer        # C build frameworks function, which is that the same flags used in
277*0ac341f1SConrad Meyer        # compilation (CFLAGS) are also used in linking.  Many systems
278*0ac341f1SConrad Meyer        # supported by AX_PTHREAD require exactly this for POSIX threads
279*0ac341f1SConrad Meyer        # support, and in fact it is often not straightforward to specify a
280*0ac341f1SConrad Meyer        # flag that is used only in the compilation phase and not in
281*0ac341f1SConrad Meyer        # linking.  Such a scenario is extremely rare in practice.
282*0ac341f1SConrad Meyer        #
283*0ac341f1SConrad Meyer        # Even though use of the -pthread flag in linking would only print
284*0ac341f1SConrad Meyer        # a warning, this can be a nuisance for well-run software projects
285*0ac341f1SConrad Meyer        # that build with -Werror.  So if the active version of Clang has
286*0ac341f1SConrad Meyer        # this misfeature, we search for an option to squash it.
287*0ac341f1SConrad Meyer
288*0ac341f1SConrad Meyer        AC_CACHE_CHECK([whether Clang needs flag to prevent "argument unused" warning when linking with -pthread],
289*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG],
290*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown
291*0ac341f1SConrad Meyer             # Create an alternate version of $ac_link that compiles and
292*0ac341f1SConrad Meyer             # links in two steps (.c -> .o, .o -> exe) instead of one
293*0ac341f1SConrad Meyer             # (.c -> exe), because the warning occurs only in the second
294*0ac341f1SConrad Meyer             # step
295*0ac341f1SConrad Meyer             ax_pthread_save_ac_link="$ac_link"
296*0ac341f1SConrad Meyer             ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g'
297*0ac341f1SConrad Meyer             ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"`
298*0ac341f1SConrad Meyer             ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)"
299*0ac341f1SConrad Meyer             ax_pthread_save_CFLAGS="$CFLAGS"
300*0ac341f1SConrad Meyer             for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do
301*0ac341f1SConrad Meyer                AS_IF([test "x$ax_pthread_try" = "xunknown"], [break])
302*0ac341f1SConrad Meyer                CFLAGS="-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS"
303*0ac341f1SConrad Meyer                ac_link="$ax_pthread_save_ac_link"
304*0ac341f1SConrad Meyer                AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],
305*0ac341f1SConrad Meyer                    [ac_link="$ax_pthread_2step_ac_link"
306*0ac341f1SConrad Meyer                     AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],
307*0ac341f1SConrad Meyer                         [break])
308*0ac341f1SConrad Meyer                    ])
309*0ac341f1SConrad Meyer             done
310*0ac341f1SConrad Meyer             ac_link="$ax_pthread_save_ac_link"
311*0ac341f1SConrad Meyer             CFLAGS="$ax_pthread_save_CFLAGS"
312*0ac341f1SConrad Meyer             AS_IF([test "x$ax_pthread_try" = "x"], [ax_pthread_try=no])
313*0ac341f1SConrad Meyer             ax_cv_PTHREAD_CLANG_NO_WARN_FLAG="$ax_pthread_try"
314*0ac341f1SConrad Meyer            ])
315*0ac341f1SConrad Meyer
316*0ac341f1SConrad Meyer        case "$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG" in
317*0ac341f1SConrad Meyer                no | unknown) ;;
318*0ac341f1SConrad Meyer                *) PTHREAD_CFLAGS="$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS" ;;
319*0ac341f1SConrad Meyer        esac
320*0ac341f1SConrad Meyer
321*0ac341f1SConrad Meyerfi # $ax_pthread_clang = yes
322*0ac341f1SConrad Meyer
323*0ac341f1SConrad Meyerif test "x$ax_pthread_ok" = "xno"; then
324*0ac341f1SConrad Meyerfor ax_pthread_try_flag in $ax_pthread_flags; do
325*0ac341f1SConrad Meyer
326*0ac341f1SConrad Meyer        case $ax_pthread_try_flag in
327*0ac341f1SConrad Meyer                none)
328*0ac341f1SConrad Meyer                AC_MSG_CHECKING([whether pthreads work without any flags])
329*0ac341f1SConrad Meyer                ;;
330*0ac341f1SConrad Meyer
331*0ac341f1SConrad Meyer                -mt,pthread)
332*0ac341f1SConrad Meyer                AC_MSG_CHECKING([whether pthreads work with -mt -lpthread])
333*0ac341f1SConrad Meyer                PTHREAD_CFLAGS="-mt"
334*0ac341f1SConrad Meyer                PTHREAD_LIBS="-lpthread"
335*0ac341f1SConrad Meyer                ;;
336*0ac341f1SConrad Meyer
337*0ac341f1SConrad Meyer                -*)
338*0ac341f1SConrad Meyer                AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag])
339*0ac341f1SConrad Meyer                PTHREAD_CFLAGS="$ax_pthread_try_flag"
340*0ac341f1SConrad Meyer                ;;
341*0ac341f1SConrad Meyer
342*0ac341f1SConrad Meyer                pthread-config)
343*0ac341f1SConrad Meyer                AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])
344*0ac341f1SConrad Meyer                AS_IF([test "x$ax_pthread_config" = "xno"], [continue])
345*0ac341f1SConrad Meyer                PTHREAD_CFLAGS="`pthread-config --cflags`"
346*0ac341f1SConrad Meyer                PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
347*0ac341f1SConrad Meyer                ;;
348*0ac341f1SConrad Meyer
349*0ac341f1SConrad Meyer                *)
350*0ac341f1SConrad Meyer                AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag])
351*0ac341f1SConrad Meyer                PTHREAD_LIBS="-l$ax_pthread_try_flag"
352*0ac341f1SConrad Meyer                ;;
353*0ac341f1SConrad Meyer        esac
354*0ac341f1SConrad Meyer
355*0ac341f1SConrad Meyer        ax_pthread_save_CFLAGS="$CFLAGS"
356*0ac341f1SConrad Meyer        ax_pthread_save_LIBS="$LIBS"
357*0ac341f1SConrad Meyer        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
358*0ac341f1SConrad Meyer        LIBS="$PTHREAD_LIBS $LIBS"
359*0ac341f1SConrad Meyer
360*0ac341f1SConrad Meyer        # Check for various functions.  We must include pthread.h,
361*0ac341f1SConrad Meyer        # since some functions may be macros.  (On the Sequent, we
362*0ac341f1SConrad Meyer        # need a special flag -Kthread to make this header compile.)
363*0ac341f1SConrad Meyer        # We check for pthread_join because it is in -lpthread on IRIX
364*0ac341f1SConrad Meyer        # while pthread_create is in libc.  We check for pthread_attr_init
365*0ac341f1SConrad Meyer        # due to DEC craziness with -lpthreads.  We check for
366*0ac341f1SConrad Meyer        # pthread_cleanup_push because it is one of the few pthread
367*0ac341f1SConrad Meyer        # functions on Solaris that doesn't have a non-functional libc stub.
368*0ac341f1SConrad Meyer        # We try pthread_create on general principles.
369*0ac341f1SConrad Meyer
370*0ac341f1SConrad Meyer        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
371*0ac341f1SConrad Meyer#                       if $ax_pthread_check_cond
372*0ac341f1SConrad Meyer#                        error "$ax_pthread_check_macro must be defined"
373*0ac341f1SConrad Meyer#                       endif
374*0ac341f1SConrad Meyer                        static void routine(void *a) { a = 0; }
375*0ac341f1SConrad Meyer                        static void *start_routine(void *a) { return a; }],
376*0ac341f1SConrad Meyer                       [pthread_t th; pthread_attr_t attr;
377*0ac341f1SConrad Meyer                        pthread_create(&th, 0, start_routine, 0);
378*0ac341f1SConrad Meyer                        pthread_join(th, 0);
379*0ac341f1SConrad Meyer                        pthread_attr_init(&attr);
380*0ac341f1SConrad Meyer                        pthread_cleanup_push(routine, 0);
381*0ac341f1SConrad Meyer                        pthread_cleanup_pop(0) /* ; */])],
382*0ac341f1SConrad Meyer            [ax_pthread_ok=yes],
383*0ac341f1SConrad Meyer            [])
384*0ac341f1SConrad Meyer
385*0ac341f1SConrad Meyer        CFLAGS="$ax_pthread_save_CFLAGS"
386*0ac341f1SConrad Meyer        LIBS="$ax_pthread_save_LIBS"
387*0ac341f1SConrad Meyer
388*0ac341f1SConrad Meyer        AC_MSG_RESULT([$ax_pthread_ok])
389*0ac341f1SConrad Meyer        AS_IF([test "x$ax_pthread_ok" = "xyes"], [break])
390*0ac341f1SConrad Meyer
391*0ac341f1SConrad Meyer        PTHREAD_LIBS=""
392*0ac341f1SConrad Meyer        PTHREAD_CFLAGS=""
393*0ac341f1SConrad Meyerdone
394*0ac341f1SConrad Meyerfi
395*0ac341f1SConrad Meyer
396*0ac341f1SConrad Meyer# Various other checks:
397*0ac341f1SConrad Meyerif test "x$ax_pthread_ok" = "xyes"; then
398*0ac341f1SConrad Meyer        ax_pthread_save_CFLAGS="$CFLAGS"
399*0ac341f1SConrad Meyer        ax_pthread_save_LIBS="$LIBS"
400*0ac341f1SConrad Meyer        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
401*0ac341f1SConrad Meyer        LIBS="$PTHREAD_LIBS $LIBS"
402*0ac341f1SConrad Meyer
403*0ac341f1SConrad Meyer        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
404*0ac341f1SConrad Meyer        AC_CACHE_CHECK([for joinable pthread attribute],
405*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_JOINABLE_ATTR],
406*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_JOINABLE_ATTR=unknown
407*0ac341f1SConrad Meyer             for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
408*0ac341f1SConrad Meyer                 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
409*0ac341f1SConrad Meyer                                                 [int attr = $ax_pthread_attr; return attr /* ; */])],
410*0ac341f1SConrad Meyer                                [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break],
411*0ac341f1SConrad Meyer                                [])
412*0ac341f1SConrad Meyer             done
413*0ac341f1SConrad Meyer            ])
414*0ac341f1SConrad Meyer        AS_IF([test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xunknown" && \
415*0ac341f1SConrad Meyer               test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xPTHREAD_CREATE_JOINABLE" && \
416*0ac341f1SConrad Meyer               test "x$ax_pthread_joinable_attr_defined" != "xyes"],
417*0ac341f1SConrad Meyer              [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE],
418*0ac341f1SConrad Meyer                                  [$ax_cv_PTHREAD_JOINABLE_ATTR],
419*0ac341f1SConrad Meyer                                  [Define to necessary symbol if this constant
420*0ac341f1SConrad Meyer                                   uses a non-standard name on your system.])
421*0ac341f1SConrad Meyer               ax_pthread_joinable_attr_defined=yes
422*0ac341f1SConrad Meyer              ])
423*0ac341f1SConrad Meyer
424*0ac341f1SConrad Meyer        AC_CACHE_CHECK([whether more special flags are required for pthreads],
425*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_SPECIAL_FLAGS],
426*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_SPECIAL_FLAGS=no
427*0ac341f1SConrad Meyer             case $host_os in
428*0ac341f1SConrad Meyer             solaris*)
429*0ac341f1SConrad Meyer             ax_cv_PTHREAD_SPECIAL_FLAGS="-D_POSIX_PTHREAD_SEMANTICS"
430*0ac341f1SConrad Meyer             ;;
431*0ac341f1SConrad Meyer             esac
432*0ac341f1SConrad Meyer            ])
433*0ac341f1SConrad Meyer        AS_IF([test "x$ax_cv_PTHREAD_SPECIAL_FLAGS" != "xno" && \
434*0ac341f1SConrad Meyer               test "x$ax_pthread_special_flags_added" != "xyes"],
435*0ac341f1SConrad Meyer              [PTHREAD_CFLAGS="$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS"
436*0ac341f1SConrad Meyer               ax_pthread_special_flags_added=yes])
437*0ac341f1SConrad Meyer
438*0ac341f1SConrad Meyer        AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
439*0ac341f1SConrad Meyer            [ax_cv_PTHREAD_PRIO_INHERIT],
440*0ac341f1SConrad Meyer            [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],
441*0ac341f1SConrad Meyer                                             [[int i = PTHREAD_PRIO_INHERIT;]])],
442*0ac341f1SConrad Meyer                            [ax_cv_PTHREAD_PRIO_INHERIT=yes],
443*0ac341f1SConrad Meyer                            [ax_cv_PTHREAD_PRIO_INHERIT=no])
444*0ac341f1SConrad Meyer            ])
445*0ac341f1SConrad Meyer        AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes" && \
446*0ac341f1SConrad Meyer               test "x$ax_pthread_prio_inherit_defined" != "xyes"],
447*0ac341f1SConrad Meyer              [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])
448*0ac341f1SConrad Meyer               ax_pthread_prio_inherit_defined=yes
449*0ac341f1SConrad Meyer              ])
450*0ac341f1SConrad Meyer
451*0ac341f1SConrad Meyer        CFLAGS="$ax_pthread_save_CFLAGS"
452*0ac341f1SConrad Meyer        LIBS="$ax_pthread_save_LIBS"
453*0ac341f1SConrad Meyer
454*0ac341f1SConrad Meyer        # More AIX lossage: compile with *_r variant
455*0ac341f1SConrad Meyer        if test "x$GCC" != "xyes"; then
456*0ac341f1SConrad Meyer            case $host_os in
457*0ac341f1SConrad Meyer                aix*)
458*0ac341f1SConrad Meyer                AS_CASE(["x/$CC"],
459*0ac341f1SConrad Meyer                    [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6],
460*0ac341f1SConrad Meyer                    [#handle absolute path differently from PATH based program lookup
461*0ac341f1SConrad Meyer                     AS_CASE(["x$CC"],
462*0ac341f1SConrad Meyer                         [x/*],
463*0ac341f1SConrad Meyer                         [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])],
464*0ac341f1SConrad Meyer                         [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])])
465*0ac341f1SConrad Meyer                ;;
466*0ac341f1SConrad Meyer            esac
467*0ac341f1SConrad Meyer        fi
468*0ac341f1SConrad Meyerfi
469*0ac341f1SConrad Meyer
470*0ac341f1SConrad Meyertest -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
471*0ac341f1SConrad Meyer
472*0ac341f1SConrad MeyerAC_SUBST([PTHREAD_LIBS])
473*0ac341f1SConrad MeyerAC_SUBST([PTHREAD_CFLAGS])
474*0ac341f1SConrad MeyerAC_SUBST([PTHREAD_CC])
475*0ac341f1SConrad Meyer
476*0ac341f1SConrad Meyer# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
477*0ac341f1SConrad Meyerif test "x$ax_pthread_ok" = "xyes"; then
478*0ac341f1SConrad Meyer        ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1])
479*0ac341f1SConrad Meyer        :
480*0ac341f1SConrad Meyerelse
481*0ac341f1SConrad Meyer        ax_pthread_ok=no
482*0ac341f1SConrad Meyer        $2
483*0ac341f1SConrad Meyerfi
484*0ac341f1SConrad MeyerAC_LANG_POP
485*0ac341f1SConrad Meyer])dnl AX_PTHREAD
486