Use a consistent style for including <config.h>.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Mostly written (for sleep.c) by Paul Eggert.
19    Factored out (creating this file) by Jim Meyering.  */
20
21 #ifdef 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 "intprops.h"
36 #include "timespec.h"
37
38 #ifndef TIME_T_MAX
39 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
40 #endif
41
42 /* Sleep until the time (call it WAKE_UP_TIME) specified as
43    SECONDS seconds after the time this function is called.
44    SECONDS must be non-negative.  If SECONDS is so large that
45    it is not representable as a `struct timespec', then use
46    the maximum value for that interval.  Return -1 on failure
47    (setting errno), 0 on success.  */
48
49 int
50 xnanosleep (double seconds)
51 {
52   enum { BILLION = 1000000000 };
53
54   bool overflow = false;
55   double ns;
56   struct timespec ts_sleep;
57
58   assert (0 <= seconds);
59
60   /* Separate whole seconds from nanoseconds.
61      Be careful to detect any overflow.  */
62   ts_sleep.tv_sec = seconds;
63   ns = BILLION * (seconds - ts_sleep.tv_sec);
64   overflow |= ! (ts_sleep.tv_sec <= seconds && 0 <= ns && ns <= BILLION);
65   ts_sleep.tv_nsec = ns;
66
67   /* Round up to the next whole number, if necessary, so that we
68      always sleep for at least the requested amount of time.  Assuming
69      the default rounding mode, we don't have to worry about the
70      rounding error when computing 'ns' above, since the error won't
71      cause 'ns' to drop below an integer boundary.  */
72   ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
73
74   /* Normalize the interval length.  nanosleep requires this.  */
75   if (BILLION <= ts_sleep.tv_nsec)
76     {
77       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 }