4 dnl Check for the nanosleep function.
5 dnl If not found, use the supplied replacement.
8 # Copyright (C) 1999-2001, 2003-2012 Free Software Foundation, Inc.
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.
14 AC_DEFUN([gl_FUNC_NANOSLEEP],
16 AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
17 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
19 dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
20 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
22 AC_CHECK_HEADERS_ONCE([sys/time.h])
23 AC_REQUIRE([gl_FUNC_SELECT])
25 nanosleep_save_libs=$LIBS
27 # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
28 # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
30 AC_SUBST([LIB_NANOSLEEP])
31 AC_SEARCH_LIBS([nanosleep], [rt posix4],
32 [test "$ac_cv_search_nanosleep" = "none required" ||
33 LIB_NANOSLEEP=$ac_cv_search_nanosleep])
34 if test "x$ac_cv_search_nanosleep" != xno; then
35 dnl The system has a nanosleep function.
37 AC_REQUIRE([gl_MULTIARCH])
38 if test $APPLE_UNIVERSAL_BUILD = 1; then
39 # A universal build on Apple Mac OS X platforms.
40 # The test result would be 'no (mishandles large arguments)' in 64-bit
41 # mode but 'yes' in 32-bit mode. But we need a configuration result that
42 # is valid in both modes.
43 gl_cv_func_nanosleep='no (mishandles large arguments)'
46 AC_CACHE_CHECK([for working nanosleep],
47 [gl_cv_func_nanosleep],
59 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
60 #define TYPE_MAXIMUM(t) \
61 ((t) (! TYPE_SIGNED (t) \
63 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
66 check_for_SIGALRM (int sig)
75 static struct timespec ts_sleep;
76 static struct timespec ts_remaining;
77 static struct sigaction act;
78 /* Test for major problems first. */
81 act.sa_handler = check_for_SIGALRM;
82 sigemptyset (&act.sa_mask);
83 sigaction (SIGALRM, &act, NULL);
87 if (nanosleep (&ts_sleep, NULL) != 0)
89 /* Test for a minor problem: the handling of large arguments. */
90 ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
91 ts_sleep.tv_nsec = 999999999;
93 if (nanosleep (&ts_sleep, &ts_remaining) != -1)
97 if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
101 [gl_cv_func_nanosleep=yes],
103 4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
104 *) gl_cv_func_nanosleep=no;;
106 [case "$host_os" in dnl ((
107 linux*) # Guess it halfway works when the kernel is Linux.
108 gl_cv_func_nanosleep='guessing no (mishandles large arguments)' ;;
109 *) # If we don't know, assume the worst.
110 gl_cv_func_nanosleep='guessing no' ;;
114 case "$gl_cv_func_nanosleep" in
120 case "$gl_cv_func_nanosleep" in
121 *"mishandles large arguments"*)
122 AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
123 [Define to 1 if nanosleep mishandles large arguments.])
126 # The replacement uses select(). Add $LIBSOCKET to $LIB_NANOSLEEP.
127 for ac_lib in $LIBSOCKET; do
128 case " $LIB_NANOSLEEP " in
130 *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
140 LIBS=$nanosleep_save_libs
143 # Prerequisites of lib/nanosleep.c.
144 AC_DEFUN([gl_PREREQ_NANOSLEEP],
146 AC_CHECK_HEADERS_ONCE([sys/select.h])
147 gl_PREREQ_SIG_HANDLER_H