1 dnl BEGIN http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html
2 dnl $Id: ac_acx_pthread.m4,v 1.3 2004/09/07 01:02:58 dnelson Exp $
3 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
5 dnl This macro figures out how to build C programs using POSIX
6 dnl threads. It sets the PTHREAD_LIBS output variable to the threads
7 dnl library and linker flags, and the PTHREAD_CFLAGS output variable
8 dnl to any special C compiler flags that are needed. (The user can also
9 dnl force certain compiler flags/libs to be tested by setting these
10 dnl environment variables.)
12 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
13 dnl multi-threaded programs (defaults to the value of CC otherwise).
14 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
16 dnl If you are only building threads programs, you may wish to
17 dnl use these variables in your default LIBS, CFLAGS, and CC:
19 dnl LIBS="$PTHREAD_LIBS $LIBS"
20 dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
23 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
24 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
25 dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
27 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
28 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
29 dnl to run it if it is not found. If ACTION-IF-FOUND is not specified,
30 dnl the default action will define HAVE_PTHREAD.
32 dnl Please let the authors know if this macro fails on any platform,
33 dnl or if you have any other suggestions or comments. This macro was
34 dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
35 dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
36 dnl macros posted by AFC to the autoconf macro repository. We are also
37 dnl grateful for the helpful feedback of numerous users.
39 dnl @version Id: acx_pthread.m4,v 1.10 2004-06-16
40 dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
42 AC_DEFUN([ACX_PTHREAD], [
43 AC_REQUIRE([AC_CANONICAL_HOST])
48 # We used to check for pthread.h first, but this fails if pthread.h
49 # requires special compiler flags (e.g. on True64 or Sequent).
50 # It gets checked for in the link test anyway.
52 # First of all, check if the user has set any of the PTHREAD_LIBS,
53 # etcetera environment variables, and if threads linking works using
55 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
57 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
59 LIBS="$PTHREAD_LIBS $LIBS"
60 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
61 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
62 AC_MSG_RESULT($acx_pthread_ok)
63 if test x"$acx_pthread_ok" = xno; then
71 # We must check for the threads library under a number of different
72 # names; the ordering is very important because some systems
73 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
74 # libraries is broken (non-POSIX).
76 # Create a list of thread flags to try. Items starting with a "-" are
77 # C compiler flags, and other items are library names, except for "none"
78 # which indicates that we try without any flags at all, and "pthread-config"
79 # which is a program returning the flags for the Pth emulation library.
81 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
83 # The ordering *is* (sometimes) important. Some notes on the
84 # individual items follow:
86 # pthreads: AIX (must check this before -lpthread)
87 # none: in case threads are in libc; should be tried before -Kthread and
88 # other compiler flags to prevent continual compiler warnings
89 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
90 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
91 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
92 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
93 # -pthreads: Solaris/gcc
94 # -mthreads: Mingw32/gcc, Lynx/gcc
95 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
96 # doesn't hurt to check since this sometimes defines pthreads too;
97 # also defines -D_REENTRANT)
98 # pthread: Linux, etcetera
99 # --thread-safe: KAI C++
100 # pthread-config: use pthread-config program (for GNU Pth library)
102 case "${host_cpu}-${host_os}" in
105 # On Solaris (at least, for some versions), libc contains stubbed
106 # (non-functional) versions of the pthreads routines, so link-based
107 # tests will erroneously succeed. (We need to link with -pthread or
108 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
109 # a function called by this macro, so we could check for that, but
110 # who knows whether they'll stub that too in a future libc.) So,
111 # we'll just look for -pthreads and -lpthread first:
113 acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
117 if test x"$acx_pthread_ok" = xno; then
118 for flag in $acx_pthread_flags; do
122 AC_MSG_CHECKING([whether pthreads work without any flags])
126 AC_MSG_CHECKING([whether pthreads work with $flag])
127 PTHREAD_CFLAGS="$flag"
131 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
132 if test x"$acx_pthread_config" = xno; then continue; fi
133 PTHREAD_CFLAGS="`pthread-config --cflags`"
134 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
138 AC_MSG_CHECKING([for the pthreads library -l$flag])
139 PTHREAD_LIBS="-l$flag"
144 save_CFLAGS="$CFLAGS"
145 LIBS="$PTHREAD_LIBS $LIBS"
146 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
148 # Check for various functions. We must include pthread.h,
149 # since some functions may be macros. (On the Sequent, we
150 # need a special flag -Kthread to make this header compile.)
151 # We check for pthread_join because it is in -lpthread on IRIX
152 # while pthread_create is in libc. We check for pthread_attr_init
153 # due to DEC craziness with -lpthreads. We check for
154 # pthread_cleanup_push because it is one of the few pthread
155 # functions on Solaris that doesn't have a non-functional libc stub.
156 # We try pthread_create on general principles.
157 AC_TRY_LINK([#include <pthread.h>],
158 [pthread_t th; pthread_join(th, 0);
159 pthread_attr_init(0); pthread_cleanup_push(0, 0);
160 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
161 [acx_pthread_ok=yes])
164 CFLAGS="$save_CFLAGS"
166 AC_MSG_RESULT($acx_pthread_ok)
167 if test "x$acx_pthread_ok" = xyes; then
176 # Various other checks:
177 if test "x$acx_pthread_ok" = xyes; then
179 LIBS="$PTHREAD_LIBS $LIBS"
180 save_CFLAGS="$CFLAGS"
181 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
183 # Detect AIX lossage: threads are created detached by default
184 # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
185 AC_MSG_CHECKING([for joinable pthread attribute])
186 AC_TRY_LINK([#include <pthread.h>],
187 [int attr=PTHREAD_CREATE_JOINABLE;],
188 ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
189 if test x"$ok" = xunknown; then
190 AC_TRY_LINK([#include <pthread.h>],
191 [int attr=PTHREAD_CREATE_UNDETACHED;],
192 ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
194 if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
195 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
196 [Define to the necessary symbol if this constant
197 uses a non-standard name on your system.])
200 if test x"$ok" = xunknown; then
201 AC_MSG_WARN([we do not know how to create joinable pthreads])
204 AC_MSG_CHECKING([if more special flags are required for pthreads])
206 case "${host_cpu}-${host_os}" in
207 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
208 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
210 AC_MSG_RESULT(${flag})
211 if test "x$flag" != xno; then
212 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
216 CFLAGS="$save_CFLAGS"
218 # More AIX lossage: must compile with cc_r
219 AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
224 AC_SUBST(PTHREAD_LIBS)
225 AC_SUBST(PTHREAD_CFLAGS)
228 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
229 if test x"$acx_pthread_ok" = xyes; then
230 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
238 dnl END http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html