]> git.meshlink.io Git - catta/blob - common/acx_pthread.m4
* add pthreads autoconf scripts
[catta] / common / acx_pthread.m4
1 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2 dnl
3 dnl This macro figures out how to build C programs using POSIX threads.
4 dnl It sets the PTHREAD_LIBS output variable to the threads library and
5 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
6 dnl C compiler flags that are needed. (The user can also force certain
7 dnl compiler flags/libs to be tested by setting these environment
8 dnl variables.)
9 dnl
10 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
11 dnl multi-threaded programs (defaults to the value of CC otherwise).
12 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
13 dnl
14 dnl NOTE: You are assumed to not only compile your program with these
15 dnl flags, but also link it with them as well. e.g. you should link
16 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
17 dnl $LIBS
18 dnl
19 dnl If you are only building threads programs, you may wish to use
20 dnl these variables in your default LIBS, CFLAGS, and CC:
21 dnl
22 dnl        LIBS="$PTHREAD_LIBS $LIBS"
23 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
24 dnl        CC="$PTHREAD_CC"
25 dnl
26 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
27 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
28 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
29 dnl
30 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
31 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
32 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
33 dnl default action will define HAVE_PTHREAD.
34 dnl
35 dnl Please let the authors know if this macro fails on any platform, or
36 dnl if you have any other suggestions or comments. This macro was based
37 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
38 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
39 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
40 dnl We are also grateful for the helpful feedback of numerous users.
41 dnl
42 dnl @category InstalledPackages
43 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
44 dnl @version 2005-01-14
45 dnl @license GPLWithACException
46
47 AC_DEFUN([ACX_PTHREAD], [
48 AC_REQUIRE([AC_CANONICAL_HOST])
49 AC_LANG_SAVE
50 AC_LANG_C
51 acx_pthread_ok=no
52
53 # We used to check for pthread.h first, but this fails if pthread.h
54 # requires special compiler flags (e.g. on True64 or Sequent).
55 # It gets checked for in the link test anyway.
56
57 # First of all, check if the user has set any of the PTHREAD_LIBS,
58 # etcetera environment variables, and if threads linking works using
59 # them:
60 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
61         save_CFLAGS="$CFLAGS"
62         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
63         save_LIBS="$LIBS"
64         LIBS="$PTHREAD_LIBS $LIBS"
65         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
66         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
67         AC_MSG_RESULT($acx_pthread_ok)
68         if test x"$acx_pthread_ok" = xno; then
69                 PTHREAD_LIBS=""
70                 PTHREAD_CFLAGS=""
71         fi
72         LIBS="$save_LIBS"
73         CFLAGS="$save_CFLAGS"
74 fi
75
76 # We must check for the threads library under a number of different
77 # names; the ordering is very important because some systems
78 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
79 # libraries is broken (non-POSIX).
80
81 # Create a list of thread flags to try.  Items starting with a "-" are
82 # C compiler flags, and other items are library names, except for "none"
83 # which indicates that we try without any flags at all, and "pthread-config"
84 # which is a program returning the flags for the Pth emulation library.
85
86 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
87
88 # The ordering *is* (sometimes) important.  Some notes on the
89 # individual items follow:
90
91 # pthreads: AIX (must check this before -lpthread)
92 # none: in case threads are in libc; should be tried before -Kthread and
93 #       other compiler flags to prevent continual compiler warnings
94 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
95 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
96 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
97 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
98 # -pthreads: Solaris/gcc
99 # -mthreads: Mingw32/gcc, Lynx/gcc
100 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
101 #      doesn't hurt to check since this sometimes defines pthreads too;
102 #      also defines -D_REENTRANT)
103 # pthread: Linux, etcetera
104 # --thread-safe: KAI C++
105 # pthread-config: use pthread-config program (for GNU Pth library)
106
107 case "${host_cpu}-${host_os}" in
108         *solaris*)
109
110         # On Solaris (at least, for some versions), libc contains stubbed
111         # (non-functional) versions of the pthreads routines, so link-based
112         # tests will erroneously succeed.  (We need to link with -pthread or
113         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
114         # a function called by this macro, so we could check for that, but
115         # who knows whether they'll stub that too in a future libc.)  So,
116         # we'll just look for -pthreads and -lpthread first:
117
118         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
119         ;;
120 esac
121
122 if test x"$acx_pthread_ok" = xno; then
123 for flag in $acx_pthread_flags; do
124
125         case $flag in
126                 none)
127                 AC_MSG_CHECKING([whether pthreads work without any flags])
128                 ;;
129
130                 -*)
131                 AC_MSG_CHECKING([whether pthreads work with $flag])
132                 PTHREAD_CFLAGS="$flag"
133                 ;;
134
135                 pthread-config)
136                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
137                 if test x"$acx_pthread_config" = xno; then continue; fi
138                 PTHREAD_CFLAGS="`pthread-config --cflags`"
139                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
140                 ;;
141
142                 *)
143                 AC_MSG_CHECKING([for the pthreads library -l$flag])
144                 PTHREAD_LIBS="-l$flag"
145                 ;;
146         esac
147
148         save_LIBS="$LIBS"
149         save_CFLAGS="$CFLAGS"
150         LIBS="$PTHREAD_LIBS $LIBS"
151         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
152
153         # Check for various functions.  We must include pthread.h,
154         # since some functions may be macros.  (On the Sequent, we
155         # need a special flag -Kthread to make this header compile.)
156         # We check for pthread_join because it is in -lpthread on IRIX
157         # while pthread_create is in libc.  We check for pthread_attr_init
158         # due to DEC craziness with -lpthreads.  We check for
159         # pthread_cleanup_push because it is one of the few pthread
160         # functions on Solaris that doesn't have a non-functional libc stub.
161         # We try pthread_create on general principles.
162         AC_TRY_LINK([#include <pthread.h>],
163                     [pthread_t th; pthread_join(th, 0);
164                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
165                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
166                     [acx_pthread_ok=yes])
167
168         LIBS="$save_LIBS"
169         CFLAGS="$save_CFLAGS"
170
171         AC_MSG_RESULT($acx_pthread_ok)
172         if test "x$acx_pthread_ok" = xyes; then
173                 break;
174         fi
175
176         PTHREAD_LIBS=""
177         PTHREAD_CFLAGS=""
178 done
179 fi
180
181 # Various other checks:
182 if test "x$acx_pthread_ok" = xyes; then
183         save_LIBS="$LIBS"
184         LIBS="$PTHREAD_LIBS $LIBS"
185         save_CFLAGS="$CFLAGS"
186         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
187
188         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
189         AC_MSG_CHECKING([for joinable pthread attribute])
190         attr_name=unknown
191         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
192             AC_TRY_LINK([#include <pthread.h>], [int attr=$attr;],
193                         [attr_name=$attr; break])
194         done
195         AC_MSG_RESULT($attr_name)
196         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
197             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
198                                [Define to necessary symbol if this constant
199                                 uses a non-standard name on your system.])
200         fi
201
202         AC_MSG_CHECKING([if more special flags are required for pthreads])
203         flag=no
204         case "${host_cpu}-${host_os}" in
205             *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
206             *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
207         esac
208         AC_MSG_RESULT(${flag})
209         if test "x$flag" != xno; then
210             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
211         fi
212
213         LIBS="$save_LIBS"
214         CFLAGS="$save_CFLAGS"
215
216         # More AIX lossage: must compile with cc_r
217         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
218 else
219         PTHREAD_CC="$CC"
220 fi
221
222 AC_SUBST(PTHREAD_LIBS)
223 AC_SUBST(PTHREAD_CFLAGS)
224 AC_SUBST(PTHREAD_CC)
225
226 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
227 if test x"$acx_pthread_ok" = xyes; then
228         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
229         :
230 else
231         acx_pthread_ok=no
232         $2
233 fi
234 AC_LANG_RESTORE
235 ])dnl ACX_PTHREAD