4 dnl Check for the nanosleep function.
5 dnl If not found, use the supplied replacement.
8 # Copyright (C) 1999-2001, 2003-2020 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 AC_CHECK_DECLS_ONCE([alarm])
27 nanosleep_save_libs=$LIBS
29 # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
30 # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
32 AC_SUBST([LIB_NANOSLEEP])
33 AC_SEARCH_LIBS([nanosleep], [rt posix4],
34 [test "$ac_cv_search_nanosleep" = "none required" ||
35 LIB_NANOSLEEP=$ac_cv_search_nanosleep])
36 if test "x$ac_cv_search_nanosleep" != xno; then
37 dnl The system has a nanosleep function.
39 AC_REQUIRE([gl_MULTIARCH])
40 if test $APPLE_UNIVERSAL_BUILD = 1; then
41 # A universal build on Apple Mac OS X platforms.
42 # The test result would be 'no (mishandles large arguments)' in 64-bit
43 # mode but 'yes' in 32-bit mode. But we need a configuration result that
44 # is valid in both modes.
45 gl_cv_func_nanosleep='no (mishandles large arguments)'
48 AC_CACHE_CHECK([for working nanosleep],
49 [gl_cv_func_nanosleep],
61 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
62 #define TYPE_MAXIMUM(t) \
63 ((t) (! TYPE_SIGNED (t) \
65 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
69 check_for_SIGALRM (int sig)
79 static struct timespec ts_sleep;
80 static struct timespec ts_remaining;
81 /* Test for major problems first. */
88 static struct sigaction act;
89 act.sa_handler = check_for_SIGALRM;
90 sigemptyset (&act.sa_mask);
91 sigaction (SIGALRM, &act, NULL);
93 if (nanosleep (&ts_sleep, NULL) != 0)
95 /* Test for a minor problem: the handling of large arguments. */
96 ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
97 ts_sleep.tv_nsec = 999999999;
99 if (nanosleep (&ts_sleep, &ts_remaining) != -1)
103 if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
106 #else /* A simpler test for native Windows. */
107 if (nanosleep (&ts_sleep, &ts_remaining) < 0)
112 [gl_cv_func_nanosleep=yes],
114 4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
115 *) gl_cv_func_nanosleep=no;;
117 [case "$host_os" in dnl ((
118 linux*) # Guess it halfway works when the kernel is Linux.
119 gl_cv_func_nanosleep='guessing no (mishandles large arguments)' ;;
120 mingw*) # Guess no on native Windows.
121 gl_cv_func_nanosleep='guessing no' ;;
122 *) # If we don't know, obey --enable-cross-guesses.
123 gl_cv_func_nanosleep="$gl_cross_guess_normal" ;;
127 case "$gl_cv_func_nanosleep" in
133 case "$gl_cv_func_nanosleep" in
134 *"mishandles large arguments"*)
135 AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
136 [Define to 1 if nanosleep mishandles large arguments.])
139 # The replacement uses select(). Add $LIBSOCKET to $LIB_NANOSLEEP.
140 for ac_lib in $LIBSOCKET; do
141 case " $LIB_NANOSLEEP " in
143 *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
153 LIBS=$nanosleep_save_libs
156 # Prerequisites of lib/nanosleep.c.
157 AC_DEFUN([gl_PREREQ_NANOSLEEP],
159 AC_CHECK_HEADERS_ONCE([sys/select.h])
160 gl_PREREQ_SIG_HANDLER_H