From 2f2b6680291d8d766e51f3c1cc5e8078f8d285f0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 12 Nov 2010 15:17:27 +0100 Subject: [PATCH] sleep, nanosleep: Work around Linux 2.6.9 nanosleep bug. * lib/sleep.c (rpl_sleep): Split in chunks no larger than 24 days. * lib/nanosleep.c (nanosleep): Likewise. --- ChangeLog | 6 ++++++ lib/nanosleep.c | 13 +++++++------ lib/sleep.c | 9 +++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index d9e32a88f..077649a92 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-11-12 Bruno Haible + + sleep, nanosleep: Work around Linux 2.6.9 nanosleep bug. + * lib/sleep.c (rpl_sleep): Split in chunks no larger than 24 days. + * lib/nanosleep.c (nanosleep): Likewise. + 2010-11-11 Bruno Haible fcntl-h: Fix for use of C++ on glibc systems. diff --git a/lib/nanosleep.c b/lib/nanosleep.c index 1a455fb70..545452d4a 100644 --- a/lib/nanosleep.c +++ b/lib/nanosleep.c @@ -47,10 +47,11 @@ nanosleep (const struct timespec *requested_delay, struct timespec *remaining_delay) #undef nanosleep { - /* nanosleep mishandles large sleeps due to internal overflow - problems. The worst known case of this is cygwin 1.5.x, which - can't sleep more than 49.7 days (2**32 milliseconds). Solve this - by breaking the sleep up into smaller chunks. */ + /* nanosleep mishandles large sleeps due to internal overflow problems. + The worst known case of this is Linux 2.6.9 with glibc 2.3.4, which + can't sleep more than 24.85 days (2^31 milliseconds). Similarly, + cygwin 1.5.x, which can't sleep more than 49.7 days (2^32 milliseconds). + Solve this by breaking the sleep up into smaller chunks. */ if (requested_delay->tv_nsec < 0 || BILLION <= requested_delay->tv_nsec) { @@ -60,8 +61,8 @@ nanosleep (const struct timespec *requested_delay, { /* Verify that time_t is large enough. */ - verify (TYPE_MAXIMUM (time_t) / 49 / 24 / 60 / 60); - const time_t limit = 49 * 24 * 60 * 60; + verify (TYPE_MAXIMUM (time_t) / 24 / 24 / 60 / 60); + const time_t limit = 24 * 24 * 60 * 60; time_t seconds = requested_delay->tv_sec; struct timespec intermediate; intermediate.tv_nsec = 0; diff --git a/lib/sleep.c b/lib/sleep.c index 213e5bd29..68d434abb 100644 --- a/lib/sleep.c +++ b/lib/sleep.c @@ -1,5 +1,5 @@ /* Pausing execution of the current thread. - Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc. + Copyright (C) 2007, 2009-2010 Free Software Foundation, Inc. Written by Bruno Haible , 2007. This program is free software: you can redistribute it and/or modify @@ -50,13 +50,14 @@ sleep (unsigned int seconds) /* Guarantee unlimited sleep and a reasonable return value. Cygwin 1.5.x rejects attempts to sleep more than 49.7 days (2**32 milliseconds), but uses uninitialized memory which results in a - garbage answer. */ + garbage answer. Similarly, Linux 2.6.9 with glibc 2.3.4 has a too + small return value when asked to sleep more than 24.85 days. */ unsigned int rpl_sleep (unsigned int seconds) { /* This requires int larger than 16 bits. */ - verify (UINT_MAX / 49 / 24 / 60 / 60); - const unsigned int limit = 49 * 24 * 60 * 60; + verify (UINT_MAX / 24 / 24 / 60 / 60); + const unsigned int limit = 24 * 24 * 60 * 60; while (limit < seconds) { unsigned int result; -- 2.11.0