source: trunk/src/sed/m4/nanosleep.m4@ 3669

Last change on this file since 3669 was 3611, checked in by bird, 10 months ago

vendor/sed/current: GNU sed 4.9 (sed-4.9.tar.xz sha256:6e226b732e1cd739464ad6862bd1a1aba42d7982922da7a53519631d24975181)

File size: 4.8 KB
Line 
1# serial 42
2
3dnl From Jim Meyering.
4dnl Check for the nanosleep function.
5dnl If not found, use the supplied replacement.
6dnl
7
8# Copyright (C) 1999-2001, 2003-2022 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
14AC_DEFUN([gl_FUNC_NANOSLEEP],
15[
16 AC_REQUIRE([gl_TIME_H_DEFAULTS])
17 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
18
19 dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
20 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
21
22 AC_CHECK_DECLS_ONCE([alarm])
23
24 nanosleep_save_libs=$LIBS
25
26 # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
27 # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
28 LIB_NANOSLEEP=
29 AC_SUBST([LIB_NANOSLEEP])
30 AC_SEARCH_LIBS([nanosleep], [rt posix4],
31 [test "$ac_cv_search_nanosleep" = "none required" ||
32 LIB_NANOSLEEP=$ac_cv_search_nanosleep])
33 if test "x$ac_cv_search_nanosleep" != xno; then
34 dnl The system has a nanosleep function.
35
36 AC_REQUIRE([gl_MULTIARCH])
37 if test $APPLE_UNIVERSAL_BUILD = 1; then
38 # A universal build on Apple Mac OS X platforms.
39 # The test result would be 'no (mishandles large arguments)' in 64-bit
40 # mode but 'yes' in 32-bit mode. But we need a configuration result that
41 # is valid in both modes.
42 gl_cv_func_nanosleep='no (mishandles large arguments)'
43 fi
44
45 AC_CACHE_CHECK([for working nanosleep],
46 [gl_cv_func_nanosleep],
47 [
48 AC_RUN_IFELSE(
49 [AC_LANG_SOURCE([[
50 #include <errno.h>
51 #include <limits.h>
52 #include <signal.h>
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) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
60
61 #if HAVE_DECL_ALARM
62 static void
63 check_for_SIGALRM (int sig)
64 {
65 if (sig != SIGALRM)
66 _exit (1);
67 }
68 #endif
69
70 int
71 main ()
72 {
73 static struct timespec ts_sleep;
74 static struct timespec ts_remaining;
75 /* Test for major problems first. */
76 if (! nanosleep)
77 return 2;
78 ts_sleep.tv_sec = 0;
79 ts_sleep.tv_nsec = 1;
80 #if HAVE_DECL_ALARM
81 {
82 static struct sigaction act;
83 act.sa_handler = check_for_SIGALRM;
84 sigemptyset (&act.sa_mask);
85 sigaction (SIGALRM, &act, NULL);
86 alarm (1);
87 if (nanosleep (&ts_sleep, NULL) != 0)
88 return 3;
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;
92 alarm (1);
93 if (nanosleep (&ts_sleep, &ts_remaining) != -1)
94 return 4;
95 if (errno != EINTR)
96 return 5;
97 if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
98 return 6;
99 }
100 #else /* A simpler test for native Windows. */
101 if (nanosleep (&ts_sleep, &ts_remaining) < 0)
102 return 3;
103 /* Test for 32-bit mingw bug: negative nanosecond values do not
104 cause failure. */
105 ts_sleep.tv_sec = 1;
106 ts_sleep.tv_nsec = -1;
107 if (nanosleep (&ts_sleep, &ts_remaining) != -1)
108 return 7;
109 #endif
110 return 0;
111 }]])],
112 [gl_cv_func_nanosleep=yes],
113 [case $? in
114 4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)' ;;
115 7) gl_cv_func_nanosleep='no (mishandles negative tv_nsec)' ;;
116 *) gl_cv_func_nanosleep=no ;;
117 esac],
118 [case "$host_os" in
119 linux*) # Guess it halfway works when the kernel is Linux.
120 gl_cv_func_nanosleep='guessing no (mishandles large arguments)' ;;
121 mingw*) # Guess no on native Windows.
122 gl_cv_func_nanosleep='guessing no' ;;
123 *) # If we don't know, obey --enable-cross-guesses.
124 gl_cv_func_nanosleep="$gl_cross_guess_normal" ;;
125 esac
126 ])
127 ])
128 case "$gl_cv_func_nanosleep" in
129 *yes)
130 REPLACE_NANOSLEEP=0
131 ;;
132 *)
133 REPLACE_NANOSLEEP=1
134 case "$gl_cv_func_nanosleep" in
135 *"mishandles large arguments"*)
136 AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
137 [Define to 1 if nanosleep mishandles large arguments.])
138 ;;
139 esac
140 ;;
141 esac
142 else
143 HAVE_NANOSLEEP=0
144 fi
145 LIBS=$nanosleep_save_libs
146])
Note: See TracBrowser for help on using the repository browser.