nanosleep patch 1

Pierre A. Humblet pierre@phumblet.no-ip.org
Fri Sep 5 01:42:00 GMT 2003


This is part 1 of the patch I sent yesterday.
See previous mails for background  info.
Here are some more details:

hires_ms::minperiod    Make NO_COPY for per process initialization.
hires_ms::resolution   For use in sleep and alarm
hires_ms::dmsecs       Ditto
_DELAY_MAX             Ditto
hires_ms::~hires_ms    Delete, rely on Windows end of process cleanup.
                       Note that previous version could call timeEndPeriod
                       even when timeBeginPeriod had not been called.

Pierre

2003-09-04  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 as global.

-------------- 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	5 Sep 2003 01:15:00 -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	5 Sep 2003 01:15:01 -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);
-}


More information about the Cygwin-patches mailing list