* modules/gethrxtime: New file.
[gnulib.git] / lib / xnanosleep.c
1 /* xnanosleep.c -- a more convenient interface to nanosleep
2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Mostly written (for sleep.c) by Paul Eggert.
19    Factored out (creating this file) by Jim Meyering.  */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "xnanosleep.h"
26
27 #include <limits.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <time.h>
34
35 #include "timespec.h"
36 #include "gethrxtime.h"
37 #include "xtime.h"
38
39 /* The extra casts work around common compiler bugs.  */
40 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
41 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
42    It is necessary at least when t == time_t.  */
43 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
44                               ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
45 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
46
47 #ifndef TIME_T_MAX
48 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
49 #endif
50
51 /* POSIX.1-2001 requires that when a process is suspended, then
52    resumed, nanosleep (A, B) returns -1, sets errno to EINTR, and sets
53    *B to the time remaining at the point of resumption.  However, some
54    versions of the Linux kernel incorrectly return the time remaining
55    at the point of suspension.  Work around this bug on GNU/Linux
56    hosts by computing the remaining time here after nanosleep returns,
57    rather than by relying on nanosleep's computation.  */
58 #ifdef __linux__
59 enum { NANOSLEEP_BUG_WORKAROUND = true };
60 #else
61 enum { NANOSLEEP_BUG_WORKAROUND = false };
62 #endif
63
64 /* Sleep until the time (call it WAKE_UP_TIME) specified as
65    SECONDS seconds after the time this function is called.
66    SECONDS must be non-negative.  If SECONDS is so large that
67    it is not representable as a `struct timespec', then use
68    the maximum value for that interval.  Return -1 on failure
69    (setting errno), 0 on success.  */
70
71 int
72 xnanosleep (double seconds)
73 {
74   enum { BILLION = 1000000000 };
75
76   bool overflow = false;
77   double ns;
78   struct timespec ts_sleep;
79   xtime_t stop = 0;
80
81   assert (0 <= seconds);
82
83   if (NANOSLEEP_BUG_WORKAROUND)
84     {
85       xtime_t now = gethrxtime ();
86       double increment = XTIME_PRECISION * seconds;
87       xtime_t incr = increment;
88       stop = now + incr + (incr < increment);
89       overflow = (stop < now);
90     }
91
92   /* Separate whole seconds from nanoseconds.
93      Be careful to detect any overflow.  */
94   ts_sleep.tv_sec = seconds;
95   ns = BILLION * (seconds - ts_sleep.tv_sec);
96   overflow |= ! (ts_sleep.tv_sec <= seconds && 0 <= ns && ns <= BILLION);
97   ts_sleep.tv_nsec = ns;
98
99   /* Round up to the next whole number, if necessary, so that we
100      always sleep for at least the requested amount of time.  Assuming
101      the default rounding mode, we don't have to worry about the
102      rounding error when computing 'ns' above, since the error won't
103      cause 'ns' to drop below an integer boundary.  */
104   ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
105
106   /* Normalize the interval length.  nanosleep requires this.  */
107   if (BILLION <= ts_sleep.tv_nsec)
108     {
109       time_t t = ts_sleep.tv_sec + 1;
110
111       /* Detect integer overflow.  */
112       overflow |= (t < ts_sleep.tv_sec);
113
114       ts_sleep.tv_sec = t;
115       ts_sleep.tv_nsec -= BILLION;
116     }
117
118   for (;;)
119     {
120       if (overflow)
121         {
122           ts_sleep.tv_sec = TIME_T_MAX;
123           ts_sleep.tv_nsec = BILLION - 1;
124         }
125
126       if (nanosleep (&ts_sleep, NULL) == 0)
127         break;
128       if (errno != EINTR)
129         return -1;
130
131       if (NANOSLEEP_BUG_WORKAROUND)
132         {
133           xtime_t now = gethrxtime ();
134           if (stop <= now)
135             break;
136           else
137             {
138               xtime_t remaining = stop - now;
139               ts_sleep.tv_sec = xtime_nonnegative_sec (remaining);
140               ts_sleep.tv_nsec = xtime_nonnegative_nsec (remaining);
141             }
142         }
143     }
144
145   return 0;
146 }