1dnl From http://ac-archive.sourceforge.net/ac-archive/acx_pthread.html
2
3AC_DEFUN([ACX_PTHREAD], [
4AC_REQUIRE([AC_CANONICAL_HOST])
5AC_LANG_SAVE
6AC_LANG_C
7acx_pthread_ok=no
8
9# We used to check for pthread.h first, but this fails if pthread.h
10# requires special compiler flags (e.g. on True64 or Sequent).
11# It gets checked for in the link test anyway.
12
13# First of all, check if the user has set any of the PTHREAD_LIBS,
14# etcetera environment variables, and if threads linking works using
15# them:
16if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
17        save_CFLAGS="$CFLAGS"
18        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
19        save_LIBS="$LIBS"
20        LIBS="$PTHREAD_LIBS $LIBS"
21        AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
22        AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
23        AC_MSG_RESULT($acx_pthread_ok)
24        if test x"$acx_pthread_ok" = xno; then
25                PTHREAD_LIBS=""
26                PTHREAD_CFLAGS=""
27        fi
28        LIBS="$save_LIBS"
29        CFLAGS="$save_CFLAGS"
30fi
31
32# We must check for the threads library under a number of different
33# names; the ordering is very important because some systems
34# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
35# libraries is broken (non-POSIX).
36
37# Create a list of thread flags to try.  Items starting with a "-" are
38# C compiler flags, and other items are library names, except for "none"
39# which indicates that we try without any flags at all, and "pthread-config"
40# which is a program returning the flags for the Pth emulation library.
41
42acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mt -mthreads pthread --thread-safe pthread-config pthreadGC2"
43
44# The ordering *is* (sometimes) important.  Some notes on the
45# individual items follow:
46
47# pthreads: AIX (must check this before -lpthread)
48# none: in case threads are in libc; should be tried before -Kthread and
49#       other compiler flags to prevent continual compiler warnings
50# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
51# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
52# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
53# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
54# -pthreads: Solaris/gcc
55# -mthreads: Mingw32/gcc, Lynx/gcc
56# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
57#      doesn't hurt to check since this sometimes defines pthreads too;
58#      also defines -D_REENTRANT)
59#      ... -mt is also the pthreads flag for HP/aCC
60# pthread: Linux, etcetera
61# --thread-safe: KAI C++
62# pthread-config: use pthread-config program (for GNU Pth library)
63
64case "${host_cpu}-${host_os}" in
65        *solaris*)
66
67        # On Solaris (at least, for some versions), libc contains stubbed
68        # (non-functional) versions of the pthreads routines, so link-based
69        # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
70        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
71        # a function called by this macro, so we could check for that, but
72        # who knows whether they'll stub that too in a future libc.)  So,
73        # we'll just look for -pthreads and -lpthread first:
74
75        acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
76        ;;
77esac
78
79if test x"$acx_pthread_ok" = xno; then
80for flag in $acx_pthread_flags; do
81
82        case $flag in
83                none)
84                AC_MSG_CHECKING([whether pthreads work without any flags])
85                ;;
86
87                -*)
88                AC_MSG_CHECKING([whether pthreads work with $flag])
89                PTHREAD_CFLAGS="$flag"
90                ;;
91
92		pthread-config)
93		AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
94		if test x"$acx_pthread_config" = xno; then continue; fi
95		PTHREAD_CFLAGS="`pthread-config --cflags`"
96		PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
97		;;
98
99                *)
100                AC_MSG_CHECKING([for the pthreads library -l$flag])
101                PTHREAD_LIBS="-l$flag"
102                ;;
103        esac
104
105        save_LIBS="$LIBS"
106        save_CFLAGS="$CFLAGS"
107        LIBS="$PTHREAD_LIBS $LIBS"
108        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
109
110        # Check for various functions.  We must include pthread.h,
111        # since some functions may be macros.  (On the Sequent, we
112        # need a special flag -Kthread to make this header compile.)
113        # We check for pthread_join because it is in -lpthread on IRIX
114        # while pthread_create is in libc.  We check for pthread_attr_init
115        # due to DEC craziness with -lpthreads.  We check for
116        # pthread_cleanup_push because it is one of the few pthread
117        # functions on Solaris that doesn't have a non-functional libc stub.
118        # We try pthread_create on general principles.
119        AC_TRY_LINK([#include <pthread.h>],
120                    [pthread_t th; pthread_join(th, 0);
121                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
122                     pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
123                    [acx_pthread_ok=yes])
124
125        LIBS="$save_LIBS"
126        CFLAGS="$save_CFLAGS"
127
128        AC_MSG_RESULT($acx_pthread_ok)
129        if test "x$acx_pthread_ok" = xyes; then
130                break;
131        fi
132
133        PTHREAD_LIBS=""
134        PTHREAD_CFLAGS=""
135done
136fi
137
138# Various other checks:
139if test "x$acx_pthread_ok" = xyes; then
140        save_LIBS="$LIBS"
141        LIBS="$PTHREAD_LIBS $LIBS"
142        save_CFLAGS="$CFLAGS"
143        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
144
145        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
146	AC_MSG_CHECKING([for joinable pthread attribute])
147	attr_name=unknown
148	for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
149	    AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
150                        [attr_name=$attr; break])
151	done
152        AC_MSG_RESULT($attr_name)
153        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
154            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
155                               [Define to necessary symbol if this constant
156                                uses a non-standard name on your system.])
157        fi
158
159        AC_MSG_CHECKING([if more special flags are required for pthreads])
160        flag=no
161        case "${host_cpu}-${host_os}" in
162            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
163            *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
164        esac
165        AC_MSG_RESULT(${flag})
166        if test "x$flag" != xno; then
167            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
168        fi
169
170        LIBS="$save_LIBS"
171        CFLAGS="$save_CFLAGS"
172
173        # More AIX lossage: must compile with xlc_r or cc_r
174	if test x"$GCC" != xyes; then
175          AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
176        else
177          PTHREAD_CC=$CC
178	fi
179else
180        PTHREAD_CC="$CC"
181fi
182
183AC_SUBST(PTHREAD_LIBS)
184AC_SUBST(PTHREAD_CFLAGS)
185AC_SUBST(PTHREAD_CC)
186
187# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
188if test x"$acx_pthread_ok" = xyes; then
189        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
190        :
191else
192        acx_pthread_ok=no
193        $2
194fi
195AC_LANG_RESTORE
196])dnl ACX_PTHREAD
197
198m4_ifdef([AC_PROG_MKDIR_P], [
199  dnl For automake-1.9.6 && autoconf < 2.62: Ensure MKDIR_P is AC_SUBSTed.
200  m4_define([AC_PROG_MKDIR_P],
201    m4_defn([AC_PROG_MKDIR_P])[
202    AC_SUBST([MKDIR_P])])], [
203  dnl For autoconf < 2.60: Backport of AC_PROG_MKDIR_P.
204  AC_DEFUN([AC_PROG_MKDIR_P],
205    [AC_REQUIRE([AM_PROG_MKDIR_P])dnl defined by automake
206     MKDIR_P='$(mkdir_p)'
207     AC_SUBST([MKDIR_P])])])
208