/[hydra]/hydra/acx_pthread.m4
ViewVC logotype

Diff of /hydra/acx_pthread.m4

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by nmav, Sat Jan 4 12:07:04 2003 UTC revision 1.2 by nmav, Wed Jan 22 07:51:49 2003 UTC
# Line 0  Line 1 
1    dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2    dnl
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.)
9    dnl
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.)
13    dnl
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:
16    dnl
17    dnl        LIBS="$PTHREAD_LIBS $LIBS"
18    dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
19    dnl        CC="$PTHREAD_CC"
20    dnl
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).
24    dnl
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.
29    dnl
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.
36    dnl
37    dnl @version $Id$
38    dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
39    
40    AC_DEFUN([ACX_PTHREAD], [
41    AC_REQUIRE([AC_CANONICAL_HOST])
42    AC_LANG_SAVE
43    AC_LANG_C
44    acx_pthread_ok=no
45    
46    # We used to check for pthread.h first, but this fails if pthread.h
47    # requires special compiler flags (e.g. on True64 or Sequent).
48    # It gets checked for in the link test anyway.
49    
50    # First of all, check if the user has set any of the PTHREAD_LIBS,
51    # etcetera environment variables, and if threads linking works using
52    # them:
53    if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
54            save_CFLAGS="$CFLAGS"
55            CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
56            save_LIBS="$LIBS"
57            LIBS="$PTHREAD_LIBS $LIBS"
58            AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
59            AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
60            AC_MSG_RESULT($acx_pthread_ok)
61            if test x"$acx_pthread_ok" = xno; then
62                    PTHREAD_LIBS=""
63                    PTHREAD_CFLAGS=""
64            fi
65            LIBS="$save_LIBS"
66            CFLAGS="$save_CFLAGS"
67    fi
68    
69    # We must check for the threads library under a number of different
70    # names; the ordering is very important because some systems
71    # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
72    # libraries is broken (non-POSIX).
73    
74    # Create a list of thread flags to try.  Items starting with a "-" are
75    # C compiler flags, and other items are library names, except for "none"
76    # which indicates that we try without any flags at all.
77    
78    acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt"
79    
80    # The ordering *is* (sometimes) important.  Some notes on the
81    # individual items follow:
82    
83    # pthreads: AIX (must check this before -lpthread)
84    # none: in case threads are in libc; should be tried before -Kthread and
85    #       other compiler flags to prevent continual compiler warnings
86    # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
87    # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
88    # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
89    # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
90    # -pthreads: Solaris/gcc
91    # -mthreads: Mingw32/gcc, Lynx/gcc
92    # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
93    #      doesn't hurt to check since this sometimes defines pthreads too;
94    #      also defines -D_REENTRANT)
95    # pthread: Linux, etcetera
96    # --thread-safe: KAI C++
97    
98    case "${host_cpu}-${host_os}" in
99            *solaris*)
100    
101            # On Solaris (at least, for some versions), libc contains stubbed
102            # (non-functional) versions of the pthreads routines, so link-based
103            # tests will erroneously succeed.  (We need to link with -pthread or
104            # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
105            # a function called by this macro, so we could check for that, but
106            # who knows whether they'll stub that too in a future libc.)  So,
107            # we'll just look for -pthreads and -lpthread first:
108    
109            acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
110            ;;
111    esac
112    
113    if test x"$acx_pthread_ok" = xno; then
114    for flag in $acx_pthread_flags; do
115    
116            case $flag in
117                    none)
118                    AC_MSG_CHECKING([whether pthreads work without any flags])
119                    ;;
120    
121                    -*)
122                    AC_MSG_CHECKING([whether pthreads work with $flag])
123                    PTHREAD_CFLAGS="$flag"
124                    ;;
125    
126                    *)
127                    AC_MSG_CHECKING([for the pthreads library -l$flag])
128                    PTHREAD_LIBS="-l$flag"
129                    ;;
130            esac
131    
132            save_LIBS="$LIBS"
133            save_CFLAGS="$CFLAGS"
134            LIBS="$PTHREAD_LIBS $LIBS"
135            CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
136    
137            # Check for various functions.  We must include pthread.h,
138            # since some functions may be macros.  (On the Sequent, we
139            # need a special flag -Kthread to make this header compile.)
140            # We check for pthread_join because it is in -lpthread on IRIX
141            # while pthread_create is in libc.  We check for pthread_attr_init
142            # due to DEC craziness with -lpthreads.  We check for
143            # pthread_cleanup_push because it is one of the few pthread
144            # functions on Solaris that doesn't have a non-functional libc stub.
145            # We try pthread_create on general principles.
146            AC_TRY_LINK([#include <pthread.h>],
147                        [pthread_t th; pthread_join(th, 0);
148                         pthread_attr_init(0); pthread_cleanup_push(0, 0);
149                         pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
150                        [acx_pthread_ok=yes])
151    
152            LIBS="$save_LIBS"
153            CFLAGS="$save_CFLAGS"
154    
155            AC_MSG_RESULT($acx_pthread_ok)
156            if test "x$acx_pthread_ok" = xyes; then
157                    break;
158            fi
159    
160            PTHREAD_LIBS=""
161            PTHREAD_CFLAGS=""
162    done
163    fi
164    
165    # Various other checks:
166    if test "x$acx_pthread_ok" = xyes; then
167            save_LIBS="$LIBS"
168            LIBS="$PTHREAD_LIBS $LIBS"
169            save_CFLAGS="$CFLAGS"
170            CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
171    
172            # Detect AIX lossage: threads are created detached by default
173            # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
174            AC_MSG_CHECKING([for joinable pthread attribute])
175            AC_TRY_LINK([#include <pthread.h>],
176                        [int attr=PTHREAD_CREATE_JOINABLE;],
177                        ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
178            if test x"$ok" = xunknown; then
179                    AC_TRY_LINK([#include <pthread.h>],
180                                [int attr=PTHREAD_CREATE_UNDETACHED;],
181                                ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
182            fi
183            if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
184                    AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
185                              [Define to the necessary symbol if this constant
186                               uses a non-standard name on your system.])
187            fi
188            AC_MSG_RESULT(${ok})
189            if test x"$ok" = xunknown; then
190                    AC_MSG_WARN([we do not know how to create joinable pthreads])
191            fi
192    
193            AC_MSG_CHECKING([if more special flags are required for pthreads])
194            flag=no
195            case "${host_cpu}-${host_os}" in
196                    *-aix* | *-freebsd*)     flag="-D_THREAD_SAFE";;
197                    *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
198            esac
199            AC_MSG_RESULT(${flag})
200            if test "x$flag" != xno; then
201                    PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
202            fi
203    
204            LIBS="$save_LIBS"
205            CFLAGS="$save_CFLAGS"
206    
207            # More AIX lossage: must compile with cc_r
208            AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
209    else
210            PTHREAD_CC="$CC"
211    fi
212    
213    AC_SUBST(PTHREAD_LIBS)
214    AC_SUBST(PTHREAD_CFLAGS)
215    AC_SUBST(PTHREAD_CC)
216    
217    # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
218    if test x"$acx_pthread_ok" = xyes; then
219            ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
220            :
221    else
222            acx_pthread_ok=no
223            $2
224    fi
225    AC_LANG_RESTORE
226    ])dnl ACX_PTHREAD

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26