public software.sextractor

[/] [trunk/] [acx_pthread.m4] - Blame information for rev 235

Details | Compare with Previous | View Log

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