Salome HOME
Update copyrights
[modules/jobmanager.git] / m4 / acx_pthread.m4
1 dnl Copyright (C) 2009-2019  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, 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 dnl Paul RASCLE: modification for Linux: -pthread required by boost...
21 dnl Available from the GNU Autoconf Macro Archive at:
22 dnl
23 AC_DEFUN([ACX_PTHREAD], [
24 AC_REQUIRE([AC_CANONICAL_HOST])
25 AC_LANG_SAVE
26 AC_LANG_C
27 acx_pthread_ok=no
28
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.
32
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
35 # them:
36 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
37         save_CFLAGS="$CFLAGS"
38         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
39         save_LIBS="$LIBS"
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
45                 PTHREAD_LIBS=""
46                 PTHREAD_CFLAGS=""
47         fi
48         LIBS="$save_LIBS"
49         CFLAGS="$save_CFLAGS"
50 fi
51
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).
56
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.
61
62 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
63
64 # The ordering *is* (sometimes) important.  Some notes on the
65 # individual items follow:
66
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)
82
83 case "${host_cpu}-${host_os}" in
84         *linux*)
85
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"
88         ;;
89
90         *solaris*)
91
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:
99
100         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
101         ;;
102 esac
103
104 if test x"$acx_pthread_ok" = xno; then
105 for flag in $acx_pthread_flags; do
106
107         case $flag in
108                 none)
109                 AC_MSG_CHECKING([whether pthreads work without any flags])
110                 ;;
111
112                 -*)
113                 AC_MSG_CHECKING([whether pthreads work with $flag])
114                 PTHREAD_CFLAGS="$flag"
115                 ;;
116
117                 pthread-config)
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`"
122                 ;;
123
124                 *)
125                 AC_MSG_CHECKING([for the pthreads library -l$flag])
126                 PTHREAD_LIBS="-l$flag"
127                 ;;
128         esac
129
130         save_LIBS="$LIBS"
131         save_CFLAGS="$CFLAGS"
132         LIBS="$PTHREAD_LIBS $LIBS"
133         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
134
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])
149
150         LIBS="$save_LIBS"
151         CFLAGS="$save_CFLAGS"
152
153         AC_MSG_RESULT($acx_pthread_ok)
154         if test "x$acx_pthread_ok" = xyes; then
155                 break;
156         fi
157
158         PTHREAD_LIBS=""
159         PTHREAD_CFLAGS=""
160 done
161 fi
162
163 # Various other checks:
164 if test "x$acx_pthread_ok" = xyes; then
165         save_LIBS="$LIBS"
166         LIBS="$PTHREAD_LIBS $LIBS"
167         save_CFLAGS="$CFLAGS"
168         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
169
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)
180         fi
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.])
185         fi
186         AC_MSG_RESULT(${ok})
187         if test x"$ok" = xunknown; then
188                 AC_MSG_WARN([we do not know how to create joinable pthreads])
189         fi
190
191         AC_MSG_CHECKING([if more special flags are required for pthreads])
192         flag=no
193         case "${host_cpu}-${host_os}" in
194                 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
195                 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
196         esac
197         AC_MSG_RESULT(${flag})
198         if test "x$flag" != xno; then
199                 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
200         fi
201
202         LIBS="$save_LIBS"
203         CFLAGS="$save_CFLAGS"
204
205         # More AIX lossage: must compile with cc_r
206         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
207 else
208         PTHREAD_CC="$CC"
209 fi
210
211 AC_SUBST(PTHREAD_LIBS)
212 AC_SUBST(PTHREAD_CFLAGS)
213 AC_SUBST(PTHREAD_CC)
214
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])
218         :
219 else
220         acx_pthread_ok=no
221         $2
222 fi
223 AC_LANG_RESTORE
224 ])dnl ACX_PTHREAD