Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/config/acx_pthread.m4
ViewVC logotype

Annotation of /branches/pure-cfg/config/acx_pthread.m4

Parent Directory Parent Directory | Revision Log Revision Log


Revision 123 - (view) (download)
Original Path: trunk/config/acx_pthread.m4

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

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0