nanosleep patch 2

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


And here is part 2, ChangeLog is self explanatory.

2003-09-04  Pierre Humblet <pierre.humblet@ieee.org>

	* 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.
-------------- next part --------------
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	5 Sep 2003 01:15:46 -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