1 dnl Copyright (C) 2009-2015 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, or (at your option) any later version.
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
20 dnl Paul RASCLE: modification for Linux: -pthread required by boost...
21 dnl Available from the GNU Autoconf Macro Archive at:
23 AC_DEFUN([ACX_PTHREAD], [
24 AC_REQUIRE([AC_CANONICAL_HOST])
29 # We used to check for pthread.h first, but this fails if pthread.h
30 # requires special compiler flags (e.g. on True64 or Sequent).
31 # It gets checked for in the link test anyway.
33 # First of all, check if the user has set any of the PTHREAD_LIBS,
34 # etcetera environment variables, and if threads linking works using
36 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
38 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
40 LIBS="$PTHREAD_LIBS $LIBS"
41 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
42 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
43 AC_MSG_RESULT($acx_pthread_ok)
44 if test x"$acx_pthread_ok" = xno; then
52 # We must check for the threads library under a number of different
53 # names; the ordering is very important because some systems
54 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
55 # libraries is broken (non-POSIX).
57 # Create a list of thread flags to try. Items starting with a "-" are
58 # C compiler flags, and other items are library names, except for "none"
59 # which indicates that we try without any flags at all, and "pthread-config"
60 # which is a program returning the flags for the Pth emulation library.
62 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
64 # The ordering *is* (sometimes) important. Some notes on the
65 # individual items follow:
67 # pthreads: AIX (must check this before -lpthread)
68 # none: in case threads are in libc; should be tried before -Kthread and
69 # other compiler flags to prevent continual compiler warnings
70 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
71 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
72 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
73 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
74 # -pthreads: Solaris/gcc
75 # -mthreads: Mingw32/gcc, Lynx/gcc
76 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
77 # doesn't hurt to check since this sometimes defines pthreads too;
78 # also defines -D_REENTRANT)
79 # pthread: Linux, etcetera
80 # --thread-safe: KAI C++
81 # pthread-config: use pthread-config program (for GNU Pth library)
83 case "${host_cpu}-${host_os}" in
86 # On linux "none" works, but boost requires -pthread, so, try first "-pthread"
87 acx_pthread_flags="-pthread pthreads none -Kthread -kthread lthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
92 # On Solaris (at least, for some versions), libc contains stubbed
93 # (non-functional) versions of the pthreads routines, so link-based
94 # tests will erroneously succeed. (We need to link with -pthread or
95 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
96 # a function called by this macro, so we could check for that, but
97 # who knows whether they'll stub that too in a future libc.) So,
98 # we'll just look for -pthreads and -lpthread first:
100 acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
104 if test x"$acx_pthread_ok" = xno; then
105 for flag in $acx_pthread_flags; do
109 AC_MSG_CHECKING([whether pthreads work without any flags])
113 AC_MSG_CHECKING([whether pthreads work with $flag])
114 PTHREAD_CFLAGS="$flag"
118 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
119 if test x"$acx_pthread_config" = xno; then continue; fi
120 PTHREAD_CFLAGS="`pthread-config --cflags`"
121 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
125 AC_MSG_CHECKING([for the pthreads library -l$flag])
126 PTHREAD_LIBS="-l$flag"
131 save_CFLAGS="$CFLAGS"
132 LIBS="$PTHREAD_LIBS $LIBS"
133 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
135 # Check for various functions. We must include pthread.h,
136 # since some functions may be macros. (On the Sequent, we
137 # need a special flag -Kthread to make this header compile.)
138 # We check for pthread_join because it is in -lpthread on IRIX
139 # while pthread_create is in libc. We check for pthread_attr_init
140 # due to DEC craziness with -lpthreads. We check for
141 # pthread_cleanup_push because it is one of the few pthread
142 # functions on Solaris that doesn't have a non-functional libc stub.
143 # We try pthread_create on general principles.
144 AC_TRY_LINK([#include <pthread.h>],
145 [pthread_t th; pthread_join(th, 0);
146 pthread_attr_init(0); pthread_cleanup_push(0, 0);
147 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
148 [acx_pthread_ok=yes])
151 CFLAGS="$save_CFLAGS"
153 AC_MSG_RESULT($acx_pthread_ok)
154 if test "x$acx_pthread_ok" = xyes; then
163 # Various other checks:
164 if test "x$acx_pthread_ok" = xyes; then
166 LIBS="$PTHREAD_LIBS $LIBS"
167 save_CFLAGS="$CFLAGS"
168 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
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)
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.])
187 if test x"$ok" = xunknown; then
188 AC_MSG_WARN([we do not know how to create joinable pthreads])
191 AC_MSG_CHECKING([if more special flags are required for pthreads])
193 case "${host_cpu}-${host_os}" in
194 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
195 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
197 AC_MSG_RESULT(${flag})
198 if test "x$flag" != xno; then
199 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
203 CFLAGS="$save_CFLAGS"
205 # More AIX lossage: must compile with cc_r
206 AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
211 AC_SUBST(PTHREAD_LIBS)
212 AC_SUBST(PTHREAD_CFLAGS)
215 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
216 if test x"$acx_pthread_ok" = xyes; then
217 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])