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