1# lock.m4 serial 8 (gettext-0.18)
2dnl Copyright (C) 2005-2009 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Bruno Haible.
8
9dnl GST_LOCK
10dnl -------
11dnl Tests for a multithreading library to be used.
12dnl Defines at most one of the macros USE_POSIX_THREADS or USE_WIN32_THREADS.
13dnl Sets the variables LIBTHREAD to the linker options for use in a Makefile.
14dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
15dnl multithread-safe programs.
16
17AC_DEFUN([GST_LOCK],
18[
19  AC_REQUIRE([AC_CANONICAL_HOST])
20  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
21
22  # Prerequisites of lib-src/lock.c.
23  AC_REQUIRE([AC_C_INLINE])
24
25  gst_threads_api=none
26  LIBTHREAD=
27
28  dnl Check for multithreading.
29  # Some systems optimize for single-threaded programs by default, and
30  # need special flags to disable these optimizations. For example, the
31  # definition of 'errno' in <errno.h>.
32  # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
33  # groks <pthread.h>. cc also understands the flag -pthread, but
34  # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
35  # 2. putting a flag into CPPFLAGS that has an effect on the linker
36  # causes the AC_TRY_LINK test below to succeed unexpectedly,
37  # leading to wrong values of LIBTHREAD.
38  # On Solaris and HP-UX, most pthread functions exist also in libc.
39  # However, pthread_create from libc will fail.
40  case "$host_os" in
41    osf*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
42    aix*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
43    freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" LIBTHREAD=-pthread ;;
44    solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" LIBTHREAD=-lpthread ;;
45    hpux*) LIBTHREAD=-lpthread ;;
46  esac
47
48  AC_CHECK_HEADER(pthread.h, gst_have_pthread_h=yes, gst_have_pthread_h=no)
49
50  if { case "$host_os" in
51         mingw*) true;;
52         *) false;;
53       esac
54     }; then
55    gst_threads_api=win32
56    AC_DEFINE([USE_WIN32_THREADS], 1,
57      [Define if the Win32 multithreading API can be used.])
58  elif test "$gst_have_pthread_h" = yes; then
59    # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
60    # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
61    # the second one only in libpthread.  FreeBSD has libc_r.
62    gst_save_CFLAGS=$CFLAGS
63    gst_save_LIBS=$LIBS
64    CFLAGS="$CFLAGS $LIBTHREAD"
65    gst_have_pthread=yes
66    AC_SEARCH_LIBS([pthread_mutexattr_init], [pthread pthreads c_r], ,
67      [gst_have_pthread=no])
68    AC_SEARCH_LIBS([pthread_mutex_lock], [pthread pthreads c_r], ,
69      [gst_have_pthread=no])
70    CFLAGS=$gst_save_CFLAGS
71    LIBS=$gst_save_LIBS
72    if test "$gst_have_pthread" = yes; then
73      if test "$ac_cv_search_pthread_mutex_lock" != "none required"; then
74        LIBTHREAD="$LIBTHREAD $ac_cv_search_pthread_mutex_lock"
75      fi
76      gst_threads_api=posix
77      AC_DEFINE([USE_POSIX_THREADS], 1,
78        [Define if the POSIX multithreading library can be used.])
79      # OSF/1 4.0 and MacOS X 10.1 lack the pthread_rwlock_t type and the
80      # pthread_rwlock_* functions.
81      AC_CHECK_TYPE([pthread_rwlock_t],
82        [AC_DEFINE([HAVE_PTHREAD_RWLOCK], 1,
83           [Define if the POSIX multithreading library has read/write locks.])],
84        [],
85        [#include <pthread.h>])
86      # glibc defines PTHREAD_MUTEX_RECURSIVE as enum, not as a macro.
87      AC_TRY_COMPILE([#include <pthread.h>],
88        [#if __FreeBSD__ == 4
89error "No, in FreeBSD 4.0 recursive mutexes actually don't work."
90#else
91int x = (int)PTHREAD_MUTEX_RECURSIVE;
92return !x;
93#endif],
94        [AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE], 1,
95           [Define if the <pthread.h> defines PTHREAD_MUTEX_RECURSIVE.])])
96    fi
97  fi
98  AC_MSG_CHECKING([for multithread API to use])
99  AC_MSG_RESULT([$gst_threads_api])
100  AC_SUBST(LIBTHREAD)
101])
102
103
104dnl Survey of platforms:
105dnl
106dnl Platform          Available   Compiler    Supports   test-lock
107dnl                   flavours    option      weak       result
108dnl ---------------   ---------   ---------   --------   ---------
109dnl Linux 2.4/glibc   posix       -lpthread       Y      OK
110dnl
111dnl GNU Hurd/glibc    posix
112dnl
113dnl FreeBSD 5.3       posix       -lc_r           Y
114dnl                   posix       -lkse ?         Y
115dnl                   posix       -lpthread ?     Y
116dnl                   posix       -lthr           Y
117dnl
118dnl FreeBSD 5.2       posix       -lc_r           Y
119dnl                   posix       -lkse           Y
120dnl                   posix       -lthr           Y
121dnl
122dnl FreeBSD 4.0,4.10  posix       -lc_r           Y      OK
123dnl
124dnl NetBSD 1.6        --
125dnl
126dnl OpenBSD 3.4       posix       -lpthread       Y      OK
127dnl
128dnl MacOS X 10.[123]  posix       -lpthread       Y      OK
129dnl
130dnl Solaris 7,8,9     posix       -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
131dnl                   solaris     -lthread        Y      Sol 7,8: 0.0; Sol 9: OK
132dnl
133dnl HP-UX 11          posix       -lpthread       N (cc) OK
134dnl                                               Y (gcc)
135dnl
136dnl IRIX 6.5          posix       -lpthread       Y      0.5
137dnl
138dnl AIX 4.3,5.1       posix       -lpthread       N      AIX 4: 0.5; AIX 5: OK
139dnl
140dnl OSF/1 4.0,5.1     posix       -pthread (cc)   N      OK
141dnl                               -lpthread (gcc) Y
142dnl
143dnl Cygwin            posix       -lpthread       Y      OK
144dnl
145dnl Mingw             win32                       N      OK
146dnl
147dnl BeOS 5            --
148dnl
149dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
150dnl turned off:
151dnl   OK if all three tests terminate OK,
152dnl   0.5 if the first test terminates OK but the second one loops endlessly,
153dnl   0.0 if the first test already loops endlessly.
154