1 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
3 dnl This macro figures out how to build C programs using POSIX
4 dnl threads. It sets the PTHREAD_LIBS output variable to the threads
5 dnl library and linker flags, and the PTHREAD_CFLAGS output variable
6 dnl to any special C compiler flags that are needed. (The user can also
7 dnl force certain compiler flags/libs to be tested by setting these
8 dnl environment variables.)
10 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
11 dnl multi-threaded programs (defaults to the value of CC otherwise).
12 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
14 dnl If you are only building threads programs, you may wish to
15 dnl use these variables in your default LIBS, CFLAGS, and CC:
17 dnl LIBS="$PTHREAD_LIBS $LIBS"
18 dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
21 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
22 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
23 dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
25 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
26 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
27 dnl to run it if it is not found. If ACTION-IF-FOUND is not specified,
28 dnl the default action will define HAVE_PTHREAD.
30 dnl Please let the authors know if this macro fails on any platform,
31 dnl or if you have any other suggestions or comments. This macro was
32 dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
33 dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
34 dnl macros posted by AFC to the autoconf macro repository. We are also
35 dnl grateful for the helpful feedback of numerous users.
37 dnl @version $Id: acx_pthread.m4,v 1.1 2002/06/04 14:20:50 aymeric Exp $
38 dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
40 AC_DEFUN([ACX_PTHREAD], [
41 AC_REQUIRE([AC_CANONICAL_HOST])
44 # We used to check for pthread.h first, but this fails if pthread.h
45 # requires special compiler flags (e.g. on True64 or Sequent).
46 # It gets checked for in the link test anyway.
48 # First of all, check if the user has set any of the PTHREAD_LIBS,
49 # etcetera environment variables, and if threads linking works using
51 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
53 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
55 LIBS="$PTHREAD_LIBS $LIBS"
56 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
57 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
58 AC_MSG_RESULT($acx_pthread_ok)
59 if test x"$acx_pthread_ok" = xno; then
67 # We must check for the threads library under a number of different
68 # names; the ordering is very important because some systems
69 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
70 # libraries is broken (non-POSIX).
72 # Create a list of thread flags to try. Items starting with a "-" are
73 # C compiler flags, and other items are library names, except for "none"
74 # which indicates that we try without any flags at all.
76 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt"
78 # The ordering *is* (sometimes) important. Some notes on the
79 # individual items follow:
81 # pthreads: AIX (must check this before -lpthread)
82 # none: in case threads are in libc; should be tried before -Kthread and
83 # other compiler flags to prevent continual compiler warnings
84 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
85 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
86 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
87 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
88 # -pthreads: Solaris/gcc
89 # -mthreads: Mingw32/gcc, Lynx/gcc
90 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
91 # doesn't hurt to check since this sometimes defines pthreads too;
92 # also defines -D_REENTRANT)
93 # pthread: Linux, etcetera
94 # --thread-safe: KAI C++
96 case "${host_cpu}-${host_os}" in
99 # On Solaris (at least, for some versions), libc contains stubbed
100 # (non-functional) versions of the pthreads routines, so link-based
101 # tests will erroneously succeed. (We need to link with -pthread or
102 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
103 # a function called by this macro, so we could check for that, but
104 # who knows whether they'll stub that too in a future libc.) So,
105 # we'll just look for -pthreads and -lpthread first:
107 acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
111 if test x"$acx_pthread_ok" = xno; then
112 for flag in $acx_pthread_flags; do
116 AC_MSG_CHECKING([whether pthreads work without any flags])
120 AC_MSG_CHECKING([whether pthreads work with $flag])
121 PTHREAD_CFLAGS="$flag"
125 AC_MSG_CHECKING([for the pthreads library -l$flag])
126 PTHREAD_LIBS="-l$flag"
131 save_CFLAGS="$CFLAGS"
132 LIBS="$PTHREAD_LIBS $LIBS"
133 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
135 # Check for various functions. We must include pthread.h,
136 # since some functions may be macros. (On the Sequent, we
137 # need a special flag -Kthread to make this header compile.)
138 # We check for pthread_join because it is in -lpthread on IRIX
139 # while pthread_create is in libc. We check for pthread_attr_init
140 # due to DEC craziness with -lpthreads. We check for
141 # pthread_cleanup_push because it is one of the few pthread
142 # functions on Solaris that doesn't have a non-functional libc stub.
143 # We try pthread_create on general principles.
144 AC_TRY_LINK([#include <pthread.h>],
145 [pthread_t th; pthread_join(th, 0);
146 pthread_attr_init(0); pthread_cleanup_push(0, 0);
147 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
148 [acx_pthread_ok=yes])
151 CFLAGS="$save_CFLAGS"
153 AC_MSG_RESULT($acx_pthread_ok)
154 if test "x$acx_pthread_ok" = xyes; then
163 # Various other checks:
164 if test "x$acx_pthread_ok" = xyes; then
166 LIBS="$PTHREAD_LIBS $LIBS"
167 save_CFLAGS="$CFLAGS"
168 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
170 # Detect AIX lossage: threads are created detached by default
171 # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
172 AC_MSG_CHECKING([for joinable pthread attribute])
173 AC_TRY_LINK([#include <pthread.h>],
174 [int attr=PTHREAD_CREATE_JOINABLE;],
175 ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
176 if test x"$ok" = xunknown; then
177 AC_TRY_LINK([#include <pthread.h>],
178 [int attr=PTHREAD_CREATE_UNDETACHED;],
179 ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
181 if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
182 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
183 [Define to the necessary symbol if this constant
184 uses a non-standard name on your system.])
187 if test x"$ok" = xunknown; then
188 AC_MSG_WARN([we do not know how to create joinable pthreads])
191 AC_MSG_CHECKING([if more special flags are required for pthreads])
193 case "${host_cpu}-${host_os}" in
194 *-aix* | *-freebsd*) flag="-D_THREAD_SAFE";;
195 *solaris* | alpha*-osf*) flag="-D_REENTRANT";;
197 AC_MSG_RESULT(${flag})
198 if test "x$flag" != xno; then
199 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
203 CFLAGS="$save_CFLAGS"
205 # More AIX lossage: must compile with cc_r
206 AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
211 AC_SUBST(PTHREAD_LIBS)
212 AC_SUBST(PTHREAD_CFLAGS)
215 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
216 if test x"$acx_pthread_ok" = xyes; then
217 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])