1# URL:     http://autoconf-archive.cryp.to/acx_pthread.html
2# Author:  Steven G. Johnson <stevenj@alum.mit.edu>
3# Revised: 2007-07-29
4#
5# Copyright �� 2007 Steven G. Johnson <stevenj@alum.mit.edu>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12
13AC_DEFUN([ACX_PTHREAD], [
14AC_REQUIRE([AC_CANONICAL_HOST])
15AC_LANG_SAVE
16AC_LANG_C
17acx_pthread_ok=no
18
19# We used to check for pthread.h first, but this fails if pthread.h
20# requires special compiler flags (e.g. on True64 or Sequent).
21# It gets checked for in the link test anyway.
22
23# First of all, check if the user has set any of the PTHREAD_LIBS,
24# etcetera environment variables, and if threads linking works using
25# them:
26if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
27        save_CFLAGS="$CFLAGS"
28        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
29        save_LIBS="$LIBS"
30        LIBS="$PTHREAD_LIBS $LIBS"
31        AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
32        AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
33        AC_MSG_RESULT($acx_pthread_ok)
34        if test x"$acx_pthread_ok" = xno; then
35                PTHREAD_LIBS=""
36                PTHREAD_CFLAGS=""
37        fi
38        LIBS="$save_LIBS"
39        CFLAGS="$save_CFLAGS"
40fi
41
42# We must check for the threads library under a number of different
43# names; the ordering is very important because some systems
44# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
45# libraries is broken (non-POSIX).
46
47# Create a list of thread flags to try.  Items starting with a "-" are
48# C compiler flags, and other items are library names, except for "none"
49# which indicates that we try without any flags at all, and "pthread-config"
50# which is a program returning the flags for the Pth emulation library.
51
52acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
53
54# The ordering *is* (sometimes) important.  Some notes on the
55# individual items follow:
56
57# pthreads: AIX (must check this before -lpthread)
58# none: in case threads are in libc; should be tried before -Kthread and
59#       other compiler flags to prevent continual compiler warnings
60# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
61# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
62# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
63# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
64# -pthreads: Solaris/gcc
65# -mthreads: Mingw32/gcc, Lynx/gcc
66# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
67#      doesn't hurt to check since this sometimes defines pthreads too;
68#      also defines -D_REENTRANT)
69#      ... -mt is also the pthreads flag for HP/aCC
70# pthread: Linux, etcetera
71# --thread-safe: KAI C++
72# pthread-config: use pthread-config program (for GNU Pth library)
73
74case "${host_cpu}-${host_os}" in
75        *solaris*)
76
77        # On Solaris (at least, for some versions), libc contains stubbed
78        # (non-functional) versions of the pthreads routines, so link-based
79        # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
80        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
81        # a function called by this macro, so we could check for that, but
82        # who knows whether they'll stub that too in a future libc.)  So,
83        # we'll just look for -pthreads and -lpthread first:
84
85        acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
86        ;;
87esac
88
89if test x"$acx_pthread_ok" = xno; then
90for flag in $acx_pthread_flags; do
91
92        case $flag in
93                none)
94                AC_MSG_CHECKING([whether pthreads work without any flags])
95                ;;
96
97                -*)
98                AC_MSG_CHECKING([whether pthreads work with $flag])
99                PTHREAD_CFLAGS="$flag"
100                ;;
101
102                pthread-config)
103                AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
104                if test x"$acx_pthread_config" = xno; then continue; fi
105                PTHREAD_CFLAGS="`pthread-config --cflags`"
106                PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
107                ;;
108
109                *)
110                AC_MSG_CHECKING([for the pthreads library -l$flag])
111                PTHREAD_LIBS="-l$flag"
112                ;;
113        esac
114
115        save_LIBS="$LIBS"
116        save_CFLAGS="$CFLAGS"
117        LIBS="$PTHREAD_LIBS $LIBS"
118        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
119
120        # Check for various functions.  We must include pthread.h,
121        # since some functions may be macros.  (On the Sequent, we
122        # need a special flag -Kthread to make this header compile.)
123        # We check for pthread_join because it is in -lpthread on IRIX
124        # while pthread_create is in libc.  We check for pthread_attr_init
125        # due to DEC craziness with -lpthreads.  We check for
126        # pthread_cleanup_push because it is one of the few pthread
127        # functions on Solaris that doesn't have a non-functional libc stub.
128        # We try pthread_create on general principles.
129        AC_TRY_LINK([#include <pthread.h>],
130                    [pthread_t th; pthread_join(th, 0);
131                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
132                     pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
133                    [acx_pthread_ok=yes])
134
135        LIBS="$save_LIBS"
136        CFLAGS="$save_CFLAGS"
137
138        AC_MSG_RESULT($acx_pthread_ok)
139        if test "x$acx_pthread_ok" = xyes; then
140                break;
141        fi
142
143        PTHREAD_LIBS=""
144        PTHREAD_CFLAGS=""
145done
146fi
147
148# Various other checks:
149if test "x$acx_pthread_ok" = xyes; then
150        save_LIBS="$LIBS"
151        LIBS="$PTHREAD_LIBS $LIBS"
152        save_CFLAGS="$CFLAGS"
153        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
154
155        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
156        AC_MSG_CHECKING([for joinable pthread attribute])
157        attr_name=unknown
158        for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
159            AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
160                        [attr_name=$attr; break])
161        done
162        AC_MSG_RESULT($attr_name)
163        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
164            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
165                               [Define to necessary symbol if this constant
166                                uses a non-standard name on your system.])
167        fi
168
169        AC_MSG_CHECKING([if more special flags are required for pthreads])
170        flag=no
171        case "${host_cpu}-${host_os}" in
172            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
173            *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
174        esac
175        AC_MSG_RESULT(${flag})
176        if test "x$flag" != xno; then
177            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
178        fi
179
180        LIBS="$save_LIBS"
181        CFLAGS="$save_CFLAGS"
182
183        # More AIX lossage: must compile with xlc_r or cc_r
184        if test x"$GCC" != xyes; then
185          AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
186        else
187          PTHREAD_CC=$CC
188        fi
189else
190        PTHREAD_CC="$CC"
191fi
192
193AC_SUBST(PTHREAD_LIBS)
194AC_SUBST(PTHREAD_CFLAGS)
195AC_SUBST(PTHREAD_CC)
196
197# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
198if test x"$acx_pthread_ok" = xyes; then
199        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
200        :
201else
202        acx_pthread_ok=no
203        $2
204fi
205AC_LANG_RESTORE
206])dnl ACX_PTHREAD
207
208
209