From ed2287adcd6b16a0ef34defb443d5c61fc7830d7 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Sun, 7 Sep 2003 05:18:01 +0000 Subject: * signal.cc (nanosleep): Improve test for valid values. Round delay up to resolution. Fix test for negative remainder. Use timeGetTime through gtod. (sleep): Round up return value. Christopher Faylor * hires.h (HIRES_DELAY_MAX): Define. (hires_ms::minperiod): Declare static. (hires_ms::resolution): New. (hires_ms::dmsecs): New. (hires_ms::prime): Return UINT. (gtod): Declare. * times.cc (hires_ms::prime): Always calculate minperiod and set it to 1 in case of failure. Return minperiod. (hires_ms::resolution): Define. (hires_ms::~hires_ms): Delete. (hires_ms::usecs): Check minperiod to prime. (gtod) Define as global. --- winsup/cygwin/signal.cc | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'winsup/cygwin/signal.cc') diff --git a/winsup/cygwin/signal.cc b/winsup/cygwin/signal.cc index 2b890dd62..133f192b4 100644 --- a/winsup/cygwin/signal.cc +++ b/winsup/cygwin/signal.cc @@ -17,6 +17,7 @@ details. */ #include #include "sigproc.h" #include "pinfo.h" +#include "hires.h" int sigcatchers; /* FIXME: Not thread safe. */ @@ -73,20 +74,22 @@ nanosleep (const struct timespec *rqtp, struct timespec *rmtp) sigframe thisframe (mainthread); pthread_testcancel (); - if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec > 999999999) + if ((unsigned int) rqtp->tv_sec > (HIRES_DELAY_MAX / 1000 - 1) + || (unsigned int) rqtp->tv_nsec > 999999999) { set_errno (EINVAL); return -1; } - - DWORD req = rqtp->tv_sec * 1000 + (rqtp->tv_nsec + 500000) / 1000000; - DWORD start_time = GetTickCount (); - DWORD end_time = start_time + req; + DWORD resolution = gtod.resolution (); + DWORD req = ((rqtp->tv_sec * 1000 + (rqtp->tv_nsec + 999999) / 1000000 + + resolution - 1) / resolution ) * resolution; + DWORD end_time = gtod.dmsecs () + req; syscall_printf ("nanosleep (%ld)", req); int rc = pthread::cancelable_wait (signal_arrived, req); - DWORD now = GetTickCount (); - DWORD rem = (rc == WAIT_TIMEOUT || now >= end_time) ? 0 : end_time - now; + DWORD rem; + if ((rem = end_time - gtod.dmsecs ()) > HIRES_DELAY_MAX) + rem = 0; if (rc == WAIT_OBJECT_0) { (void) thisframe.call_signal_handler (); @@ -111,7 +114,7 @@ sleep (unsigned int seconds) req.tv_sec = seconds; req.tv_nsec = 0; nanosleep (&req, &rem); - return rem.tv_sec + (rem.tv_nsec + 500000000) / 1000000000; + return rem.tv_sec + (rem.tv_nsec > 0); } extern "C" unsigned int -- cgit v1.2.3