1 /* xnanosleep.c -- a more convenient interface to nanosleep
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Mostly written (for sleep.c) by Paul Eggert.
19 Factored out (creating this file) by Jim Meyering. */
25 #include "xnanosleep.h"
32 #include <sys/types.h>
37 /* The extra casts work around common compiler bugs. */
38 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
39 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
40 It is necessary at least when t == time_t. */
41 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
42 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
43 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
46 # define TIME_T_MAX TYPE_MAXIMUM (time_t)
49 /* Sleep until the time (call it WAKE_UP_TIME) specified as
50 SECONDS seconds after the time this function is called.
51 SECONDS must be non-negative. If SECONDS is so large that
52 it is not representable as a `struct timespec', then use
53 the maximum value for that interval. Return -1 on failure
54 (setting errno), 0 on success. */
57 xnanosleep (double seconds)
59 enum { BILLION = 1000000000 };
61 bool overflow = false;
63 struct timespec ts_sleep;
65 assert (0 <= seconds);
67 /* Separate whole seconds from nanoseconds.
68 Be careful to detect any overflow. */
69 ts_sleep.tv_sec = seconds;
70 ns = BILLION * (seconds - ts_sleep.tv_sec);
71 overflow |= ! (ts_sleep.tv_sec <= seconds && 0 <= ns && ns <= BILLION);
72 ts_sleep.tv_nsec = ns;
74 /* Round up to the next whole number, if necessary, so that we
75 always sleep for at least the requested amount of time. Assuming
76 the default rounding mode, we don't have to worry about the
77 rounding error when computing 'ns' above, since the error won't
78 cause 'ns' to drop below an integer boundary. */
79 ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
81 /* Normalize the interval length. nanosleep requires this. */
82 if (BILLION <= ts_sleep.tv_nsec)
84 time_t t = ts_sleep.tv_sec + 1;
86 /* Detect integer overflow. */
87 overflow |= (t < ts_sleep.tv_sec);
90 ts_sleep.tv_nsec -= BILLION;
97 ts_sleep.tv_sec = TIME_T_MAX;
98 ts_sleep.tv_nsec = BILLION - 1;
101 /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
102 when resumed after being suspended. Earlier versions would
103 set errno to EINTR. nanosleep from linux-2.6.10, as well as
104 implementations by (all?) other vendors, doesn't return -1
105 in that case; either it continues sleeping (if time remains)
106 or it returns zero (if the wake-up time has passed). */
108 if (nanosleep (&ts_sleep, NULL) == 0)
110 if (errno != EINTR && errno != 0)