1 /* xnanosleep.c -- a more convenient interface to nanosleep
3 Copyright (C) 2002-2007, 2009-2010 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Mostly written (for sleep.c) by Paul Eggert.
19 Factored out (creating this file) by Jim Meyering. */
23 #include "xnanosleep.h"
30 #include <sys/types.h>
36 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
39 /* Sleep until the time (call it WAKE_UP_TIME) specified as
40 SECONDS seconds after the time this function is called.
41 SECONDS must be non-negative. If SECONDS is so large that
42 it is not representable as a `struct timespec', then use
43 the maximum value for that interval. Return -1 on failure
44 (setting errno), 0 on success. */
47 xnanosleep (double seconds)
49 enum { BILLION = 1000000000 };
51 /* For overflow checking, use naive comparison if possible, widening
52 to long double if double is not wide enough. Otherwise, use <=,
53 not <, to avoid problems when TIME_T_MAX is less than SECONDS but
54 compares equal to SECONDS after loss of precision when coercing
55 from time_t to long double. This mishandles near-maximal values
56 in some rare (perhaps theoretical) cases but that is better than
57 undefined behavior. */
58 bool overflow = ((time_t) ((double) TIME_T_MAX / 2) == TIME_T_MAX / 2
59 ? TIME_T_MAX < seconds
60 : (time_t) ((long double) TIME_T_MAX / 2) == TIME_T_MAX / 2
61 ? TIME_T_MAX < (long double) seconds
62 : TIME_T_MAX <= (long double) seconds);
64 struct timespec ts_sleep;
66 assert (0 <= seconds);
68 /* Separate whole seconds from nanoseconds. */
71 time_t floor_seconds = seconds;
72 double ns = BILLION * (seconds - floor_seconds);
73 ts_sleep.tv_sec = floor_seconds;
75 /* Round up to the next whole number, if necessary, so that we
76 always sleep for at least the requested amount of time. Assuming
77 the default rounding mode, we don't have to worry about the
78 rounding error when computing 'ns' above, since the error won't
79 cause 'ns' to drop below an integer boundary. */
80 ts_sleep.tv_nsec = ns;
81 ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
83 /* Normalize the interval length. nanosleep requires this. */
84 if (BILLION <= ts_sleep.tv_nsec)
86 if (ts_sleep.tv_sec == TIME_T_MAX)
91 ts_sleep.tv_nsec -= BILLION;
100 ts_sleep.tv_sec = TIME_T_MAX;
101 ts_sleep.tv_nsec = BILLION - 1;
104 /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
105 when resumed after being suspended. Earlier versions would
106 set errno to EINTR. nanosleep from linux-2.6.10, as well as
107 implementations by (all?) other vendors, doesn't return -1
108 in that case; either it continues sleeping (if time remains)
109 or it returns zero (if the wake-up time has passed). */
111 if (nanosleep (&ts_sleep, NULL) == 0)
113 if (errno != EINTR && errno != 0)