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