* m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Check for a nanosleep that
[gnulib.git] / m4 / nanosleep.m4
1 #serial 21
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, 2000, 2001, 2003, 2004, 2005, 2006, 2007 Free
9 # Software Foundation, Inc.
10
11 # This file is free software; the Free Software Foundation
12 # gives unlimited permission to copy and/or distribute it,
13 # with or without modifications, as long as this notice is preserved.
14
15 AC_DEFUN([gl_FUNC_NANOSLEEP],
16 [
17  dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
18  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
19
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_CACHE_CHECK([for working nanosleep],
32   [gl_cv_func_nanosleep],
33   [
34    AC_RUN_IFELSE(
35      [AC_LANG_SOURCE([[
36         #include <errno.h>
37         #include <limits.h>
38         #include <signal.h>
39         #if HAVE_SYS_TIME_H
40          #include <sys/time.h>
41         #endif
42         #include <time.h>
43         #include <unistd.h>
44         #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
45         #define TYPE_MAXIMUM(t) \
46           ((t) (! TYPE_SIGNED (t) \
47                 ? (t) -1 \
48                 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
49
50         static void
51         check_for_SIGALRM (int sig)
52         {
53           if (sig != SIGALRM)
54             _exit (1);
55         }
56
57         int
58         main ()
59         {
60           static struct timespec ts_sleep;
61           static struct timespec ts_remaining;
62           static struct sigaction act;
63           if (! nanosleep)
64             return 1;
65           act.sa_handler = check_for_SIGALRM;
66           sigemptyset (&act.sa_mask);
67           sigaction (SIGALRM, &act, NULL);
68           ts_sleep.tv_sec = 0;
69           ts_sleep.tv_nsec = 1;
70           alarm (1);
71           if (nanosleep (&ts_sleep, NULL) != 0)
72             return 1;
73           ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
74           ts_sleep.tv_nsec = 999999999;
75           alarm (1);
76           if (nanosleep (&ts_sleep, &ts_remaining) == -1 && errno == EINTR
77               && TYPE_MAXIMUM (time_t) - 10 < ts_remaining.tv_sec)
78             return 0;
79           return 119;
80         }]])],
81      [gl_cv_func_nanosleep=yes],
82      [case $? in dnl (
83       119) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
84       *)   gl_cv_func_nanosleep=no;;
85       esac],
86      [gl_cv_func_nanosleep=cross-compiling])
87   ])
88   if test "$gl_cv_func_nanosleep" = yes; then
89     REPLACE_NANOSLEEP=0
90   else
91     REPLACE_NANOSLEEP=1
92     if test "$gl_cv_func_nanosleep" = 'no (mishandles large arguments)'; then
93       AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], 1,
94         [Define to 1 if nanosleep mishandle large arguments.])
95       for ac_lib in $LIB_CLOCK_GETTIME; do
96         case " $LIB_NANOSLEEP " in
97         *" $ac_lib "*) ;;
98         *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
99         esac
100       done
101     fi
102     AC_LIBOBJ(nanosleep)
103     gl_PREREQ_NANOSLEEP
104   fi
105
106  AC_SUBST([LIB_NANOSLEEP])
107  LIBS=$nanosleep_save_libs
108 ])
109
110 # Prerequisites of lib/nanosleep.c.
111 AC_DEFUN([gl_PREREQ_NANOSLEEP],
112 [
113   AC_CHECK_FUNCS_ONCE(siginterrupt)
114   AC_CHECK_HEADERS_ONCE(sys/select.h)
115 ])