1 dnl Copyright (C) 2006-2008 CEA/DEN, EDF R&D
3 dnl This library is free software; you can redistribute it and/or
4 dnl modify it under the terms of the GNU Lesser General Public
5 dnl License as published by the Free Software Foundation; either
6 dnl version 2.1 of the License.
8 dnl This library is distributed in the hope that it will be useful,
9 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
10 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 dnl Lesser General Public License for more details.
13 dnl You should have received a copy of the GNU Lesser General Public
14 dnl License along with this library; if not, write to the Free Software
15 dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 dnl See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 # Check presence of "threads"
21 # Author : Anthony GEAY (CEA, 2006)
24 AC_DEFUN([CHECK_THREAD],[
26 AC_CHECKING(for THREADs)
32 if test `uname` = "Linux"
34 THREAD_DEF="-DYACS_PTHREAD"
35 AC_CHECK_HEADER(pthread.h,thread_ok=yes ,thread_ok=no)
40 AC_MSG_RESULT(for THREADs: $thread_ok)
44 AC_DEFUN([ACX_PTHREAD], [
45 AC_REQUIRE([AC_CANONICAL_HOST])
50 # We used to check for pthread.h first, but this fails if pthread.h
51 # requires special compiler flags (e.g. on True64 or Sequent).
52 # It gets checked for in the link test anyway.
54 # First of all, check if the user has set any of the PTHREAD_LIBS,
55 # etcetera environment variables, and if threads linking works using
57 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
59 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
61 LIBS="$PTHREAD_LIBS $LIBS"
62 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
63 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
64 AC_MSG_RESULT($acx_pthread_ok)
65 if test x"$acx_pthread_ok" = xno; then
73 # We must check for the threads library under a number of different
74 # names; the ordering is very important because some systems
75 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
76 # libraries is broken (non-POSIX).
78 # Create a list of thread flags to try. Items starting with a "-" are
79 # C compiler flags, and other items are library names, except for "none"
80 # which indicates that we try without any flags at all, and "pthread-config"
81 # which is a program returning the flags for the Pth emulation library.
83 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
85 # The ordering *is* (sometimes) important. Some notes on the
86 # individual items follow:
88 # pthreads: AIX (must check this before -lpthread)
89 # none: in case threads are in libc; should be tried before -Kthread and
90 # other compiler flags to prevent continual compiler warnings
91 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
92 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
93 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
94 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
95 # -pthreads: Solaris/gcc
96 # -mthreads: Mingw32/gcc, Lynx/gcc
97 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
98 # doesn't hurt to check since this sometimes defines pthreads too;
99 # also defines -D_REENTRANT)
100 # pthread: Linux, etcetera
101 # --thread-safe: KAI C++
102 # pthread-config: use pthread-config program (for GNU Pth library)
104 case "${host_cpu}-${host_os}" in
107 # On linux "none" works, but boost requires -pthread, so, try first "-pthread"
108 acx_pthread_flags="-pthread pthreads none -Kthread -kthread lthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
113 # On Solaris (at least, for some versions), libc contains stubbed
114 # (non-functional) versions of the pthreads routines, so link-based
115 # tests will erroneously succeed. (We need to link with -pthread or
116 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
117 # a function called by this macro, so we could check for that, but
118 # who knows whether they'll stub that too in a future libc.) So,
119 # we'll just look for -pthreads and -lpthread first:
121 acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
125 if test x"$acx_pthread_ok" = xno; then
126 for flag in $acx_pthread_flags; do
130 AC_MSG_CHECKING([whether pthreads work without any flags])
134 AC_MSG_CHECKING([whether pthreads work with $flag])
135 PTHREAD_CFLAGS="$flag"
139 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
140 if test x"$acx_pthread_config" = xno; then continue; fi
141 PTHREAD_CFLAGS="`pthread-config --cflags`"
142 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
146 AC_MSG_CHECKING([for the pthreads library -l$flag])
147 PTHREAD_LIBS="-l$flag"
152 save_CFLAGS="$CFLAGS"
153 LIBS="$PTHREAD_LIBS $LIBS"
154 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
156 # Check for various functions. We must include pthread.h,
157 # since some functions may be macros. (On the Sequent, we
158 # need a special flag -Kthread to make this header compile.)
159 # We check for pthread_join because it is in -lpthread on IRIX
160 # while pthread_create is in libc. We check for pthread_attr_init
161 # due to DEC craziness with -lpthreads. We check for
162 # pthread_cleanup_push because it is one of the few pthread
163 # functions on Solaris that doesn't have a non-functional libc stub.
164 # We try pthread_create on general principles.
165 AC_TRY_LINK([#include <pthread.h>],
166 [pthread_t th; pthread_join(th, 0);
167 pthread_attr_init(0); pthread_cleanup_push(0, 0);
168 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
169 [acx_pthread_ok=yes])
172 CFLAGS="$save_CFLAGS"
174 AC_MSG_RESULT($acx_pthread_ok)
175 if test "x$acx_pthread_ok" = xyes; then
184 # Various other checks:
185 if test "x$acx_pthread_ok" = xyes; then
187 LIBS="$PTHREAD_LIBS $LIBS"
188 save_CFLAGS="$CFLAGS"
189 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
191 # Detect AIX lossage: threads are created detached by default
192 # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
193 AC_MSG_CHECKING([for joinable pthread attribute])
194 AC_TRY_LINK([#include <pthread.h>],
195 [int attr=PTHREAD_CREATE_JOINABLE;],
196 ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
197 if test x"$ok" = xunknown; then
198 AC_TRY_LINK([#include <pthread.h>],
199 [int attr=PTHREAD_CREATE_UNDETACHED;],
200 ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
202 if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
203 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
204 [Define to the necessary symbol if this constant
205 uses a non-standard name on your system.])
208 if test x"$ok" = xunknown; then
209 AC_MSG_WARN([we do not know how to create joinable pthreads])
212 AC_MSG_CHECKING([if more special flags are required for pthreads])
214 case "${host_cpu}-${host_os}" in
215 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
216 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
218 AC_MSG_RESULT(${flag})
219 if test "x$flag" != xno; then
220 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
224 CFLAGS="$save_CFLAGS"
226 # More AIX lossage: must compile with cc_r
227 AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
232 AC_SUBST(PTHREAD_LIBS)
233 AC_SUBST(PTHREAD_CFLAGS)
236 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
237 if test x"$acx_pthread_ok" = xyes; then
238 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
248 AC_DEFUN([ENABLE_PTHREADS],[
249 AC_REQUIRE([ACX_PTHREAD])
251 if test x"$enable_pthreads_done" != xyes; then
252 if test x"$acx_pthread_ok" = xyes; then
253 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
254 CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
255 LIBS="$LIBS $PTHREAD_LIBS"
260 enable_pthreads_done=yes