Bump to m4 1.4.19
[platform/upstream/m4.git] / m4 / nanosleep.m4
1 # serial 40
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-2001, 2003-2021 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
14 AC_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_HEADERS_ONCE([sys/time.h])
23  AC_REQUIRE([gl_FUNC_SELECT])
24
25  AC_CHECK_DECLS_ONCE([alarm])
26
27  nanosleep_save_libs=$LIBS
28
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.
31  LIB_NANOSLEEP=
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.
38
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)'
46    fi
47
48    AC_CACHE_CHECK([for working nanosleep],
49     [gl_cv_func_nanosleep],
50     [
51      AC_RUN_IFELSE(
52        [AC_LANG_SOURCE([[
53           #include <errno.h>
54           #include <limits.h>
55           #include <signal.h>
56           #if HAVE_SYS_TIME_H
57            #include <sys/time.h>
58           #endif
59           #include <time.h>
60           #include <unistd.h>
61           #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
62           #define TYPE_MAXIMUM(t) \
63             ((t) (! TYPE_SIGNED (t) \
64                   ? (t) -1 \
65                   : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
66
67           #if HAVE_DECL_ALARM
68           static void
69           check_for_SIGALRM (int sig)
70           {
71             if (sig != SIGALRM)
72               _exit (1);
73           }
74           #endif
75
76           int
77           main ()
78           {
79             static struct timespec ts_sleep;
80             static struct timespec ts_remaining;
81             /* Test for major problems first.  */
82             if (! nanosleep)
83               return 2;
84             ts_sleep.tv_sec = 0;
85             ts_sleep.tv_nsec = 1;
86             #if HAVE_DECL_ALARM
87             {
88               static struct sigaction act;
89               act.sa_handler = check_for_SIGALRM;
90               sigemptyset (&act.sa_mask);
91               sigaction (SIGALRM, &act, NULL);
92               alarm (1);
93               if (nanosleep (&ts_sleep, NULL) != 0)
94                 return 3;
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;
98               alarm (1);
99               if (nanosleep (&ts_sleep, &ts_remaining) != -1)
100                 return 4;
101               if (errno != EINTR)
102                 return 5;
103               if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
104                 return 6;
105             }
106             #else /* A simpler test for native Windows.  */
107             if (nanosleep (&ts_sleep, &ts_remaining) < 0)
108               return 3;
109             #endif
110             return 0;
111           }]])],
112        [gl_cv_func_nanosleep=yes],
113        [case $? in dnl (
114         4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
115         *)   gl_cv_func_nanosleep=no;;
116         esac],
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" ;;
124         esac
125        ])
126     ])
127    case "$gl_cv_func_nanosleep" in
128      *yes)
129        REPLACE_NANOSLEEP=0
130        ;;
131      *)
132        REPLACE_NANOSLEEP=1
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.])
137            ;;
138          *)
139            # The replacement uses select(). Add $LIBSOCKET to $LIB_NANOSLEEP.
140            for ac_lib in $LIBSOCKET; do
141              case " $LIB_NANOSLEEP " in
142                *" $ac_lib "*) ;;
143                *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
144              esac
145            done
146            ;;
147        esac
148        ;;
149    esac
150  else
151    HAVE_NANOSLEEP=0
152  fi
153  LIBS=$nanosleep_save_libs
154 ])
155
156 # Prerequisites of lib/nanosleep.c.
157 AC_DEFUN([gl_PREREQ_NANOSLEEP],
158 [
159   AC_CHECK_HEADERS_ONCE([sys/select.h])
160   gl_PREREQ_SIG_HANDLER_H
161 ])