Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / adm / unix / config_files / thread.m4
1 dnl  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 dnl
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.
7 dnl
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.
12 dnl
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
16 dnl
17 dnl  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 dnl
19 # Check presence of "threads" 
20 #
21 # Author : Anthony GEAY (CEA, 2006)
22 #
23
24 AC_DEFUN([CHECK_THREAD],[
25
26 AC_CHECKING(for THREADs)
27 AC_LANG_SAVE
28 AC_LANG_CPLUSPLUS
29 thread_ok=no
30
31 AC_SUBST(THREAD_DEF)
32 if test `uname` = "Linux"
33 then
34    THREAD_DEF="-DYACS_PTHREAD"
35    AC_CHECK_HEADER(pthread.h,thread_ok=yes ,thread_ok=no)
36    thread_ok="yes"
37 fi
38
39 AC_LANG_RESTORE
40 AC_MSG_RESULT(for THREADs: $thread_ok)
41
42 ])dnl
43
44 AC_DEFUN([ACX_PTHREAD], [
45 AC_REQUIRE([AC_CANONICAL_HOST])
46 AC_LANG_SAVE
47 AC_LANG_C
48 acx_pthread_ok=no
49
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.
53
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
56 # them:
57 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
58         save_CFLAGS="$CFLAGS"
59         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
60         save_LIBS="$LIBS"
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
66                 PTHREAD_LIBS=""
67                 PTHREAD_CFLAGS=""
68         fi
69         LIBS="$save_LIBS"
70         CFLAGS="$save_CFLAGS"
71 fi
72
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).
77
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.
82
83 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
84
85 # The ordering *is* (sometimes) important.  Some notes on the
86 # individual items follow:
87
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)
103
104 case "${host_cpu}-${host_os}" in
105         *linux*)
106
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"
109         ;;
110
111         *solaris*)
112
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:
120
121         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
122         ;;
123 esac
124
125 if test x"$acx_pthread_ok" = xno; then
126 for flag in $acx_pthread_flags; do
127
128         case $flag in
129                 none)
130                 AC_MSG_CHECKING([whether pthreads work without any flags])
131                 ;;
132
133                 -*)
134                 AC_MSG_CHECKING([whether pthreads work with $flag])
135                 PTHREAD_CFLAGS="$flag"
136                 ;;
137
138                 pthread-config)
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`"
143                 ;;
144
145                 *)
146                 AC_MSG_CHECKING([for the pthreads library -l$flag])
147                 PTHREAD_LIBS="-l$flag"
148                 ;;
149         esac
150
151         save_LIBS="$LIBS"
152         save_CFLAGS="$CFLAGS"
153         LIBS="$PTHREAD_LIBS $LIBS"
154         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
155
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])
170
171         LIBS="$save_LIBS"
172         CFLAGS="$save_CFLAGS"
173
174         AC_MSG_RESULT($acx_pthread_ok)
175         if test "x$acx_pthread_ok" = xyes; then
176                 break;
177         fi
178
179         PTHREAD_LIBS=""
180         PTHREAD_CFLAGS=""
181 done
182 fi
183
184 # Various other checks:
185 if test "x$acx_pthread_ok" = xyes; then
186         save_LIBS="$LIBS"
187         LIBS="$PTHREAD_LIBS $LIBS"
188         save_CFLAGS="$CFLAGS"
189         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
190
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)
201         fi
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.])
206         fi
207         AC_MSG_RESULT(${ok})
208         if test x"$ok" = xunknown; then
209                 AC_MSG_WARN([we do not know how to create joinable pthreads])
210         fi
211
212         AC_MSG_CHECKING([if more special flags are required for pthreads])
213         flag=no
214         case "${host_cpu}-${host_os}" in
215                 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
216                 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
217         esac
218         AC_MSG_RESULT(${flag})
219         if test "x$flag" != xno; then
220                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
221         fi
222
223         LIBS="$save_LIBS"
224         CFLAGS="$save_CFLAGS"
225
226         # More AIX lossage: must compile with cc_r
227         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
228 else
229         PTHREAD_CC="$CC"
230 fi
231
232 AC_SUBST(PTHREAD_LIBS)
233 AC_SUBST(PTHREAD_CFLAGS)
234 AC_SUBST(PTHREAD_CC)
235
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])
239         :
240 else
241         acx_pthread_ok=no
242         $2
243 fi
244 AC_LANG_RESTORE
245 ])dnl ACX_PTHREAD
246
247
248 AC_DEFUN([ENABLE_PTHREADS],[
249 AC_REQUIRE([ACX_PTHREAD])
250
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"
256     threads_ok=yes
257   else
258     threads_ok=no
259   fi
260   enable_pthreads_done=yes
261 fi
262 ])dnl