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