Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / adm / unix / config_files / thread.m4
1 # Check presence of "threads" 
2 #
3 # Author : Anthony GEAY (CEA, 2006)
4 #
5
6 AC_DEFUN([CHECK_THREAD],[
7
8 AC_CHECKING(for THREADs)
9 AC_LANG_SAVE
10 AC_LANG_CPLUSPLUS
11 thread_ok=no
12
13 AC_SUBST(THREAD_DEF)
14 if test `uname` = "Linux"
15 then
16    THREAD_DEF="-DYACS_PTHREAD"
17    AC_CHECK_HEADER(pthread.h,thread_ok=yes ,thread_ok=no)
18    thread_ok="yes"
19 fi
20
21 AC_LANG_RESTORE
22 AC_MSG_RESULT(for THREADs: $thread_ok)
23
24 ])dnl
25
26 AC_DEFUN([ACX_PTHREAD], [
27 AC_REQUIRE([AC_CANONICAL_HOST])
28 AC_LANG_SAVE
29 AC_LANG_C
30 acx_pthread_ok=no
31
32 # We used to check for pthread.h first, but this fails if pthread.h
33 # requires special compiler flags (e.g. on True64 or Sequent).
34 # It gets checked for in the link test anyway.
35
36 # First of all, check if the user has set any of the PTHREAD_LIBS,
37 # etcetera environment variables, and if threads linking works using
38 # them:
39 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
40         save_CFLAGS="$CFLAGS"
41         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
42         save_LIBS="$LIBS"
43         LIBS="$PTHREAD_LIBS $LIBS"
44         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
45         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
46         AC_MSG_RESULT($acx_pthread_ok)
47         if test x"$acx_pthread_ok" = xno; then
48                 PTHREAD_LIBS=""
49                 PTHREAD_CFLAGS=""
50         fi
51         LIBS="$save_LIBS"
52         CFLAGS="$save_CFLAGS"
53 fi
54
55 # We must check for the threads library under a number of different
56 # names; the ordering is very important because some systems
57 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
58 # libraries is broken (non-POSIX).
59
60 # Create a list of thread flags to try.  Items starting with a "-" are
61 # C compiler flags, and other items are library names, except for "none"
62 # which indicates that we try without any flags at all, and "pthread-config"
63 # which is a program returning the flags for the Pth emulation library.
64
65 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
66
67 # The ordering *is* (sometimes) important.  Some notes on the
68 # individual items follow:
69
70 # pthreads: AIX (must check this before -lpthread)
71 # none: in case threads are in libc; should be tried before -Kthread and
72 #       other compiler flags to prevent continual compiler warnings
73 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
74 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
75 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
76 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
77 # -pthreads: Solaris/gcc
78 # -mthreads: Mingw32/gcc, Lynx/gcc
79 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
80 #      doesn't hurt to check since this sometimes defines pthreads too;
81 #      also defines -D_REENTRANT)
82 # pthread: Linux, etcetera
83 # --thread-safe: KAI C++
84 # pthread-config: use pthread-config program (for GNU Pth library)
85
86 case "${host_cpu}-${host_os}" in
87         *linux*)
88
89         # On linux "none" works, but boost requires -pthread, so, try first "-pthread"
90 acx_pthread_flags="-pthread pthreads none -Kthread -kthread lthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
91         ;;
92
93         *solaris*)
94
95         # On Solaris (at least, for some versions), libc contains stubbed
96         # (non-functional) versions of the pthreads routines, so link-based
97         # tests will erroneously succeed.  (We need to link with -pthread or
98         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
99         # a function called by this macro, so we could check for that, but
100         # who knows whether they'll stub that too in a future libc.)  So,
101         # we'll just look for -pthreads and -lpthread first:
102
103         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
104         ;;
105 esac
106
107 if test x"$acx_pthread_ok" = xno; then
108 for flag in $acx_pthread_flags; do
109
110         case $flag in
111                 none)
112                 AC_MSG_CHECKING([whether pthreads work without any flags])
113                 ;;
114
115                 -*)
116                 AC_MSG_CHECKING([whether pthreads work with $flag])
117                 PTHREAD_CFLAGS="$flag"
118                 ;;
119
120                 pthread-config)
121                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
122                 if test x"$acx_pthread_config" = xno; then continue; fi
123                 PTHREAD_CFLAGS="`pthread-config --cflags`"
124                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
125                 ;;
126
127                 *)
128                 AC_MSG_CHECKING([for the pthreads library -l$flag])
129                 PTHREAD_LIBS="-l$flag"
130                 ;;
131         esac
132
133         save_LIBS="$LIBS"
134         save_CFLAGS="$CFLAGS"
135         LIBS="$PTHREAD_LIBS $LIBS"
136         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
137
138         # Check for various functions.  We must include pthread.h,
139         # since some functions may be macros.  (On the Sequent, we
140         # need a special flag -Kthread to make this header compile.)
141         # We check for pthread_join because it is in -lpthread on IRIX
142         # while pthread_create is in libc.  We check for pthread_attr_init
143         # due to DEC craziness with -lpthreads.  We check for
144         # pthread_cleanup_push because it is one of the few pthread
145         # functions on Solaris that doesn't have a non-functional libc stub.
146         # We try pthread_create on general principles.
147         AC_TRY_LINK([#include <pthread.h>],
148                     [pthread_t th; pthread_join(th, 0);
149                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
150                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
151                     [acx_pthread_ok=yes])
152
153         LIBS="$save_LIBS"
154         CFLAGS="$save_CFLAGS"
155
156         AC_MSG_RESULT($acx_pthread_ok)
157         if test "x$acx_pthread_ok" = xyes; then
158                 break;
159         fi
160
161         PTHREAD_LIBS=""
162         PTHREAD_CFLAGS=""
163 done
164 fi
165
166 # Various other checks:
167 if test "x$acx_pthread_ok" = xyes; then
168         save_LIBS="$LIBS"
169         LIBS="$PTHREAD_LIBS $LIBS"
170         save_CFLAGS="$CFLAGS"
171         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
172
173         # Detect AIX lossage: threads are created detached by default
174         # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
175         AC_MSG_CHECKING([for joinable pthread attribute])
176         AC_TRY_LINK([#include <pthread.h>],
177                     [int attr=PTHREAD_CREATE_JOINABLE;],
178                     ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
179         if test x"$ok" = xunknown; then
180                 AC_TRY_LINK([#include <pthread.h>],
181                             [int attr=PTHREAD_CREATE_UNDETACHED;],
182                             ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
183         fi
184         if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
185                 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
186                           [Define to the necessary symbol if this constant
187                            uses a non-standard name on your system.])
188         fi
189         AC_MSG_RESULT(${ok})
190         if test x"$ok" = xunknown; then
191                 AC_MSG_WARN([we do not know how to create joinable pthreads])
192         fi
193
194         AC_MSG_CHECKING([if more special flags are required for pthreads])
195         flag=no
196         case "${host_cpu}-${host_os}" in
197                 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
198                 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
199         esac
200         AC_MSG_RESULT(${flag})
201         if test "x$flag" != xno; then
202                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
203         fi
204
205         LIBS="$save_LIBS"
206         CFLAGS="$save_CFLAGS"
207
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"
212 fi
213
214 AC_SUBST(PTHREAD_LIBS)
215 AC_SUBST(PTHREAD_CFLAGS)
216 AC_SUBST(PTHREAD_CC)
217
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
225 fi
226 AC_LANG_RESTORE
227 ])dnl ACX_PTHREAD
228
229
230 AC_DEFUN([ENABLE_PTHREADS],[
231 AC_REQUIRE([ACX_PTHREAD])
232
233 if test x"$enable_pthreads_done" != xyes; then
234   if test x"$acx_pthread_ok" = xyes; then
235     CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
236     CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
237     LIBS="$LIBS $PTHREAD_LIBS"
238     threads_ok=yes
239   else
240     threads_ok=no
241   fi
242   enable_pthreads_done=yes
243 fi
244 ])dnl