OSDN Git Service

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