1 dnl -------------------------------------------------------------------------
2 dnl $Id: threads.m4 80826 2008-03-04 14:51:23Z wotte $
6 dnl ACE M4 include file which contains ACE specific M4 macros
7 dnl for configuring thread support. This file is to be used
8 dnl with the configure script.
10 dnl -------------------------------------------------------------------------
12 dnl Copyright (C) 1998, 1999, 2002 Ossama Othman
14 dnl All Rights Reserved
16 dnl This library is free software; you can redistribute it and/or
17 dnl modify it under the current ACE distribution terms.
19 dnl This library is distributed in the hope that it will be useful,
20 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
21 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 dnl Make sure thread library exists
24 dnl We need to be careful when tests for other thread libraries are
25 dnl added that we don't screw up handling of "ace_user_enable_threads"
26 dnl Tests should probably be more platform specific later on.
28 dnl Check for thread related libraries and compiler flags
29 dnl Usage: ACE_CHECK_THREADS
30 AC_DEFUN([ACE_CHECK_THREADS],
32 dnl AC_REQUIRE([AC_PROG_CXX])
33 dnl AC_REQUIRE([AC_PROG_CXXCPP])
35 dnl AC_REQUIRE([AC_LANG])
37 dnl Check if compiler accepts specific flag to enable threads
38 ACE_CACHE_CHECK([if compiler may need a command line thread flag],
39 [ace_cv_feature_may_need_thread_flag],
41 ace_save_CXXFLAGS="$CXXFLAGS"
43 ACE_CHECK_THREAD_FLAGS(
45 ace_cv_feature_may_need_thread_flag=no
48 ace_cv_feature_may_need_thread_flag=yes
50 dnl Reset the flags to a consistent state.
51 dnl This prevents duplicate flags from being added to
52 dnl the C/CXXFLAGS variable.
53 CXXFLAGS="$ace_save_CXXFLAGS"
56 dnl The compiler/platform has no thread support linked in by default
57 dnl so search for a usable compiler flag to enable thread support.
58 dnl If no thread flag is found then the remaining tests should still
59 dnl figure out how to enable thread support via library checks.
60 ACE_SEARCH_THREAD_FLAGS(
61 [mt pthread pthreads mthreads threads Kthread kthread -thread_safe],,)
62 dnl NOTE: "-thread_safe" is correct, not "thread_safe."
63 dnl KAI C++ uses the flag "--thread_safe" which is why
64 dnl "-thread_safe" is passed as the flag to test.
70 dnl Check for UNIX International Threads (ACE calls this STHREADS)
71 dnl This used to check for thr_create(), but AIX has a semi-functional
72 dnl UI Threads capability that includes thr_create(). We don't want to
73 dnl find such a half-hearted UI Threads, so this was changed to look for
74 dnl a UI Threads function that AIX doesn't offer.
75 AS_IF([test "$ace_user_enable_uithreads" = yes],
76 [ AC_MSG_CHECKING([for UNIX International threads capability])
77 AC_SEARCH_LIBS([mutex_lock], [thread],
80 AC_DEFINE([ACE_HAS_STHREADS], 1,
81 [Define to 1 if platform has UNIX International Threads])
86 dnl Sometimes thr_create is actually found with explicitly linking against
87 dnl -lthread, so try a more "exotic" function.
88 AC_SEARCH_LIBS([rwlock_destroy], [thread],[],[])
89 AC_MSG_RESULT([$ace_has_sthreads])
92 dnl Check if any thread related preprocessor flags are needed.
93 ACE_CHECK_THREAD_CPPFLAGS
95 dnl Check for POSIX threads
97 AS_IF([test "$ace_user_enable_pthreads" = yes],
98 [ AC_MSG_CHECKING([for POSIX threads library])
99 ACE_CHECK_POSIX_THREADS(
102 AC_DEFINE([ACE_HAS_PTHREADS], 1,
103 [Define to 1 if platform has POSIX threads])
104 AC_MSG_RESULT([none required])
108 AS_IF([test "$ace_has_pthreads" != yes],
110 ace_posix_threads_search_LIBS="$LIBS"
111 for ace_p in pthread pthreads c_r gthreads; do
112 LIBS="-l$ace_p $ace_posix_threads_search_LIBS"
113 ACE_CHECK_POSIX_THREADS(
116 AC_DEFINE([ACE_HAS_PTHREADS])
117 AC_MSG_RESULT([-l$ace_p])
123 AS_IF([test "$ace_has_pthreads" != yes],
126 LIBS="$ace_posix_threads_search_LIBS"
132 AC_MSG_NOTICE([Pthreads disabled by user; not checking for it])
135 dnl If we don't have any thread library, then disable threading altogether!
136 AS_IF([test "$ace_has_pthreads" != yes && test "$ace_has_sthreads" != yes],
138 ace_user_enable_threads=no
142 dnl This macro will check that the current compiler flags do something
143 dnl useful in terms of thread libraries and/or functions.
144 dnl Usage: ACE_CHECK_THREAD_FLAGS(ACTION-IF-USABLE [, ACTION-IF-NOT-USABLE]])
145 AC_DEFUN([ACE_CHECK_THREAD_FLAGS],
147 ACE_CONVERT_WARNINGS_TO_ERRORS([
148 dnl Check for UI thread support first.
150 dnl Because some platforms are brain damaged enough to provide
151 dnl useless thread function stubs, link tests may succeed despite the
152 dnl fact the stubs are no-ops. This forces us to use a run-time test
153 dnl to get around this nuisance by checking the return value of
154 dnl thr_create(). The cross-compiled case will use a link-time
156 dnl Furthermore, we need the implementation to be a real one, not
157 dnl a half-hearted attempt such as that provided on AIX 5. So, we
158 dnl make sure it can at least work with a mutex.
160 AC_RUN_IFELSE([AC_LANG_SOURCE([[
162 #if ((THR_BOUND & THR_SUSPEND & THR_DETACHED) != 0)
163 # error This is a silly UI Threads implementation.
167 ace_start_func (void *)
170 mutex_init (&m, USYNC_THREAD, NULL);
182 return thr_create (0, 0, ace_start_func, 0, 0, &tid);
187 dnl Now check for POSIX thread support.
188 ACE_CHECK_POSIX_THREADS([$1],[$2])
192 dnl UI threads cross-compiled case
194 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
198 ace_start_func (void *)
206 (void) thr_create (&tid, 0, ace_start_func, 0);
214 dnl Check what compiler thread flag may be used, if any, from the given list.
215 dnl The flag list is separated by white space.
216 dnl Usage: ACE_SEARCH_THREAD_FLAGS(THREAD-FLAG-LIST,
217 dnl [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
218 AC_DEFUN([ACE_SEARCH_THREAD_FLAGS],
221 AC_REQUIRE([AC_LANG])
223 ACE_CACHE_CHECK([for compiler thread flag],
224 [ace_cv_thread_flag_search],
226 ace_save_CXXFLAGS="$CXXFLAGS"
229 CXXFLAGS="$CXXFLAGS -$i"
231 ACE_CHECK_THREAD_FLAGS(
233 ace_cv_thread_flag_search="-$i"
235 dnl A usable flag was found so break out of the loop.
239 ace_cv_thread_flag_search=no
242 dnl Reset the flags for the next flag check.
243 CXXFLAGS="$ace_save_CXXFLAGS"
246 dnl Reset the flags to a consistent state.
247 dnl This prevents duplicate flags from being added to
248 dnl the CCXXFLAGS variable.
249 CXXFLAGS="$ace_save_CXXFLAGS"
252 dnl Add the found/cached thread flag to the C/CXXFLAGS variables
253 CXXFLAGS="$CXXFLAGS $ace_cv_thread_flag_search"
263 dnl Check if the compiler defines thread related preprocessor flags.
264 dnl If not, then provide them.
265 dnl Usage: ACE_CHECK_THREAD_CPPFLAGS
266 AC_DEFUN([ACE_CHECK_THREAD_CPPFLAGS],
268 dnl A compile-time test is used instead of a preprocesse-time test
269 dnl because compiler thread flags defined in CFLAGS or CXXFLAGS
270 dnl should be used for this test.
271 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
272 #if !defined (_REENTRANT) && !defined (_THREAD_SAFE)
273 #error Neither _REENTRANT nor _THREAD_SAFE were defined.
281 ACE_THR_CPPFLAGS="-D_REENTRANT -D_THREAD_SAFE"
285 dnl Check for POSIX threads support.
286 dnl Usage: ACE_CHECK_POSIX_THREADS([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
287 AC_DEFUN([ACE_CHECK_POSIX_THREADS],
289 dnl Because some platforms are brain damaged enough to provide
290 dnl useless thread function stubs, link tests may succeed despite the
291 dnl fact the stubs are no-ops. This forces us to use a run-time test
292 dnl to get around this nuisance by checking the return value of
293 dnl pthread_create(). The cross-compiled case will use a link-time
295 AC_RUN_IFELSE([AC_LANG_SOURCE([[
299 ace_start_func (void *)
309 return pthread_create (&tid, 0, ace_start_func, 0);
315 dnl POSIX threads check -- cross-compiled case
317 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
321 ace_start_func (void *)
329 (void) pthread_create (&tid, 0, ace_start_func, 0);