1/* xnanosleep.c -- a more convenient interface to nanosleep 2 3 Copyright (C) 2002-2007, 2009-2010 Free Software Foundation, Inc. 4 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. 9 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. 14 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/>. */ 17 18/* Mostly written (for sleep.c) by Paul Eggert. 19 Factored out (creating this file) by Jim Meyering. */ 20 21#include <config.h> 22 23#include "xnanosleep.h" 24 25#include <limits.h> 26#include <stdbool.h> 27#include <stdio.h> 28#include <assert.h> 29#include <errno.h> 30#include <sys/types.h> 31#include <time.h> 32 33#include "intprops.h" 34 35#ifndef TIME_T_MAX 36# define TIME_T_MAX TYPE_MAXIMUM (time_t) 37#endif 38 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. */ 45 46int 47xnanosleep (double seconds) 48{ 49 enum { BILLION = 1000000000 }; 50 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); 63 64 struct timespec ts_sleep; 65 66 assert (0 <= seconds); 67 68 /* Separate whole seconds from nanoseconds. */ 69 if (! overflow) 70 { 71 time_t floor_seconds = seconds; 72 double ns = BILLION * (seconds - floor_seconds); 73 ts_sleep.tv_sec = floor_seconds; 74 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); 82 83 /* Normalize the interval length. nanosleep requires this. */ 84 if (BILLION <= ts_sleep.tv_nsec) 85 { 86 if (ts_sleep.tv_sec == TIME_T_MAX) 87 overflow = true; 88 else 89 { 90 ts_sleep.tv_sec++; 91 ts_sleep.tv_nsec -= BILLION; 92 } 93 } 94 } 95 96 for (;;) 97 { 98 if (overflow) 99 { 100 ts_sleep.tv_sec = TIME_T_MAX; 101 ts_sleep.tv_nsec = BILLION - 1; 102 } 103 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). */ 110 errno = 0; 111 if (nanosleep (&ts_sleep, NULL) == 0) 112 break; 113 if (errno != EINTR && errno != 0) 114 return -1; 115 } 116 117 return 0; 118} 119