7f08744ac0a6aac9bc019cd28f8404a50a23c111
[gnulib.git] / m4 / nanosleep.m4
1 # serial 27
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_REQUIRE([gl_CLOCK_TIME])
21  AC_CHECK_HEADERS_ONCE([sys/time.h])
22
23  nanosleep_save_libs=$LIBS
24
25  # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
26  # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
27  AC_SEARCH_LIBS([nanosleep], [rt posix4],
28                 [test "$ac_cv_search_nanosleep" = "none required" ||
29                  LIB_NANOSLEEP=$ac_cv_search_nanosleep])
30
31  AC_REQUIRE([gl_MULTIARCH])
32  if test $APPLE_UNIVERSAL_BUILD = 1; then
33    # A universal build on Apple MacOS X platforms.
34    # The test result would be 'no (mishandles large arguments)' in 64-bit mode
35    # but 'yes' in 32-bit mode. But we need a configuration result that is
36    # valid in both modes.
37    gl_cv_func_nanosleep='no (mishandles large arguments)'
38  fi
39
40  AC_CACHE_CHECK([for working nanosleep],
41   [gl_cv_func_nanosleep],
42   [
43    AC_RUN_IFELSE(
44      [AC_LANG_SOURCE([[
45         #include <errno.h>
46         #include <limits.h>
47         #include <signal.h>
48         #if HAVE_SYS_TIME_H
49          #include <sys/time.h>
50         #endif
51         #include <time.h>
52         #include <unistd.h>
53         #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
54         #define TYPE_MAXIMUM(t) \
55           ((t) (! TYPE_SIGNED (t) \
56                 ? (t) -1 \
57                 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
58
59         static void
60         check_for_SIGALRM (int sig)
61         {
62           if (sig != SIGALRM)
63             _exit (1);
64         }
65
66         int
67         main ()
68         {
69           static struct timespec ts_sleep;
70           static struct timespec ts_remaining;
71           static struct sigaction act;
72           if (! nanosleep)
73             return 1;
74           act.sa_handler = check_for_SIGALRM;
75           sigemptyset (&act.sa_mask);
76           sigaction (SIGALRM, &act, NULL);
77           ts_sleep.tv_sec = 0;
78           ts_sleep.tv_nsec = 1;
79           alarm (1);
80           if (nanosleep (&ts_sleep, NULL) != 0)
81             return 1;
82           ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
83           ts_sleep.tv_nsec = 999999999;
84           alarm (1);
85           if (nanosleep (&ts_sleep, &ts_remaining) == -1 && errno == EINTR
86               && TYPE_MAXIMUM (time_t) - 10 < ts_remaining.tv_sec)
87             return 0;
88           return 119;
89         }]])],
90      [gl_cv_func_nanosleep=yes],
91      [case $? in dnl (
92       119) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
93       *)   gl_cv_func_nanosleep=no;;
94       esac],
95      [gl_cv_func_nanosleep=cross-compiling])
96   ])
97   if test "$gl_cv_func_nanosleep" = yes; then
98     REPLACE_NANOSLEEP=0
99   else
100     REPLACE_NANOSLEEP=1
101     if test "$gl_cv_func_nanosleep" = 'no (mishandles large arguments)'; then
102       AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
103         [Define to 1 if nanosleep mishandles large arguments.])
104       for ac_lib in $LIB_CLOCK_GETTIME; do
105         case " $LIB_NANOSLEEP " in
106         *" $ac_lib "*) ;;
107         *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
108         esac
109       done
110     fi
111     AC_LIBOBJ([nanosleep])
112     gl_PREREQ_NANOSLEEP
113   fi
114
115  AC_SUBST([LIB_NANOSLEEP])
116  LIBS=$nanosleep_save_libs
117 ])
118
119 # Prerequisites of lib/nanosleep.c.
120 AC_DEFUN([gl_PREREQ_NANOSLEEP],
121 [
122   AC_CHECK_HEADERS_ONCE([sys/select.h])
123   gl_PREREQ_SIG_HANDLER_H
124 ])