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