nanosleep: work around cygwin bug
[gnulib.git] / m4 / nanosleep.m4
1 # serial 29
2
3 dnl From Jim Meyering.
4 dnl Check for the nanosleep function.
5 dnl If not found, use the supplied replacement.
6 dnl
7
8 # Copyright (C) 1999-2001, 2003-2009 Free Software Foundation, Inc.
9
10 # This file is free software; the Free Software Foundation
11 # gives unlimited permission to copy and/or distribute it,
12 # with or without modifications, as long as this notice is preserved.
13
14 AC_DEFUN([gl_FUNC_NANOSLEEP],
15 [
16  dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
17  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
18
19  AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
20  AC_CHECK_HEADERS_ONCE([sys/time.h])
21
22  nanosleep_save_libs=$LIBS
23
24  # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
25  # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
26  LIB_NANOSLEEP=
27  AC_SUBST([LIB_NANOSLEEP])
28  AC_SEARCH_LIBS([nanosleep], [rt posix4],
29                 [test "$ac_cv_search_nanosleep" = "none required" ||
30                  LIB_NANOSLEEP=$ac_cv_search_nanosleep])
31
32  AC_REQUIRE([gl_MULTIARCH])
33  if test $APPLE_UNIVERSAL_BUILD = 1; then
34    # A universal build on Apple MacOS X platforms.
35    # The test result would be 'no (mishandles large arguments)' in 64-bit mode
36    # but 'yes' in 32-bit mode. But we need a configuration result that is
37    # valid in both modes.
38    gl_cv_func_nanosleep='no (mishandles large arguments)'
39  fi
40
41  AC_CACHE_CHECK([for working nanosleep],
42   [gl_cv_func_nanosleep],
43   [
44    AC_RUN_IFELSE(
45      [AC_LANG_SOURCE([[
46         #include <errno.h>
47         #include <limits.h>
48         #include <signal.h>
49         #if HAVE_SYS_TIME_H
50          #include <sys/time.h>
51         #endif
52         #include <time.h>
53         #include <unistd.h>
54         #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
55         #define TYPE_MAXIMUM(t) \
56           ((t) (! TYPE_SIGNED (t) \
57                 ? (t) -1 \
58                 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
59
60         static void
61         check_for_SIGALRM (int sig)
62         {
63           if (sig != SIGALRM)
64             _exit (1);
65         }
66
67         int
68         main ()
69         {
70           static struct timespec ts_sleep;
71           static struct timespec ts_remaining;
72           static struct sigaction act;
73           if (! nanosleep)
74             return 1;
75           act.sa_handler = check_for_SIGALRM;
76           sigemptyset (&act.sa_mask);
77           sigaction (SIGALRM, &act, NULL);
78           ts_sleep.tv_sec = 0;
79           ts_sleep.tv_nsec = 1;
80           alarm (1);
81           if (nanosleep (&ts_sleep, NULL) != 0)
82             return 1;
83           ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
84           ts_sleep.tv_nsec = 999999999;
85           alarm (1);
86           if (nanosleep (&ts_sleep, &ts_remaining) == -1 && errno == EINTR
87               && TYPE_MAXIMUM (time_t) - 10 < ts_remaining.tv_sec)
88             return 0;
89           return 119;
90         }]])],
91      [gl_cv_func_nanosleep=yes],
92      [case $? in dnl (
93       119) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
94       *)   gl_cv_func_nanosleep=no;;
95       esac],
96      [gl_cv_func_nanosleep=cross-compiling])
97   ])
98   if test "$gl_cv_func_nanosleep" = yes; then
99     REPLACE_NANOSLEEP=0
100   else
101     REPLACE_NANOSLEEP=1
102     if test "$gl_cv_func_nanosleep" = 'no (mishandles large arguments)'; then
103       AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
104         [Define to 1 if nanosleep mishandles large arguments.])
105     fi
106     AC_LIBOBJ([nanosleep])
107     gl_PREREQ_NANOSLEEP
108   fi
109
110  LIBS=$nanosleep_save_libs
111 ])
112
113 # Prerequisites of lib/nanosleep.c.
114 AC_DEFUN([gl_PREREQ_NANOSLEEP],
115 [
116   AC_CHECK_HEADERS_ONCE([sys/select.h])
117   gl_PREREQ_SIG_HANDLER_H
118 ])