Debian Codegraph @

m4/acx_pthread.m4

raw
1 dnl Available from the GNU Autoconf Macro Archive at:
2 dnl http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html
3 dnl
4 AC_DEFUN([ACX_PTHREAD], [
5 AC_REQUIRE([AC_CANONICAL_HOST])
6 AC_LANG_SAVE
7 AC_LANG_C
8 acx_pthread_ok=no
9
10 # We used to check for pthread.h first, but this fails if pthread.h
11 # requires special compiler flags (e.g. on True64 or Sequent).
12 # It gets checked for in the link test anyway.
13
14 # First of all, check if the user has set any of the PTHREAD_LIBS,
15 # etcetera environment variables, and if threads linking works using
16 # them:
17 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
18 save_CFLAGS="$CFLAGS"
19 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
20 save_LIBS="$LIBS"
21 LIBS="$PTHREAD_LIBS $LIBS"
22 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
23 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
24 AC_MSG_RESULT($acx_pthread_ok)
25 if test x"$acx_pthread_ok" = xno; then
26 PTHREAD_LIBS=""
27 PTHREAD_CFLAGS=""
28 fi
29 LIBS="$save_LIBS"
30 CFLAGS="$save_CFLAGS"
31 fi
32
33 # We must check for the threads library under a number of different
34 # names; the ordering is very important because some systems
35 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
36 # libraries is broken (non-POSIX).
37
38 # Create a list of thread flags to try. Items starting with a "-" are
39 # C compiler flags, and other items are library names, except for "none"
40 # which indicates that we try without any flags at all, and "pthread-config"
41 # which is a program returning the flags for the Pth emulation library.
42
43 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
44
45 # The ordering *is* (sometimes) important. Some notes on the
46 # individual items follow:
47
48 # pthreads: AIX (must check this before -lpthread)
49 # none: in case threads are in libc; should be tried before -Kthread and
50 # other compiler flags to prevent continual compiler warnings
51 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
52 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
53 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
54 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
55 # -pthreads: Solaris/gcc
56 # -mthreads: Mingw32/gcc, Lynx/gcc
57 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
58 # doesn't hurt to check since this sometimes defines pthreads too;
59 # also defines -D_REENTRANT)
60 # ... -mt is also the pthreads flag for HP/aCC
61 # pthread: Linux, etcetera
62 # --thread-safe: KAI C++
63 # pthread-config: use pthread-config program (for GNU Pth library)
64
65 case "${host_cpu}-${host_os}" in
66 *solaris*)
67
68 # On Solaris (at least, for some versions), libc contains stubbed
69 # (non-functional) versions of the pthreads routines, so link-based
70 # tests will erroneously succeed. (We need to link with -pthreads/-mt/
71 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
72 # a function called by this macro, so we could check for that, but
73 # who knows whether they'll stub that too in a future libc.) So,
74 # we'll just look for -pthreads and -lpthread first:
75
76 acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
77 ;;
78 esac
79
80 if test x"$acx_pthread_ok" = xno; then
81 for flag in $acx_pthread_flags; do
82
83 case $flag in
84 none)
85 AC_MSG_CHECKING([whether pthreads work without any flags])
86 ;;
87
88 -*)
89 AC_MSG_CHECKING([whether pthreads work with $flag])
90 PTHREAD_CFLAGS="$flag"
91 ;;
92
93 pthread-config)
94 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
95 if test x"$acx_pthread_config" = xno; then continue; fi
96 PTHREAD_CFLAGS="`pthread-config --cflags`"
97 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
98 ;;
99
100 *)
101 AC_MSG_CHECKING([for the pthreads library -l$flag])
102 PTHREAD_LIBS="-l$flag"
103 ;;
104 esac
105
106 save_LIBS="$LIBS"
107 save_CFLAGS="$CFLAGS"
108 LIBS="$PTHREAD_LIBS $LIBS"
109 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
110
111 # Check for various functions. We must include pthread.h,
112 # since some functions may be macros. (On the Sequent, we
113 # need a special flag -Kthread to make this header compile.)
114 # We check for pthread_join because it is in -lpthread on IRIX
115 # while pthread_create is in libc. We check for pthread_attr_init
116 # due to DEC craziness with -lpthreads. We check for
117 # pthread_cleanup_push because it is one of the few pthread
118 # functions on Solaris that doesn't have a non-functional libc stub.
119 # We try pthread_create on general principles.
120 AC_TRY_LINK([#include <pthread.h>],
121 [pthread_t th; pthread_join(th, 0);
122 pthread_attr_init(0); pthread_cleanup_push(0, 0);
123 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
124 [acx_pthread_ok=yes])
125
126 LIBS="$save_LIBS"
127 CFLAGS="$save_CFLAGS"
128
129 AC_MSG_RESULT($acx_pthread_ok)
130 if test "x$acx_pthread_ok" = xyes; then
131 break;
132 fi
133
134 PTHREAD_LIBS=""
135 PTHREAD_CFLAGS=""
136 done
137 fi
138
139 # Various other checks:
140 if test "x$acx_pthread_ok" = xyes; then
141 save_LIBS="$LIBS"
142 LIBS="$PTHREAD_LIBS $LIBS"
143 save_CFLAGS="$CFLAGS"
144 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
145
146 # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
147 AC_MSG_CHECKING([for joinable pthread attribute])
148 attr_name=unknown
149 for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
150 AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
151 [attr_name=$attr; break])
152 done
153 AC_MSG_RESULT($attr_name)
154 if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
155 AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
156 [Define to necessary symbol if this constant
157 uses a non-standard name on your system.])
158 fi
159
160 AC_MSG_CHECKING([if more special flags are required for pthreads])
161 flag=no
162 case "${host_cpu}-${host_os}" in
163 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
164 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
165 esac
166 AC_MSG_RESULT(${flag})
167 if test "x$flag" != xno; then
168 PTHREAD_CPPFLAGS="$flag $PTHREAD_CPPFLAGS"
169 fi
170
171 AC_CHECK_FUNCS([pthread_spin_lock])
172 LIBS="$save_LIBS"
173 CFLAGS="$save_CFLAGS"
174
175 # More AIX lossage: must compile with xlc_r or cc_r
176 if test x"$GCC" != xyes; then
177 AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
178 else
179 PTHREAD_CC=$CC
180 fi
181 else
182 PTHREAD_CC="$CC"
183 fi
184
185 AC_SUBST(PTHREAD_LIBS)
186 AC_SUBST(PTHREAD_CFLAGS)
187 AC_SUBST(PTHREAD_CPPFLAGS)
188 AC_SUBST(PTHREAD_CC)
189
190 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
191 if test x"$acx_pthread_ok" = xyes; then
192 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
193 :
194 else
195 acx_pthread_ok=no
196 $2
197 fi
198 AC_LANG_RESTORE
199 ])dnl ACX_PTHREAD