Index: hires.h =================================================================== RCS file: /cvs/src/src/winsup/cygwin/hires.h,v retrieving revision 1.4 diff -u -p -r1.4 hires.h --- hires.h 30 Sep 2002 02:51:21 -0000 1.4 +++ hires.h 4 Sep 2003 03:08:03 -0000 @@ -33,14 +33,30 @@ class hires_us : hires_base LONGLONG usecs (bool justdelta); }; +/* Largest delay in ms for sleep and alarm calls. + Allow actual delay to exceed requested delay by 10 s. + Express as multiple of 1000 (i.e. seconds) + max resolution + The tv_sec argument in timeval structures cannot exceed _DELAY_MAX / 1000 -1, + so that adding fractional part and rounding won't exceed _DELAY_MAX */ +#define _DELAY_MAX (((UINT_MAX - 10000) / 1000) * 1000) + 10 + class hires_ms : hires_base { DWORD initime_ms; LARGE_INTEGER initime_us; - UINT minperiod; + static UINT minperiod; void prime (); public: LONGLONG usecs (bool justdelta); - ~hires_ms (); + UINT dmsecs () { return timeGetTime (); } + UINT resolution () + { + if (!minperiod) + prime (); + return minperiod; + } }; + +extern hires_ms gtod; + #endif /*__HIRES_H__*/ Index: times.cc =================================================================== RCS file: /cvs/src/src/winsup/cygwin/times.cc,v retrieving revision 1.46 diff -u -p -r1.46 times.cc --- times.cc 17 Jul 2003 05:27:03 -0000 1.46 +++ times.cc 4 Sep 2003 03:08:20 -0000 @@ -146,7 +146,6 @@ totimeval (struct timeval *dst, FILETIME extern "C" int gettimeofday (struct timeval *tv, struct timezone *tz) { - static hires_ms gtod; static bool tzflag; LONGLONG now = gtod.usecs (false); if (now == (LONGLONG) -1) @@ -620,37 +619,44 @@ hires_us::usecs (bool justdelta) return justdelta ? now.QuadPart : primed_ft.QuadPart + now.QuadPart; } +hires_ms gtod; +UINT NO_COPY hires_ms::minperiod; + void hires_ms::prime () { TIMECAPS tc; FILETIME f; - int priority = GetThreadPriority (GetCurrentThread ()); - SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL); - if (timeGetDevCaps (&tc, sizeof (tc)) != TIMERR_NOERROR) - minperiod = 0; - else + if (!minperiod) + if (timeGetDevCaps (&tc, sizeof (tc)) != TIMERR_NOERROR) + minperiod = 1; + else + { + minperiod = min (max (tc.wPeriodMin, 1), tc.wPeriodMax); + timeBeginPeriod (minperiod); + } + + if (!inited) { - minperiod = min (max (tc.wPeriodMin, 1), tc.wPeriodMax); - timeBeginPeriod (minperiod); + int priority = GetThreadPriority (GetCurrentThread ()); + SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL); + initime_ms = timeGetTime (); + GetSystemTimeAsFileTime (&f); + SetThreadPriority (GetCurrentThread (), priority); + + inited = 1; + initime_us.HighPart = f.dwHighDateTime; + initime_us.LowPart = f.dwLowDateTime; + initime_us.QuadPart -= FACTOR; + initime_us.QuadPart /= 10; } - - initime_ms = timeGetTime (); - GetSystemTimeAsFileTime (&f); - SetThreadPriority (GetCurrentThread (), priority); - - inited = 1; - initime_us.HighPart = f.dwHighDateTime; - initime_us.LowPart = f.dwLowDateTime; - initime_us.QuadPart -= FACTOR; - initime_us.QuadPart /= 10; } LONGLONG hires_ms::usecs (bool justdelta) { - if (!inited) + if (!minperiod) /* NO_COPY variable */ prime (); DWORD now = timeGetTime (); // FIXME: Not sure how this will handle the 49.71 day wrap around @@ -658,7 +664,3 @@ hires_ms::usecs (bool justdelta) return res; } -hires_ms::~hires_ms () -{ - timeEndPeriod (minperiod); -} Index: signal.cc =================================================================== RCS file: /cvs/src/src/winsup/cygwin/signal.cc,v retrieving revision 1.47 diff -u -p -r1.47 signal.cc --- signal.cc 1 Sep 2003 02:05:32 -0000 1.47 +++ signal.cc 4 Sep 2003 03:08:34 -0000 @@ -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, sigframe thisframe (mainthread); pthread_testcancel (); - if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec > 999999999) + if ((unsigned int) rqtp->tv_sec > (_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 ()) > _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