Debian Codegraph @

m4/nanosleep.m4

raw
1 # nanosleep.m4
2 # serial 47
3 dnl Copyright (C) 1999-2001, 2003-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 dnl From Jim Meyering.
10 dnl Check for the nanosleep function.
11 dnl If not found, use the supplied replacement.
12
13 AC_DEFUN([gl_FUNC_NANOSLEEP],
14 [
15 AC_REQUIRE([gl_TIME_H_DEFAULTS])
16 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17
18 dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
19 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
20
21 AC_CHECK_DECLS_ONCE([alarm])
22
23 gl_saved_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 NANOSLEEP_LIB=
28 AC_SUBST([NANOSLEEP_LIB])
29 AC_SEARCH_LIBS([nanosleep], [rt posix4],
30 [test "$ac_cv_search_nanosleep" = "none required" ||
31 NANOSLEEP_LIB=$ac_cv_search_nanosleep])
32 if test "x$ac_cv_search_nanosleep" != xno; then
33 dnl The system has a nanosleep function.
34
35 AC_REQUIRE([gl_MULTIARCH])
36 if test $APPLE_UNIVERSAL_BUILD = 1; then
37 # A universal build on Apple Mac OS X platforms.
38 # The test result would be 'no (mishandles large arguments)' in 64-bit
39 # mode but 'yes' in 32-bit mode. But we need a configuration result that
40 # is valid in both modes.
41 gl_cv_func_nanosleep='no (mishandles large arguments)'
42 fi
43
44 AC_CACHE_CHECK([for working nanosleep],
45 [gl_cv_func_nanosleep],
46 [
47 AC_RUN_IFELSE(
48 [AC_LANG_SOURCE([[
49 #include <errno.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <time.h>
53 #include <unistd.h>
54 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
55 #define TYPE_MAXIMUM(t) \
56 ((t) (! TYPE_SIGNED (t) \
57 ? (t) -1 \
58 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
59
60 #if HAVE_DECL_ALARM
61 static void
62 check_for_SIGALRM (int sig)
63 {
64 if (sig != SIGALRM)
65 _exit (1);
66 }
67 #endif
68
69 int
70 main ()
71 {
72 static struct timespec ts_sleep;
73 static struct timespec ts_remaining;
74 /* Test for major problems first. */
75 if (! nanosleep)
76 return 2;
77 ts_sleep.tv_sec = 0;
78 ts_sleep.tv_nsec = 1;
79 #if HAVE_DECL_ALARM
80 {
81 static struct sigaction act;
82 act.sa_handler = check_for_SIGALRM;
83 sigemptyset (&act.sa_mask);
84 sigaction (SIGALRM, &act, NULL);
85 alarm (1);
86 if (nanosleep (&ts_sleep, NULL) != 0)
87 return 3;
88 /* Test for a minor problem: the handling of large arguments. */
89 ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
90 ts_sleep.tv_nsec = 999999999;
91 alarm (1);
92 if (nanosleep (&ts_sleep, &ts_remaining) != -1)
93 return 4;
94 if (errno != EINTR)
95 return 5;
96 if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
97 return 6;
98 }
99 #else /* A simpler test for native Windows. */
100 if (nanosleep (&ts_sleep, &ts_remaining) < 0)
101 return 3;
102 /* Test for 32-bit mingw bug: negative nanosecond values do not
103 cause failure. */
104 ts_sleep.tv_sec = 1;
105 ts_sleep.tv_nsec = -1;
106 if (nanosleep (&ts_sleep, &ts_remaining) != -1)
107 return 7;
108 #endif
109 return 0;
110 }]])],
111 [gl_cv_func_nanosleep=yes],
112 [case $? in
113 4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)' ;;
114 7) gl_cv_func_nanosleep='no (mishandles negative tv_nsec)' ;;
115 *) gl_cv_func_nanosleep=no ;;
116 esac],
117 [case "$host_os" in
118 # Guess it halfway works when the kernel is Linux.
119 linux*)
120 gl_cv_func_nanosleep='guessing no (mishandles large arguments)' ;;
121 # Midipix generally emulates the Linux system calls,
122 # but here it handles large arguments correctly.
123 midipix*)
124 gl_cv_func_nanosleep='guessing yes' ;;
125 # Guess no on native Windows.
126 mingw* | windows*)
127 gl_cv_func_nanosleep='guessing no' ;;
128 # If we don't know, obey --enable-cross-guesses.
129 *)
130 gl_cv_func_nanosleep="$gl_cross_guess_normal" ;;
131 esac
132 ])
133 ])
134 case "$gl_cv_func_nanosleep" in
135 *yes) ;;
136 *)
137 REPLACE_NANOSLEEP=1
138 case "$gl_cv_func_nanosleep" in
139 *"mishandles large arguments"*)
140 AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
141 [Define to 1 if nanosleep mishandles large arguments.])
142 ;;
143 esac
144 ;;
145 esac
146 else
147 HAVE_NANOSLEEP=0
148 fi
149 LIBS=$gl_saved_LIBS
150
151 # For backward compatibility.
152 LIB_NANOSLEEP="$NANOSLEEP_LIB"
153 AC_SUBST([LIB_NANOSLEEP])
154 ])