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