monotone

monotone Mtn Source Tree

Root/m4/acx_pthread.m4

1dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2dnl
3dnl @summary figure out how to build C programs using POSIX threads
4dnl
5dnl This macro figures out how to build C programs using POSIX threads.
6dnl It sets the PTHREAD_LIBS output variable to the threads library and
7dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
8dnl C compiler flags that are needed. (The user can also force certain
9dnl compiler flags/libs to be tested by setting these environment
10dnl variables.)
11dnl
12dnl Also sets PTHREAD_CC to any special C compiler that is needed for
13dnl multi-threaded programs (defaults to the value of CC otherwise).
14dnl (This is necessary on AIX to use the special cc_r compiler alias.)
15dnl
16dnl NOTE: You are assumed to not only compile your program with these
17dnl flags, but also link it with them as well. e.g. you should link
18dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
19dnl $LIBS
20dnl
21dnl If you are only building threads programs, you may wish to use
22dnl these variables in your default LIBS, CFLAGS, and CC:
23dnl
24dnl LIBS="$PTHREAD_LIBS $LIBS"
25dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
26dnl CC="$PTHREAD_CC"
27dnl
28dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
29dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
30dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
31dnl
32dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
33dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
34dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
35dnl default action will define HAVE_PTHREAD.
36dnl
37dnl Please let the authors know if this macro fails on any platform, or
38dnl if you have any other suggestions or comments. This macro was based
39dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
40dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
41dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
42dnl We are also grateful for the helpful feedback of numerous users.
43dnl
44dnl @category InstalledPackages
45dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
46dnl @version 2006-05-29
47dnl @license GPLWithACException
48
49AC_DEFUN([ACX_PTHREAD], [
50AC_REQUIRE([AC_CANONICAL_HOST])
51AC_LANG_ASSERT([C])
52acx_pthread_ok=no
53
54# We used to check for pthread.h first, but this fails if pthread.h
55# requires special compiler flags (e.g. on True64 or Sequent).
56# It gets checked for in the link test anyway.
57
58# First of all, check if the user has set any of the PTHREAD_LIBS,
59# etcetera environment variables, and if threads linking works using
60# them:
61if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
62 save_CFLAGS="$CFLAGS"
63 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
64 save_LIBS="$LIBS"
65 LIBS="$PTHREAD_LIBS $LIBS"
66 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
67 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
68 AC_MSG_RESULT($acx_pthread_ok)
69 if test x"$acx_pthread_ok" = xno; then
70 PTHREAD_LIBS=""
71 PTHREAD_CFLAGS=""
72 fi
73 LIBS="$save_LIBS"
74 CFLAGS="$save_CFLAGS"
75fi
76
77# We must check for the threads library under a number of different
78# names; the ordering is very important because some systems
79# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
80# libraries is broken (non-POSIX).
81
82# Create a list of thread flags to try. Items starting with a "-" are
83# C compiler flags, and other items are library names, except for "none"
84# which indicates that we try without any flags at all, and "pthread-config"
85# which is a program returning the flags for the Pth emulation library.
86
87acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
88
89# The ordering *is* (sometimes) important. Some notes on the
90# individual items follow:
91
92# pthreads: AIX (must check this before -lpthread)
93# none: in case threads are in libc; should be tried before -Kthread and
94# other compiler flags to prevent continual compiler warnings
95# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
96# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
97# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
98# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
99# -pthreads: Solaris/gcc
100# -mthreads: Mingw32/gcc, Lynx/gcc
101# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
102# doesn't hurt to check since this sometimes defines pthreads too;
103# also defines -D_REENTRANT)
104# ... -mt is also the pthreads flag for HP/aCC
105# pthread: Linux, etcetera
106# --thread-safe: KAI C++
107# pthread-config: use pthread-config program (for GNU Pth library)
108
109case "${host_cpu}-${host_os}" in
110 *solaris*)
111
112 # On Solaris (at least, for some versions), libc contains stubbed
113 # (non-functional) versions of the pthreads routines, so link-based
114 # tests will erroneously succeed. (We need to link with -pthreads/-mt/
115 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
116 # a function called by this macro, so we could check for that, but
117 # who knows whether they'll stub that too in a future libc.) So,
118 # we'll just look for -pthreads and -lpthread first:
119
120 acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
121 ;;
122esac
123
124if test x"$acx_pthread_ok" = xno; then
125for flag in $acx_pthread_flags; do
126
127 case $flag in
128 none)
129 AC_MSG_CHECKING([whether pthreads work without any flags])
130 ;;
131
132 -*)
133 AC_MSG_CHECKING([whether pthreads work with $flag])
134 PTHREAD_CFLAGS="$flag"
135 ;;
136
137pthread-config)
138AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
139if test x"$acx_pthread_config" = xno; then continue; fi
140PTHREAD_CFLAGS="`pthread-config --cflags`"
141PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
142;;
143
144 *)
145 AC_MSG_CHECKING([for the pthreads library -l$flag])
146 PTHREAD_LIBS="-l$flag"
147 ;;
148 esac
149
150 save_LIBS="$LIBS"
151 save_CFLAGS="$CFLAGS"
152 LIBS="$PTHREAD_LIBS $LIBS"
153 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
154
155 # Check for various functions. We must include pthread.h,
156 # since some functions may be macros. (On the Sequent, we
157 # need a special flag -Kthread to make this header compile.)
158 # We check for pthread_join because it is in -lpthread on IRIX
159 # while pthread_create is in libc. We check for pthread_attr_init
160 # due to DEC craziness with -lpthreads. We check for
161 # pthread_cleanup_push because it is one of the few pthread
162 # functions on Solaris that doesn't have a non-functional libc stub.
163 # We try pthread_create on general principles.
164 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
165 [pthread_t th; pthread_join(th, 0);
166 pthread_attr_init(0); pthread_cleanup_push(0, 0);
167 pthread_create(0,0,0,0); pthread_cleanup_pop(0);])],
168 [acx_pthread_ok=yes])
169
170 LIBS="$save_LIBS"
171 CFLAGS="$save_CFLAGS"
172
173 AC_MSG_RESULT($acx_pthread_ok)
174 if test "x$acx_pthread_ok" = xyes; then
175 break;
176 fi
177
178 PTHREAD_LIBS=""
179 PTHREAD_CFLAGS=""
180done
181fi
182
183# Various other checks:
184if test "x$acx_pthread_ok" = xyes; then
185 save_LIBS="$LIBS"
186 LIBS="$PTHREAD_LIBS $LIBS"
187 save_CFLAGS="$CFLAGS"
188 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
189
190 # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
191AC_MSG_CHECKING([for joinable pthread attribute])
192attr_name=unknown
193for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
194 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
195 [int attr=$attr; return attr;])],
196[attr_name=$attr; break])
197done
198 AC_MSG_RESULT($attr_name)
199 if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
200 AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
201 [Define to necessary symbol if this constant
202 uses a non-standard name on your system.])
203 fi
204
205 AC_MSG_CHECKING([if more special flags are required for pthreads])
206 flag=no
207 case "${host_cpu}-${host_os}" in
208 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
209 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
210 esac
211 AC_MSG_RESULT(${flag})
212 if test "x$flag" != xno; then
213 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
214 fi
215
216 LIBS="$save_LIBS"
217 CFLAGS="$save_CFLAGS"
218
219 # More AIX lossage: must compile with xlc_r or cc_r
220if test x"$GCC" != xyes; then
221 AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
222 else
223 PTHREAD_CC=$CC
224fi
225else
226 PTHREAD_CC="$CC"
227fi
228
229AC_SUBST(PTHREAD_LIBS)
230AC_SUBST(PTHREAD_CFLAGS)
231AC_SUBST(PTHREAD_CC)
232
233# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
234if test x"$acx_pthread_ok" = xyes; then
235 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
236 :
237else
238 acx_pthread_ok=no
239 $2
240fi
241])dnl ACX_PTHREAD

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status