1 /* xnanosleep.c -- a more convenient interface to nanosleep
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
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.
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.
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/>. */
19 /* Mostly written (for sleep.c) by Paul Eggert.
20 Factored out (creating this file) by Jim Meyering. */
24 #include "xnanosleep.h"
31 #include <sys/types.h>
37 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
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. */
48 xnanosleep (double seconds)
50 enum { BILLION = 1000000000 };
52 /* For overflow checking, use naive comparison if possible, widening
53 to long double if double is not wide enough. Otherwise, use <=,
54 not <, to avoid problems when TIME_T_MAX is less than SECONDS but
55 compares equal to SECONDS after loss of precision when coercing
56 from time_t to long double. This mishandles near-maximal values
57 in some rare (perhaps theoretical) cases but that is better than
58 undefined behavior. */
59 bool overflow = ((time_t) ((double) TIME_T_MAX / 2) == TIME_T_MAX / 2
60 ? TIME_T_MAX < seconds
61 : (time_t) ((long double) TIME_T_MAX / 2) == TIME_T_MAX / 2
62 ? TIME_T_MAX < (long double) seconds
63 : TIME_T_MAX <= (long double) seconds);
65 struct timespec ts_sleep;
67 assert (0 <= seconds);
69 /* Separate whole seconds from nanoseconds. */
72 time_t floor_seconds = seconds;
73 double ns = BILLION * (seconds - floor_seconds);
74 ts_sleep.tv_sec = floor_seconds;
76 /* Round up to the next whole number, if necessary, so that we
77 always sleep for at least the requested amount of time. Assuming
78 the default rounding mode, we don't have to worry about the
79 rounding error when computing 'ns' above, since the error won't
80 cause 'ns' to drop below an integer boundary. */
81 ts_sleep.tv_nsec = ns;
82 ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
84 /* Normalize the interval length. nanosleep requires this. */
85 if (BILLION <= ts_sleep.tv_nsec)
87 if (ts_sleep.tv_sec == TIME_T_MAX)
92 ts_sleep.tv_nsec -= BILLION;
101 ts_sleep.tv_sec = TIME_T_MAX;
102 ts_sleep.tv_nsec = BILLION - 1;
105 /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
106 when resumed after being suspended. Earlier versions would
107 set errno to EINTR. nanosleep from linux-2.6.10, as well as
108 implementations by (all?) other vendors, doesn't return -1
109 in that case; either it continues sleeping (if time remains)
110 or it returns zero (if the wake-up time has passed). */
112 if (nanosleep (&ts_sleep, NULL) == 0)
114 if (errno != EINTR && errno != 0)