|
Packit |
33f14e |
/* Pausing execution of the current thread.
|
|
Packit |
33f14e |
Copyright (C) 2007, 2009-2017 Free Software Foundation, Inc.
|
|
Packit |
33f14e |
Written by Bruno Haible <bruno@clisp.org>, 2007.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
33f14e |
it under the terms of the GNU General Public License as published by
|
|
Packit |
33f14e |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
33f14e |
(at your option) any later version.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is distributed in the hope that it will be useful,
|
|
Packit |
33f14e |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
33f14e |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
33f14e |
GNU General Public License for more details.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
You should have received a copy of the GNU General Public License
|
|
Packit |
33f14e |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include <config.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Specification. */
|
|
Packit |
33f14e |
#include <unistd.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include <limits.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include "verify.h"
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
|
|
Packit |
33f14e |
# include <windows.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
unsigned int
|
|
Packit |
33f14e |
sleep (unsigned int seconds)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
unsigned int remaining;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Sleep for 1 second many times, because
|
|
Packit |
33f14e |
1. Sleep is not interruptible by Ctrl-C,
|
|
Packit |
33f14e |
2. we want to avoid arithmetic overflow while multiplying with 1000. */
|
|
Packit |
33f14e |
for (remaining = seconds; remaining > 0; remaining--)
|
|
Packit |
33f14e |
Sleep (1000);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
return remaining;
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#elif HAVE_SLEEP
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# undef sleep
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Guarantee unlimited sleep and a reasonable return value. Cygwin
|
|
Packit |
33f14e |
1.5.x rejects attempts to sleep more than 49.7 days (2**32
|
|
Packit |
33f14e |
milliseconds), but uses uninitialized memory which results in a
|
|
Packit |
33f14e |
garbage answer. Similarly, Linux 2.6.9 with glibc 2.3.4 has a too
|
|
Packit |
33f14e |
small return value when asked to sleep more than 24.85 days. */
|
|
Packit |
33f14e |
unsigned int
|
|
Packit |
33f14e |
rpl_sleep (unsigned int seconds)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
/* This requires int larger than 16 bits. */
|
|
Packit |
33f14e |
verify (UINT_MAX / 24 / 24 / 60 / 60);
|
|
Packit |
33f14e |
const unsigned int limit = 24 * 24 * 60 * 60;
|
|
Packit |
33f14e |
while (limit < seconds)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
unsigned int result;
|
|
Packit |
33f14e |
seconds -= limit;
|
|
Packit |
33f14e |
result = sleep (limit);
|
|
Packit |
33f14e |
if (result)
|
|
Packit |
33f14e |
return seconds + result;
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
return sleep (seconds);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#else /* !HAVE_SLEEP */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#error "Please port gnulib sleep.c to your platform, possibly using usleep() or select(), then report this to bug-gnulib."
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#endif
|