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