129
125
[#include <sys/types.h>
130
126
#include <sys/socket.h>])
133
dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
135
dnl This macro figures out how to build C programs using POSIX
136
dnl threads. It sets the PTHREAD_LIBS output variable to the threads
137
dnl library and linker flags, and the PTHREAD_CFLAGS output variable
138
dnl to any special C compiler flags that are needed. (The user can also
139
dnl force certain compiler flags/libs to be tested by setting these
140
dnl environment variables.)
142
dnl Also sets PTHREAD_CC to any special C compiler that is needed for
143
dnl multi-threaded programs (defaults to the value of CC otherwise).
144
dnl (This is necessary on AIX to use the special cc_r compiler alias.)
146
dnl If you are only building threads programs, you may wish to
147
dnl use these variables in your default LIBS, CFLAGS, and CC:
149
dnl LIBS="$PTHREAD_LIBS $LIBS"
150
dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
153
dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
154
dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
155
dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
157
dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
158
dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
159
dnl to run it if it is not found. If ACTION-IF-FOUND is not specified,
160
dnl the default action will define HAVE_PTHREAD.
162
dnl Please let the authors know if this macro fails on any platform,
163
dnl or if you have any other suggestions or comments. This macro was
164
dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
165
dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
166
dnl macros posted by AFC to the autoconf macro repository. We are also
167
dnl grateful for the helpful feedback of numerous users.
169
dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
171
AC_DEFUN([ACX_PTHREAD], [
172
AC_REQUIRE([AC_CANONICAL_HOST])
175
# We used to check for pthread.h first, but this fails if pthread.h
176
# requires special compiler flags (e.g. on True64 or Sequent).
177
# It gets checked for in the link test anyway.
179
# First of all, check if the user has set any of the PTHREAD_LIBS,
180
# etcetera environment variables, and if threads linking works using
182
if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
183
save_CFLAGS="$CFLAGS"
184
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
186
LIBS="$PTHREAD_LIBS $LIBS"
187
AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
188
AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
189
AC_MSG_RESULT($acx_pthread_ok)
190
if test x"$acx_pthread_ok" = xno; then
195
CFLAGS="$save_CFLAGS"
198
# We must check for the threads library under a number of different
199
# names; the ordering is very important because some systems
200
# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
201
# libraries is broken (non-POSIX).
203
# Create a list of thread flags to try. Items starting with a "-" are
204
# C compiler flags, and other items are library names, except for "none"
205
# which indicates that we try without any flags at all.
207
acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt"
209
# The ordering *is* (sometimes) important. Some notes on the
210
# individual items follow:
212
# pthreads: AIX (must check this before -lpthread)
213
# none: in case threads are in libc; should be tried before -Kthread and
214
# other compiler flags to prevent continual compiler warnings
215
# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
216
# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
217
# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
218
# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
219
# -pthreads: Solaris/gcc
220
# -mthreads: Mingw32/gcc, Lynx/gcc
221
# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
222
# doesn't hurt to check since this sometimes defines pthreads too;
223
# also defines -D_REENTRANT)
224
# pthread: Linux, etcetera
225
# --thread-safe: KAI C++
230
# On Solaris (at least, for some versions), libc contains stubbed
231
# (non-functional) versions of the pthreads routines, so link-based
232
# tests will erroneously succeed. (We need to link with -pthread or
233
# -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
234
# a function called by this macro, so we could check for that, but
235
# who knows whether they'll stub that too in a future libc.) So,
236
# we'll just look for -pthreads and -lpthread first:
238
acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
242
if test x"$acx_pthread_ok" = xno; then
243
for flag in $acx_pthread_flags; do
247
AC_MSG_CHECKING([whether pthreads work without any flags])
251
AC_MSG_CHECKING([whether pthreads work with $flag])
252
PTHREAD_CFLAGS="$flag"
256
AC_MSG_CHECKING([for the pthreads library -l$flag])
257
PTHREAD_LIBS="-l$flag"
262
save_CFLAGS="$CFLAGS"
263
LIBS="$PTHREAD_LIBS $LIBS"
264
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
266
# Check for various functions. We must include pthread.h,
267
# since some functions may be macros. (On the Sequent, we
268
# need a special flag -Kthread to make this header compile.)
269
# We check for pthread_join because it is in -lpthread on IRIX
270
# while pthread_create is in libc. We check for pthread_attr_init
271
# due to DEC craziness with -lpthreads. We check for
272
# pthread_cleanup_push because it is one of the few pthread
273
# functions on Solaris that doesn't have a non-functional libc stub.
274
# We try pthread_create on general principles.
275
AC_TRY_LINK([#include <pthread.h>],
276
[pthread_t th; pthread_join(th, 0);
277
pthread_attr_init(0); pthread_cleanup_push(0, 0);
278
pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
279
[acx_pthread_ok=yes])
282
CFLAGS="$save_CFLAGS"
284
AC_MSG_RESULT($acx_pthread_ok)
285
if test "x$acx_pthread_ok" = xyes; then
294
# Various other checks:
295
if test "x$acx_pthread_ok" = xyes; then
297
LIBS="$PTHREAD_LIBS $LIBS"
298
save_CFLAGS="$CFLAGS"
299
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
301
# Detect AIX lossage: threads are created detached by default
302
# and the JOINABLE attribute has a nonstandard name (UNDETACHED).
303
AC_MSG_CHECKING([for joinable pthread attribute])
304
AC_TRY_LINK([#include <pthread.h>],
305
[int attr=PTHREAD_CREATE_JOINABLE;],
306
ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
307
if test x"$ok" = xunknown; then
308
AC_TRY_LINK([#include <pthread.h>],
309
[int attr=PTHREAD_CREATE_UNDETACHED;],
310
ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
312
if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
313
AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
314
[Define to the necessary symbol if this constant
315
uses a non-standard name on your system.])
318
if test x"$ok" = xunknown; then
319
AC_MSG_WARN([we do not know how to create joinable pthreads])
322
AC_MSG_CHECKING([if more special flags are required for pthreads])
325
*-aix* | *-freebsd*) flag="-D_THREAD_SAFE";;
326
*solaris* | alpha*-osf* | *linux*) flag="-D_REENTRANT";;
328
AC_MSG_RESULT(${flag})
329
if test "x$flag" != xno; then
330
PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
334
CFLAGS="$save_CFLAGS"
336
# More AIX lossage: must compile with cc_r
337
AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
342
AC_SUBST(PTHREAD_LIBS)
343
AC_SUBST(PTHREAD_CFLAGS)
346
# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
347
if test x"$acx_pthread_ok" = xyes; then
348
ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])