1 AC_DEFUN([ACX_PTHREAD], [
2 AC_REQUIRE([AC_CANONICAL_HOST])
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.
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
14 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
16 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
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
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).
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.
40 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
42 # The ordering *is* (sometimes) important. Some notes on the
43 # individual items follow:
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)
61 case "${host_cpu}-${host_os}" in
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:
72 acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
76 if test x"$acx_pthread_ok" = xno; then
77 for flag in $acx_pthread_flags; do
81 AC_MSG_CHECKING([whether pthreads work without any flags])
85 AC_MSG_CHECKING([whether pthreads work with $flag])
86 PTHREAD_CFLAGS="$flag"
90 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
91 if test x"$acx_pthread_config" = xno; then continue; fi
92 PTHREAD_CFLAGS="`pthread-config --cflags`"
93 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
97 AC_MSG_CHECKING([for the pthreads library -l$flag])
98 PTHREAD_LIBS="-l$flag"
103 save_CFLAGS="$CFLAGS"
104 LIBS="$PTHREAD_LIBS $LIBS"
105 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
107 # Check for various functions. We must include pthread.h,
108 # since some functions may be macros. (On the Sequent, we
109 # need a special flag -Kthread to make this header compile.)
110 # We check for pthread_join because it is in -lpthread on IRIX
111 # while pthread_create is in libc. We check for pthread_attr_init
112 # due to DEC craziness with -lpthreads. We check for
113 # pthread_cleanup_push because it is one of the few pthread
114 # functions on Solaris that doesn't have a non-functional libc stub.
115 # We try pthread_create on general principles.
116 AC_TRY_LINK([#include <pthread.h>],
117 [pthread_t th; pthread_join(th, 0);
118 pthread_attr_init(0); pthread_cleanup_push(0, 0);
119 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
120 [acx_pthread_ok=yes])
123 CFLAGS="$save_CFLAGS"
125 AC_MSG_RESULT($acx_pthread_ok)
126 if test "x$acx_pthread_ok" = xyes; then
135 # Various other checks:
136 if test "x$acx_pthread_ok" = xyes; then
138 LIBS="$PTHREAD_LIBS $LIBS"
139 save_CFLAGS="$CFLAGS"
140 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
142 # Detect AIX lossage: threads are created detached by default
143 # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
144 AC_MSG_CHECKING([for joinable pthread attribute])
145 AC_TRY_LINK([#include <pthread.h>],
146 [int attr=PTHREAD_CREATE_JOINABLE;],
147 ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
148 if test x"$ok" = xunknown; then
149 AC_TRY_LINK([#include <pthread.h>],
150 [int attr=PTHREAD_CREATE_UNDETACHED;],
151 ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
153 if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
154 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
155 [Define to the necessary symbol if this constant
156 uses a non-standard name on your system.])
159 if test x"$ok" = xunknown; then
160 AC_MSG_WARN([we do not know how to create joinable pthreads])
163 AC_MSG_CHECKING([if more special flags are required for pthreads])
165 case "${host_cpu}-${host_os}" in
166 *-aix* | *-freebsd*) flag="-D_THREAD_SAFE";;
167 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
169 AC_MSG_RESULT(${flag})
170 if test "x$flag" != xno; then
171 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
175 CFLAGS="$save_CFLAGS"
177 # More AIX lossage: must compile with cc_r
178 AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
183 AC_SUBST(PTHREAD_LIBS)
184 AC_SUBST(PTHREAD_CFLAGS)
187 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
188 if 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])