pthread: work around winpthread header pollution on mingw
[gnulib.git] / m4 / pthread.m4
1 # pthread.m4 serial 8
2 dnl Copyright (C) 2009-2014 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_PTHREAD_CHECK],
8 [
9    AC_REQUIRE([gl_PTHREAD_DEFAULTS])
10    gl_CHECK_NEXT_HEADERS([pthread.h])
11    if test $ac_cv_header_pthread_h = yes; then
12      HAVE_PTHREAD_H=1
13      # mingw 3.0 uses winpthreads which installs broken macros via <pthread.h>
14      AC_CACHE_CHECK([whether <pthread.h> pollutes the namespace],
15       [gl_cv_header_pthread_h_pollution],
16       [AC_COMPILE_IFELSE(
17         [AC_LANG_PROGRAM(
18           [[#include <pthread.h>
19             #ifdef strtok_r
20             #error
21              break me
22             #endif
23           ]])],
24         [gl_cv_header_pthread_h_pollution=no],
25         [gl_cv_header_pthread_h_pollution=yes])])
26    else
27      HAVE_PTHREAD_H=0
28    fi
29
30    AC_CHECK_TYPES([pthread_t, pthread_spinlock_t], [], [],
31      [AC_INCLUDES_DEFAULT[
32       #if HAVE_PTHREAD_H
33        #include <pthread.h>
34       #endif]])
35    if test $ac_cv_type_pthread_t != yes; then
36      HAVE_PTHREAD_T=0
37    fi
38    if test $ac_cv_type_pthread_spinlock_t != yes; then
39      HAVE_PTHREAD_SPINLOCK_T=0
40    fi
41
42    if test $ac_cv_header_pthread_h != yes ||
43       test $ac_cv_type_pthread_t != yes ||
44       test $ac_cv_type_pthread_spinlock_t != yes; then
45      PTHREAD_H='pthread.h'
46      AC_LIBOBJ([pthread])
47    elif test $gl_cv_header_pthread_h_pollution = yes; then
48      PTHREAD_H=pthread.h
49    else
50      PTHREAD_H=
51    fi
52    AC_SUBST([PTHREAD_H])
53    AM_CONDITIONAL([GL_GENERATE_PTHREAD_H], [test -n "$PTHREAD_H"])
54
55    LIB_PTHREAD=
56    if test $ac_cv_header_pthread_h = yes; then
57      dnl We cannot use AC_SEARCH_LIBS here, because on OSF/1 5.1 pthread_join
58      dnl is defined as a macro which expands to __phread_join, and libpthread
59      dnl contains a definition for __phread_join but none for pthread_join.
60      dnl Also, FreeBSD 9 puts pthread_create in libpthread and pthread_join
61      dnl in libc, whereas on IRIX 6.5 the reverse is true; so check for both.
62      AC_CACHE_CHECK([for library containing pthread_create and pthread_join],
63        [gl_cv_lib_pthread],
64        [gl_saved_libs=$LIBS
65         gl_cv_lib_pthread=
66         for gl_lib_prefix in '' '-lpthread'; do
67           LIBS="$gl_lib_prefix $gl_saved_libs"
68           AC_LINK_IFELSE(
69             [AC_LANG_PROGRAM(
70                [[#include <pthread.h>
71                  void *noop (void *p) { return p; }]],
72                [[pthread_t pt;
73                  void *arg = 0;
74                  pthread_create (&pt, 0, noop, arg);
75                  pthread_join (pthread_self (), &arg);]])],
76             [if test -z "$gl_lib_prefix"; then
77                gl_cv_lib_pthread="none required"
78              else
79                gl_cv_lib_pthread=$gl_lib_prefix
80              fi])
81           test -n "$gl_cv_lib_pthread" && break
82         done
83         LIBS="$gl_saved_libs"
84        ])
85      if test "$gl_cv_lib_pthread" != "none required"; then
86        LIB_PTHREAD="$gl_cv_lib_pthread"
87      fi
88    fi
89    AC_SUBST([LIB_PTHREAD])
90
91    AC_REQUIRE([AC_C_RESTRICT])
92 ])
93
94 AC_DEFUN([gl_PTHREAD_DEFAULTS],
95 [
96   dnl Assume proper GNU behavior unless another module says otherwise.
97   HAVE_PTHREAD_H=1;              AC_SUBST([HAVE_PTHREAD_H])
98   HAVE_PTHREAD_T=1;              AC_SUBST([HAVE_PTHREAD_T])
99   HAVE_PTHREAD_SPINLOCK_T=1;     AC_SUBST([HAVE_PTHREAD_SPINLOCK_T])
100 ])