source: branches/gsoc09-logging/base/m4/pthread.m4 @ 136621

Last change on this file since 136621 was 50736, checked in by toby@…, 11 years ago

eliminate the last autoconf warning

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