Clean up ecpg's use of mmerror(): const-ify the format argument, add an
[PostgreSQL.git] / config / acx_pthread.m4
blobceb161a556ae295918f853dfadbd08b5a6a6305f
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
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.
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"
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).
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.
46 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config pthreadGC2"
48 # The ordering *is* (sometimes) important.  Some notes on the
49 # individual items follow:
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)
67 case "${host_cpu}-${host_os}" in
68         *solaris*)
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:
78         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
79         ;;
80 esac
82 if test x"$acx_pthread_ok" = xno; then
83 for flag in $acx_pthread_flags; do
85         tryPTHREAD_CFLAGS=""
86         tryPTHREAD_LIBS=""
87         case $flag in
88                 none)
89                 AC_MSG_CHECKING([whether pthreads work without any flags])
90                 ;;
92                 -*)
93                 AC_MSG_CHECKING([whether pthreads work with $flag])
94                 tryPTHREAD_CFLAGS="$flag"
95                 ;;
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                 ;;
106                 *)
107                 AC_MSG_CHECKING([for the pthreads library -l$flag])
108                 tryPTHREAD_LIBS="-l$flag"
109                 ;;
110         esac
112         save_LIBS="$LIBS"
113         save_CFLAGS="$CFLAGS"
114         LIBS="$tryPTHREAD_LIBS $PTHREAD_LIBS $LIBS"
115         CFLAGS="$CFLAGS $PTHREAD_CFLAGS $tryPTHREAD_CFLAGS"
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])
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
137 main (int argc, char **argv)
139   (void) argc;
140   (void) argv;
141   return 0;
143 _ACEOF
144             rm -f conftest.$ac_objext conftest$ac_exeext
145             if test "`(eval $ac_link 2>&1 1>&5)`" = ""; then
146                 # we continue with more flags because Linux needs -lpthread
147                 # for libpq builds on PostgreSQL.  The test above only
148                 # tests for building binaries, not shared libraries.
149                 PTHREAD_LIBS=" $tryPTHREAD_LIBS $PTHREAD_LIBS"
150                 PTHREAD_CFLAGS="$PTHREAD_CFLAGS $tryPTHREAD_CFLAGS"
151             else   acx_pthread_ok=no
152             fi
153         fi
155         LIBS="$save_LIBS"
156         CFLAGS="$save_CFLAGS"
158         AC_MSG_RESULT($acx_pthread_ok)
159 done
162 # Various other checks:
163 if test "x$acx_pthread_ok" = xyes; then
164         save_LIBS="$LIBS"
165         LIBS="$PTHREAD_LIBS $LIBS"
166         save_CFLAGS="$CFLAGS"
167         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
169         # Detect AIX lossage: threads are created detached by default
170         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
171         AC_MSG_CHECKING([for joinable pthread attribute])
172         AC_TRY_LINK([#include <pthread.h>],
173                     [int attr=PTHREAD_CREATE_JOINABLE;],
174                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
175         if test x"$ok" = xunknown; then
176                 AC_TRY_LINK([#include <pthread.h>],
177                             [int attr=PTHREAD_CREATE_UNDETACHED;],
178                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
179         fi
180         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
181                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
182                           [Define to the necessary symbol if this constant
183                            uses a non-standard name on your system.])
184         fi
185         AC_MSG_RESULT(${ok})
186         if test x"$ok" = xunknown; then
187                 AC_MSG_WARN([we do not know how to create joinable pthreads])
188         fi
190         AC_MSG_CHECKING([if more special flags are required for pthreads])
191         flag=no
192 # We always add these in PostgreSQL
193 #       case "${host_cpu}-${host_os}" in
194 #               *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
195 #               *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
196 #       esac
197         AC_MSG_RESULT(${flag})
198         if test "x$flag" != xno; then
199                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
200         fi
202         LIBS="$save_LIBS"
203         CFLAGS="$save_CFLAGS"
205 # Supporting cc_r would require a special CC in all places that
206 # use libpq, and that is ugly, so we don't do it.  Users can still
207 # define their compiler as cc_r to do thread builds of everything.
208         # More AIX lossage: must compile with cc_r
209         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
210 else
211         PTHREAD_CC="$CC"
214 AC_SUBST(PTHREAD_LIBS)
215 AC_SUBST(PTHREAD_CFLAGS)
216 AC_SUBST(PTHREAD_CC)
218 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
219 if test x"$acx_pthread_ok" = xyes; then
220         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
221         :
222 else
223         acx_pthread_ok=no
224         $2
226 AC_LANG_RESTORE
227 ])dnl ACX_PTHREAD