Make xnanosleep's integer overflow test more robust.
[gnulib.git] / lib / xnanosleep.c
1 /* xnanosleep.c -- a more convenient interface to nanosleep
2
3    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4    Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Mostly written (for sleep.c) by Paul Eggert.
20    Factored out (creating this file) by Jim Meyering.  */
21
22 #include <config.h>
23
24 #include "xnanosleep.h"
25
26 #include <limits.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <time.h>
33
34 #include "intprops.h"
35
36 #ifndef TIME_T_MAX
37 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
38 #endif
39
40 /* Sleep until the time (call it WAKE_UP_TIME) specified as
41    SECONDS seconds after the time this function is called.
42    SECONDS must be non-negative.  If SECONDS is so large that
43    it is not representable as a `struct timespec', then use
44    the maximum value for that interval.  Return -1 on failure
45    (setting errno), 0 on success.  */
46
47 int
48 xnanosleep (double seconds)
49 {
50   enum { BILLION = 1000000000 };
51
52   bool overflow = false;
53   double ns;
54   struct timespec ts_sleep;
55
56   assert (0 <= seconds);
57
58   /* Separate whole seconds from nanoseconds.
59      Be careful to detect any overflow.  */
60   ts_sleep.tv_sec = seconds;
61   ns = BILLION * (seconds - ts_sleep.tv_sec);
62   overflow |= ! (ts_sleep.tv_sec <= seconds && 0 <= ns && ns <= BILLION);
63   ts_sleep.tv_nsec = ns;
64
65   /* Round up to the next whole number, if necessary, so that we
66      always sleep for at least the requested amount of time.  Assuming
67      the default rounding mode, we don't have to worry about the
68      rounding error when computing 'ns' above, since the error won't
69      cause 'ns' to drop below an integer boundary.  */
70   ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
71
72   /* Normalize the interval length.  nanosleep requires this.  */
73   if (BILLION <= ts_sleep.tv_nsec)
74     {
75       /* Declare "volatile" so that gcc-4.3.0 doesn't optimize away
76          the overflow test.  */
77       volatile time_t t = ts_sleep.tv_sec + 1;
78
79       /* Detect integer overflow.  */
80       overflow |= (t < ts_sleep.tv_sec);
81
82       ts_sleep.tv_sec = t;
83       ts_sleep.tv_nsec -= BILLION;
84     }
85
86   for (;;)
87     {
88       if (overflow)
89         {
90           ts_sleep.tv_sec = TIME_T_MAX;
91           ts_sleep.tv_nsec = BILLION - 1;
92         }
93
94       /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
95          when resumed after being suspended.  Earlier versions would
96          set errno to EINTR.  nanosleep from linux-2.6.10, as well as
97          implementations by (all?) other vendors, doesn't return -1
98          in that case;  either it continues sleeping (if time remains)
99          or it returns zero (if the wake-up time has passed).  */
100       errno = 0;
101       if (nanosleep (&ts_sleep, NULL) == 0)
102         break;
103       if (errno != EINTR && errno != 0)
104         return -1;
105     }
106
107   return 0;
108 }