[Patch] nanosleep()
Pierre A. Humblet
pierre@phumblet.no-ip.org
Thu Sep 4 03:28:00 GMT 2003
This patch to nanosleep, sleep and usleep
a) makes them Posix conformant: the system clock (gettimeofday) must
advance by at least d during Xsleep(d)
(e.g. exim relies on this to create unique ids).
b) improves the resolution of the result by using the multimedia
timer.
c) calls timeBeginPeriod in forked processes.
A follow up patch will improve setitimer.
Pierre
2003-09-03 Pierre Humblet <pierre.humblet@ieee.org>
* hires.h (_DELAY_MAX): Define.
(hires_ms::minperiod): Declare static.
(hires_ms::resolution): New.
(hires_ms::dmsecs): New.
(hires_ms::~hires_ms): Delete.
(gtod): Declare.
* time.c (hires_ms::prime): Always calculate minperiod and
set it to 1 in case of failure.
(hires_ms::resolution): Define.
(hires_ms::~hires_ms): Delete.
(hires_ms::usecs): Check minperiod to prime.
(gtod) Define.
* signal.cc (nanosleep): Round delay up to resolution.
Fix test for negative remainder. Use timeGetTime through gtod.
(sleep): Round up return value.
-------------- next part --------------
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 <sys/cygwin.h>
#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
More information about the Cygwin-patches
mailing list