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