A bug fix for Windows: Use GetSystemTimeAsFileTime instead of GetSystemTime. SYSTEMTI...
[platform/upstream/glog.git] / m4 / acx_pthread.m4
1 # This was retrieved from
2 #    http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/common/acx_pthread.m4?rev=1227
3 # See also (perhaps for new versions?)
4 #    http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/common/acx_pthread.m4
5
6 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
7 dnl
8 dnl @summary figure out how to build C programs using POSIX threads
9 dnl
10 dnl This macro figures out how to build C programs using POSIX threads.
11 dnl It sets the PTHREAD_LIBS output variable to the threads library and
12 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
13 dnl C compiler flags that are needed. (The user can also force certain
14 dnl compiler flags/libs to be tested by setting these environment
15 dnl variables.)
16 dnl
17 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
18 dnl multi-threaded programs (defaults to the value of CC otherwise).
19 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
20 dnl
21 dnl NOTE: You are assumed to not only compile your program with these
22 dnl flags, but also link it with them as well. e.g. you should link
23 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
24 dnl $LIBS
25 dnl
26 dnl If you are only building threads programs, you may wish to use
27 dnl these variables in your default LIBS, CFLAGS, and CC:
28 dnl
29 dnl        LIBS="$PTHREAD_LIBS $LIBS"
30 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
31 dnl        CC="$PTHREAD_CC"
32 dnl
33 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
34 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
35 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
36 dnl
37 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
38 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
39 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
40 dnl default action will define HAVE_PTHREAD.
41 dnl
42 dnl Please let the authors know if this macro fails on any platform, or
43 dnl if you have any other suggestions or comments. This macro was based
44 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
45 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
46 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
47 dnl We are also grateful for the helpful feedback of numerous users.
48 dnl
49 dnl @category InstalledPackages
50 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
51 dnl @version 2006-05-29
52 dnl @license GPLWithACException
53 dnl 
54 dnl Checks for GCC shared/pthread inconsistency based on work by
55 dnl Marcin Owsiany <marcin@owsiany.pl>
56
57
58 AC_DEFUN([ACX_PTHREAD], [
59 AC_REQUIRE([AC_CANONICAL_HOST])
60 AC_LANG_SAVE
61 AC_LANG_C
62 acx_pthread_ok=no
63
64 # We used to check for pthread.h first, but this fails if pthread.h
65 # requires special compiler flags (e.g. on True64 or Sequent).
66 # It gets checked for in the link test anyway.
67
68 # First of all, check if the user has set any of the PTHREAD_LIBS,
69 # etcetera environment variables, and if threads linking works using
70 # them:
71 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
72         save_CFLAGS="$CFLAGS"
73         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
74         save_LIBS="$LIBS"
75         LIBS="$PTHREAD_LIBS $LIBS"
76         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
77         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
78         AC_MSG_RESULT($acx_pthread_ok)
79         if test x"$acx_pthread_ok" = xno; then
80                 PTHREAD_LIBS=""
81                 PTHREAD_CFLAGS=""
82         fi
83         LIBS="$save_LIBS"
84         CFLAGS="$save_CFLAGS"
85 fi
86
87 # We must check for the threads library under a number of different
88 # names; the ordering is very important because some systems
89 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
90 # libraries is broken (non-POSIX).
91
92 # Create a list of thread flags to try.  Items starting with a "-" are
93 # C compiler flags, and other items are library names, except for "none"
94 # which indicates that we try without any flags at all, and "pthread-config"
95 # which is a program returning the flags for the Pth emulation library.
96
97 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
98
99 # The ordering *is* (sometimes) important.  Some notes on the
100 # individual items follow:
101
102 # pthreads: AIX (must check this before -lpthread)
103 # none: in case threads are in libc; should be tried before -Kthread and
104 #       other compiler flags to prevent continual compiler warnings
105 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
106 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
107 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
108 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
109 # -pthreads: Solaris/gcc
110 # -mthreads: Mingw32/gcc, Lynx/gcc
111 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
112 #      doesn't hurt to check since this sometimes defines pthreads too;
113 #      also defines -D_REENTRANT)
114 #      ... -mt is also the pthreads flag for HP/aCC
115 # pthread: Linux, etcetera
116 # --thread-safe: KAI C++
117 # pthread-config: use pthread-config program (for GNU Pth library)
118
119 case "${host_cpu}-${host_os}" in
120         *solaris*)
121
122         # On Solaris (at least, for some versions), libc contains stubbed
123         # (non-functional) versions of the pthreads routines, so link-based
124         # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
125         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
126         # a function called by this macro, so we could check for that, but
127         # who knows whether they'll stub that too in a future libc.)  So,
128         # we'll just look for -pthreads and -lpthread first:
129
130         acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
131         ;;
132 esac
133
134 if test x"$acx_pthread_ok" = xno; then
135 for flag in $acx_pthread_flags; do
136
137         case $flag in
138                 none)
139                 AC_MSG_CHECKING([whether pthreads work without any flags])
140                 ;;
141
142                 -*)
143                 AC_MSG_CHECKING([whether pthreads work with $flag])
144                 PTHREAD_CFLAGS="$flag"
145                 ;;
146
147                 pthread-config)
148                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
149                 if test x"$acx_pthread_config" = xno; then continue; fi
150                 PTHREAD_CFLAGS="`pthread-config --cflags`"
151                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
152                 ;;
153
154                 *)
155                 AC_MSG_CHECKING([for the pthreads library -l$flag])
156                 PTHREAD_LIBS="-l$flag"
157                 ;;
158         esac
159
160         save_LIBS="$LIBS"
161         save_CFLAGS="$CFLAGS"
162         LIBS="$PTHREAD_LIBS $LIBS"
163         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
164
165         # Check for various functions.  We must include pthread.h,
166         # since some functions may be macros.  (On the Sequent, we
167         # need a special flag -Kthread to make this header compile.)
168         # We check for pthread_join because it is in -lpthread on IRIX
169         # while pthread_create is in libc.  We check for pthread_attr_init
170         # due to DEC craziness with -lpthreads.  We check for
171         # pthread_cleanup_push because it is one of the few pthread
172         # functions on Solaris that doesn't have a non-functional libc stub.
173         # We try pthread_create on general principles.
174         AC_TRY_LINK([#include <pthread.h>],
175                     [pthread_t th; pthread_join(th, 0);
176                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
177                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
178                     [acx_pthread_ok=yes])
179
180         LIBS="$save_LIBS"
181         CFLAGS="$save_CFLAGS"
182
183         AC_MSG_RESULT($acx_pthread_ok)
184         if test "x$acx_pthread_ok" = xyes; then
185                 break;
186         fi
187
188         PTHREAD_LIBS=""
189         PTHREAD_CFLAGS=""
190 done
191 fi
192
193 # Various other checks:
194 if test "x$acx_pthread_ok" = xyes; then
195         save_LIBS="$LIBS"
196         LIBS="$PTHREAD_LIBS $LIBS"
197         save_CFLAGS="$CFLAGS"
198         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
199
200         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
201         AC_MSG_CHECKING([for joinable pthread attribute])
202         attr_name=unknown
203         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
204             AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
205                         [attr_name=$attr; break])
206         done
207         AC_MSG_RESULT($attr_name)
208         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
209             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
210                                [Define to necessary symbol if this constant
211                                 uses a non-standard name on your system.])
212         fi
213
214         AC_MSG_CHECKING([if more special flags are required for pthreads])
215         flag=no
216         case "${host_cpu}-${host_os}" in
217             *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
218             *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
219         esac
220         AC_MSG_RESULT(${flag})
221         if test "x$flag" != xno; then
222             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
223         fi
224
225         LIBS="$save_LIBS"
226         CFLAGS="$save_CFLAGS"
227         # More AIX lossage: must compile with xlc_r or cc_r
228         if test x"$GCC" != xyes; then
229           AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
230         else
231           PTHREAD_CC=$CC
232         fi
233
234    # The next part tries to detect GCC inconsistency with -shared on some
235    # architectures and systems. The problem is that in certain
236    # configurations, when -shared is specified, GCC "forgets" to
237    # internally use various flags which are still necessary.
238    
239    AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies])
240    check_inconsistencies=yes
241    case "${host_cpu}-${host_os}" in
242      *-darwin*) check_inconsistencies=no ;;
243    esac
244    if test x"$GCC" != xyes -o "x$check_inconsistencies" != xyes ; then
245       AC_MSG_RESULT([no])
246    else
247       AC_MSG_RESULT([yes])
248
249       # In order not to create several levels of indentation, we test
250       # the value of "$ok" until we find out the cure or run out of
251       # ideas.
252       ok="no"
253
254       #
255       # Prepare the flags
256       #
257       save_CFLAGS="$CFLAGS"
258       save_LIBS="$LIBS"
259       save_CC="$CC"
260       # Try with the flags determined by the earlier checks.
261       #
262       # -Wl,-z,defs forces link-time symbol resolution, so that the
263       # linking checks with -shared actually have any value
264       #
265       # FIXME: -fPIC is required for -shared on many architectures,
266       # so we specify it here, but the right way would probably be to
267       # properly detect whether it is actually required.
268       CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS"
269       LIBS="$PTHREAD_LIBS $LIBS"
270       CC="$PTHREAD_CC"
271
272       AC_MSG_CHECKING([whether -pthread is sufficient with -shared])
273       AC_TRY_LINK([#include <pthread.h>],
274          [pthread_t th; pthread_join(th, 0);
275          pthread_attr_init(0); pthread_cleanup_push(0, 0);
276          pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
277          [ok=yes])
278       
279       if test "x$ok" = xyes; then
280          AC_MSG_RESULT([yes])
281       else
282          AC_MSG_RESULT([no])
283       fi
284    
285       #
286       # Linux gcc on some architectures such as mips/mipsel forgets
287       # about -lpthread
288       #
289       if test x"$ok" = xno; then
290          AC_MSG_CHECKING([whether -lpthread fixes that])
291          LIBS="-lpthread $PTHREAD_LIBS $save_LIBS"
292          AC_TRY_LINK([#include <pthread.h>],
293             [pthread_t th; pthread_join(th, 0);
294             pthread_attr_init(0); pthread_cleanup_push(0, 0);
295             pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
296             [ok=yes])
297    
298          if test "x$ok" = xyes; then
299             AC_MSG_RESULT([yes])
300             PTHREAD_LIBS="-lpthread $PTHREAD_LIBS"
301          else
302             AC_MSG_RESULT([no])
303          fi
304       fi
305       #
306       # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc
307       #
308       if test x"$ok" = xno; then
309          AC_MSG_CHECKING([whether -lc_r fixes that])
310          LIBS="-lc_r $PTHREAD_LIBS $save_LIBS"
311          AC_TRY_LINK([#include <pthread.h>],
312              [pthread_t th; pthread_join(th, 0);
313               pthread_attr_init(0); pthread_cleanup_push(0, 0);
314               pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
315              [ok=yes])
316    
317          if test "x$ok" = xyes; then
318             AC_MSG_RESULT([yes])
319             PTHREAD_LIBS="-lc_r $PTHREAD_LIBS"
320          else
321             AC_MSG_RESULT([no])
322          fi
323       fi
324       if test x"$ok" = xno; then
325          # OK, we have run out of ideas
326          AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries])
327
328          # so it's not safe to assume that we may use pthreads
329          acx_pthread_ok=no
330       fi
331
332       CFLAGS="$save_CFLAGS"
333       LIBS="$save_LIBS"
334       CC="$save_CC"
335    fi
336 else
337         PTHREAD_CC="$CC"
338 fi
339
340 AC_SUBST(PTHREAD_LIBS)
341 AC_SUBST(PTHREAD_CFLAGS)
342 AC_SUBST(PTHREAD_CC)
343
344 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
345 if test x"$acx_pthread_ok" = xyes; then
346         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
347         :
348 else
349         acx_pthread_ok=no
350         $2
351 fi
352 AC_LANG_RESTORE
353 ])dnl ACX_PTHREAD