8c66940ac1efe5cdd1be0f24040edd1a60edb7dd
[gnulib.git] / m4 / nanosleep.m4
1 # serial 34
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-2012 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  dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
17  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
18
19  AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
20  AC_CHECK_HEADERS_ONCE([sys/time.h])
21  AC_REQUIRE([gl_FUNC_SELECT])
22
23  nanosleep_save_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  LIB_NANOSLEEP=
28  AC_SUBST([LIB_NANOSLEEP])
29  AC_SEARCH_LIBS([nanosleep], [rt posix4],
30                 [test "$ac_cv_search_nanosleep" = "none required" ||
31                  LIB_NANOSLEEP=$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 MacOS 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           #if HAVE_SYS_TIME_H
53            #include <sys/time.h>
54           #endif
55           #include <time.h>
56           #include <unistd.h>
57           #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
58           #define TYPE_MAXIMUM(t) \
59             ((t) (! TYPE_SIGNED (t) \
60                   ? (t) -1 \
61                   : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
62
63           static void
64           check_for_SIGALRM (int sig)
65           {
66             if (sig != SIGALRM)
67               _exit (1);
68           }
69
70           int
71           main ()
72           {
73             static struct timespec ts_sleep;
74             static struct timespec ts_remaining;
75             static struct sigaction act;
76             if (! nanosleep)
77               return 2;
78             act.sa_handler = check_for_SIGALRM;
79             sigemptyset (&act.sa_mask);
80             sigaction (SIGALRM, &act, NULL);
81             ts_sleep.tv_sec = 0;
82             ts_sleep.tv_nsec = 1;
83             alarm (1);
84             if (nanosleep (&ts_sleep, NULL) != 0)
85               return 3;
86             ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
87             ts_sleep.tv_nsec = 999999999;
88             alarm (1);
89             if (nanosleep (&ts_sleep, &ts_remaining) != -1)
90               return 4;
91             if (errno != EINTR)
92               return 5;
93             if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
94               return 6;
95             return 0;
96           }]])],
97        [gl_cv_func_nanosleep=yes],
98        [case $? in dnl (
99         4|5|6) gl_cv_func_nanosleep='no (mishandles large arguments)';; dnl (
100         *)   gl_cv_func_nanosleep=no;;
101         esac],
102        [gl_cv_func_nanosleep=cross-compiling])
103     ])
104    if test "$gl_cv_func_nanosleep" = yes; then
105      REPLACE_NANOSLEEP=0
106    else
107      REPLACE_NANOSLEEP=1
108      if test "$gl_cv_func_nanosleep" = 'no (mishandles large arguments)'; then
109        AC_DEFINE([HAVE_BUG_BIG_NANOSLEEP], [1],
110          [Define to 1 if nanosleep mishandles large arguments.])
111      else
112        for ac_lib in $LIBSOCKET; do
113          case " $LIB_NANOSLEEP " in
114          *" $ac_lib "*) ;;
115          *) LIB_NANOSLEEP="$LIB_NANOSLEEP $ac_lib";;
116          esac
117        done
118      fi
119    fi
120  else
121    HAVE_NANOSLEEP=0
122  fi
123  LIBS=$nanosleep_save_libs
124 ])
125
126 # Prerequisites of lib/nanosleep.c.
127 AC_DEFUN([gl_PREREQ_NANOSLEEP],
128 [
129   AC_CHECK_HEADERS_ONCE([sys/select.h])
130   gl_PREREQ_SIG_HANDLER_H
131 ])