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