1290001Sglebius##### http://autoconf-archive.cryp.to/acx_pthread.html
2290001Sglebius#
3290001Sglebius# SYNOPSIS
4290001Sglebius#
5290001Sglebius#   ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
6290001Sglebius#
7290001Sglebius# DESCRIPTION
8290001Sglebius#
9290001Sglebius#   This macro figures out how to build C programs using POSIX threads.
10290001Sglebius#   It sets the PTHREAD_LIBS output variable to the threads library and
11290001Sglebius#   linker flags, and the PTHREAD_CFLAGS output variable to any special
12290001Sglebius#   C compiler flags that are needed. (The user can also force certain
13290001Sglebius#   compiler flags/libs to be tested by setting these environment
14290001Sglebius#   variables.)
15290001Sglebius#
16290001Sglebius#   Also sets PTHREAD_CC to any special C compiler that is needed for
17290001Sglebius#   multi-threaded programs (defaults to the value of CC otherwise).
18290001Sglebius#   (This is necessary on AIX to use the special cc_r compiler alias.)
19290001Sglebius#
20290001Sglebius#   NOTE: You are assumed to not only compile your program with these
21290001Sglebius#   flags, but also link it with them as well. e.g. you should link
22290001Sglebius#   with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
23290001Sglebius#   $LIBS
24290001Sglebius#
25290001Sglebius#   If you are only building threads programs, you may wish to use
26290001Sglebius#   these variables in your default LIBS, CFLAGS, and CC:
27290001Sglebius#
28290001Sglebius#          LIBS="$PTHREAD_LIBS $LIBS"
29290001Sglebius#          CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
30290001Sglebius#          CC="$PTHREAD_CC"
31290001Sglebius#
32290001Sglebius#   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
33290001Sglebius#   constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
34290001Sglebius#   that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
35290001Sglebius#
36290001Sglebius#   ACTION-IF-FOUND is a list of shell commands to run if a threads
37290001Sglebius#   library is found, and ACTION-IF-NOT-FOUND is a list of commands to
38290001Sglebius#   run it if it is not found. If ACTION-IF-FOUND is not specified, the
39290001Sglebius#   default action will define HAVE_PTHREAD.
40290001Sglebius#
41290001Sglebius#   Please let the authors know if this macro fails on any platform, or
42290001Sglebius#   if you have any other suggestions or comments. This macro was based
43290001Sglebius#   on work by SGJ on autoconf scripts for FFTW (http://www.fftw.org/)
44290001Sglebius#   (with help from M. Frigo), as well as ac_pthread and hb_pthread
45290001Sglebius#   macros posted by Alejandro Forero Cuervo to the autoconf macro
46290001Sglebius#   repository. We are also grateful for the helpful feedback of
47290001Sglebius#   numerous users.
48290001Sglebius#
49290001Sglebius# LAST MODIFICATION
50290001Sglebius#
51290001Sglebius#   2007-07-29
52290001Sglebius#
53290001Sglebius# COPYLEFT
54290001Sglebius#
55290001Sglebius#   Copyright (c) 2007 Steven G. Johnson <stevenj@alum.mit.edu>
56290001Sglebius#
57290001Sglebius#   This program is free software: you can redistribute it and/or
58290001Sglebius#   modify it under the terms of the GNU General Public License as
59290001Sglebius#   published by the Free Software Foundation, either version 3 of the
60290001Sglebius#   License, or (at your option) any later version.
61290001Sglebius#
62290001Sglebius#   This program is distributed in the hope that it will be useful, but
63290001Sglebius#   WITHOUT ANY WARRANTY; without even the implied warranty of
64290001Sglebius#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
65290001Sglebius#   General Public License for more details.
66290001Sglebius#
67290001Sglebius#   You should have received a copy of the GNU General Public License
68290001Sglebius#   along with this program. If not, see
69290001Sglebius#   <http://www.gnu.org/licenses/>.
70290001Sglebius#
71290001Sglebius#   As a special exception, the respective Autoconf Macro's copyright
72290001Sglebius#   owner gives unlimited permission to copy, distribute and modify the
73290001Sglebius#   configure scripts that are the output of Autoconf when processing
74290001Sglebius#   the Macro. You need not follow the terms of the GNU General Public
75290001Sglebius#   License when using or distributing such scripts, even though
76290001Sglebius#   portions of the text of the Macro appear in them. The GNU General
77290001Sglebius#   Public License (GPL) does govern all other use of the material that
78290001Sglebius#   constitutes the Autoconf Macro.
79290001Sglebius#
80290001Sglebius#   This special exception to the GPL applies to versions of the
81290001Sglebius#   Autoconf Macro released by the Autoconf Macro Archive. When you
82290001Sglebius#   make and distribute a modified version of the Autoconf Macro, you
83290001Sglebius#   may extend this special exception to the GPL to apply to your
84290001Sglebius#   modified version as well.
85290001Sglebius
86290001SglebiusAC_DEFUN([ACX_PTHREAD], [
87290001SglebiusAC_REQUIRE([AC_CANONICAL_HOST])
88290001SglebiusAC_LANG_SAVE
89290001SglebiusAC_LANG_C
90290001Sglebiusacx_pthread_ok=no
91290001Sglebius
92290001Sglebius# We used to check for pthread.h first, but this fails if pthread.h
93290001Sglebius# requires special compiler flags (e.g. on True64 or Sequent).
94290001Sglebius# It gets checked for in the link test anyway.
95290001Sglebius
96290001Sglebius# First of all, check if the user has set any of the PTHREAD_LIBS,
97290001Sglebius# etcetera environment variables, and if threads linking works using
98290001Sglebius# them:
99290001Sglebiusif test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
100290001Sglebius        save_CFLAGS="$CFLAGS"
101290001Sglebius        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
102290001Sglebius        save_LIBS="$LIBS"
103290001Sglebius        LIBS="$PTHREAD_LIBS $LIBS"
104290001Sglebius        AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
105290001Sglebius        AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
106290001Sglebius        AC_MSG_RESULT($acx_pthread_ok)
107290001Sglebius        if test x"$acx_pthread_ok" = xno; then
108290001Sglebius                PTHREAD_LIBS=""
109290001Sglebius                PTHREAD_CFLAGS=""
110290001Sglebius        fi
111290001Sglebius        LIBS="$save_LIBS"
112290001Sglebius        CFLAGS="$save_CFLAGS"
113290001Sglebiusfi
114290001Sglebius
115290001Sglebius# We must check for the threads library under a number of different
116290001Sglebius# names; the ordering is very important because some systems
117290001Sglebius# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
118290001Sglebius# libraries is broken (non-POSIX).
119290001Sglebius
120290001Sglebius# Create a list of thread flags to try.  Items starting with a "-" are
121290001Sglebius# C compiler flags, and other items are library names, except for "none"
122290001Sglebius# which indicates that we try without any flags at all, and "pthread-config"
123290001Sglebius# which is a program returning the flags for the Pth emulation library.
124290001Sglebius
125290001Sglebiusacx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
126290001Sglebius
127290001Sglebius# The ordering *is* (sometimes) important.  Some notes on the
128290001Sglebius# individual items follow:
129290001Sglebius
130290001Sglebius# pthreads: AIX (must check this before -lpthread)
131290001Sglebius# none: in case threads are in libc; should be tried before -Kthread and
132290001Sglebius#       other compiler flags to prevent continual compiler warnings
133290001Sglebius# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
134290001Sglebius# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
135290001Sglebius# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
136290001Sglebius# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
137290001Sglebius# -pthreads: Solaris/gcc
138290001Sglebius# -mthreads: Mingw32/gcc, Lynx/gcc
139290001Sglebius# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
140290001Sglebius#      doesn't hurt to check since this sometimes defines pthreads too;
141290001Sglebius#      also defines -D_REENTRANT)
142290001Sglebius#      ... -mt is also the pthreads flag for HP/aCC
143290001Sglebius# pthread: Linux, etcetera
144290001Sglebius# --thread-safe: KAI C++
145290001Sglebius# pthread-config: use pthread-config program (for GNU Pth library)
146290001Sglebius
147290001Sglebiuscase "${host_cpu}-${host_os}" in
148290001Sglebius        *solaris*)
149290001Sglebius
150290001Sglebius        # On Solaris (at least, for some versions), libc contains stubbed
151290001Sglebius        # (non-functional) versions of the pthreads routines, so link-based
152290001Sglebius        # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
153290001Sglebius        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
154290001Sglebius        # a function called by this macro, so we could check for that, but
155290001Sglebius        # who knows whether they'll stub that too in a future libc.)  So,
156290001Sglebius        # we'll just look for -pthreads and -lpthread first:
157290001Sglebius
158290001Sglebius        acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
159290001Sglebius        ;;
160290001Sglebiusesac
161290001Sglebius
162290001Sglebiusif test x"$acx_pthread_ok" = xno; then
163290001Sglebiusfor flag in $acx_pthread_flags; do
164290001Sglebius
165290001Sglebius        case $flag in
166290001Sglebius                none)
167290001Sglebius                AC_MSG_CHECKING([whether pthreads work without any flags])
168290001Sglebius                ;;
169290001Sglebius
170290001Sglebius                -*)
171290001Sglebius                AC_MSG_CHECKING([whether pthreads work with $flag])
172290001Sglebius                PTHREAD_CFLAGS="$flag"
173290001Sglebius                ;;
174290001Sglebius
175290001Sglebius		pthread-config)
176290001Sglebius		AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
177290001Sglebius		if test x"$acx_pthread_config" = xno; then continue; fi
178290001Sglebius		PTHREAD_CFLAGS="`pthread-config --cflags`"
179290001Sglebius		PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
180290001Sglebius		;;
181290001Sglebius
182290001Sglebius                *)
183290001Sglebius                AC_MSG_CHECKING([for the pthreads library -l$flag])
184290001Sglebius                PTHREAD_LIBS="-l$flag"
185290001Sglebius                ;;
186290001Sglebius        esac
187290001Sglebius
188290001Sglebius        save_LIBS="$LIBS"
189290001Sglebius        save_CFLAGS="$CFLAGS"
190290001Sglebius        LIBS="$PTHREAD_LIBS $LIBS"
191290001Sglebius        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
192290001Sglebius
193290001Sglebius        # Check for various functions.  We must include pthread.h,
194290001Sglebius        # since some functions may be macros.  (On the Sequent, we
195290001Sglebius        # need a special flag -Kthread to make this header compile.)
196290001Sglebius        # We check for pthread_join because it is in -lpthread on IRIX
197290001Sglebius        # while pthread_create is in libc.  We check for pthread_attr_init
198290001Sglebius        # due to DEC craziness with -lpthreads.  We check for
199290001Sglebius        # pthread_cleanup_push because it is one of the few pthread
200290001Sglebius        # functions on Solaris that doesn't have a non-functional libc stub.
201290001Sglebius        # We try pthread_create on general principles.
202290001Sglebius        AC_TRY_LINK([#include <pthread.h>],
203290001Sglebius                    [pthread_t th; pthread_join(th, 0);
204290001Sglebius                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
205290001Sglebius                     pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
206290001Sglebius                    [acx_pthread_ok=yes])
207290001Sglebius
208290001Sglebius        LIBS="$save_LIBS"
209290001Sglebius        CFLAGS="$save_CFLAGS"
210290001Sglebius
211290001Sglebius        AC_MSG_RESULT($acx_pthread_ok)
212290001Sglebius        if test "x$acx_pthread_ok" = xyes; then
213290001Sglebius                break;
214290001Sglebius        fi
215290001Sglebius
216290001Sglebius        PTHREAD_LIBS=""
217290001Sglebius        PTHREAD_CFLAGS=""
218290001Sglebiusdone
219290001Sglebiusfi
220290001Sglebius
221290001Sglebius# Various other checks:
222290001Sglebiusif test "x$acx_pthread_ok" = xyes; then
223290001Sglebius        save_LIBS="$LIBS"
224290001Sglebius        LIBS="$PTHREAD_LIBS $LIBS"
225290001Sglebius        save_CFLAGS="$CFLAGS"
226290001Sglebius        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
227290001Sglebius
228290001Sglebius        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
229290001Sglebius	AC_MSG_CHECKING([for joinable pthread attribute])
230290001Sglebius	attr_name=unknown
231290001Sglebius	for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
232290001Sglebius	    AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
233290001Sglebius                        [attr_name=$attr; break])
234290001Sglebius	done
235290001Sglebius        AC_MSG_RESULT($attr_name)
236290001Sglebius        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
237290001Sglebius            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
238290001Sglebius                               [Define to necessary symbol if this constant
239290001Sglebius                                uses a non-standard name on your system.])
240290001Sglebius        fi
241290001Sglebius
242290001Sglebius        AC_MSG_CHECKING([if more special flags are required for pthreads])
243290001Sglebius        flag=no
244290001Sglebius        case "${host_cpu}-${host_os}" in
245290001Sglebius            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
246290001Sglebius            *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
247290001Sglebius        esac
248290001Sglebius        AC_MSG_RESULT(${flag})
249290001Sglebius        if test "x$flag" != xno; then
250290001Sglebius            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
251290001Sglebius        fi
252290001Sglebius
253290001Sglebius        LIBS="$save_LIBS"
254290001Sglebius        CFLAGS="$save_CFLAGS"
255290001Sglebius
256290001Sglebius        # More AIX lossage: must compile with xlc_r or cc_r
257290001Sglebius	if test x"$GCC" != xyes; then
258290001Sglebius          AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
259290001Sglebius        else
260290001Sglebius          PTHREAD_CC=$CC
261290001Sglebius	fi
262290001Sglebiuselse
263290001Sglebius        PTHREAD_CC="$CC"
264290001Sglebiusfi
265290001Sglebius
266290001SglebiusAC_SUBST(PTHREAD_LIBS)
267290001SglebiusAC_SUBST(PTHREAD_CFLAGS)
268290001SglebiusAC_SUBST(PTHREAD_CC)
269290001Sglebius
270290001Sglebius# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
271290001Sglebiusif test x"$acx_pthread_ok" = xyes; then
272290001Sglebius        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
273290001Sglebius        :
274290001Sglebiuselse
275290001Sglebius        acx_pthread_ok=no
276290001Sglebius        $2
277290001Sglebiusfi
278290001SglebiusAC_LANG_RESTORE
279290001Sglebius])dnl ACX_PTHREAD
280