]> Savannah Git Hosting - gnulib.git/commitdiff
usleep: Implement with millisecond resolution on native Windows.
authorBruno Haible <bruno@clisp.org>
Tue, 2 Jul 2019 17:57:03 +0000 (19:57 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 2 Jul 2019 17:57:37 +0000 (19:57 +0200)
* lib/usleep.c (usleep): On native Windows, implement using Sleep().
* doc/pastposix-functions/usleep.texi: Update accordingly.

ChangeLog
doc/pastposix-functions/usleep.texi
lib/usleep.c

index a65943e2f798148849f4b9ab6b1b3612981c41ee..3c5f0a325f25dec42a6b2dc875365161dcde4c2f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-07-02  Bruno Haible  <bruno@clisp.org>
+
+       usleep: Implement with millisecond resolution on native Windows.
+       * lib/usleep.c (usleep): On native Windows, implement using Sleep().
+       * doc/pastposix-functions/usleep.texi: Update accordingly.
+
 2019-07-02  Bruno Haible  <bruno@clisp.org>
 
        lstat tests: Fix test failure on MSVC.
index 440a00e78c28c2ce7d433a9d58e174ff6285aef5..14de4b2afa45b2401c0750854cd18926518b9c21 100644 (file)
@@ -16,7 +16,7 @@ mingw.
 This function is missing on some platforms.  However, the replacement
 is designed to be lightweight, and may round to the nearest second;
 use @code{select} or @code{nanosleep} if better resolution is needed:
-IRIX 5.3, Solaris 2.4, older mingw, MSVC 14, BeOS.
+IRIX 5.3, Solaris 2.4, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index f7e248027fbf98387d83df7657c9302930b9d8c3..480605fa95569306b3963d092d0caded96d65dba 100644 (file)
 
 #include <errno.h>
 
+#if defined _WIN32 && ! defined __CYGWIN__
+# define WIN32_LEAN_AND_MEAN  /* avoid including junk */
+# include <windows.h>
+#endif
+
 #ifndef HAVE_USLEEP
 # define HAVE_USLEEP 0
 #endif
 
 int
 usleep (useconds_t micro)
+#undef usleep
 {
+#if defined _WIN32 && ! defined __CYGWIN__
+  unsigned int milliseconds = micro / 1000;
+  if (sizeof milliseconds < sizeof micro && micro / 1000 != milliseconds)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  if (micro % 1000)
+    milliseconds++;
+  Sleep (milliseconds);
+  return 0;
+#else
   unsigned int seconds = micro / 1000000;
   if (sizeof seconds < sizeof micro && micro / 1000000 != seconds)
     {
@@ -50,9 +68,9 @@ usleep (useconds_t micro)
     seconds++;
   while ((seconds = sleep (seconds)) != 0);
 
-#undef usleep
-#if !HAVE_USLEEP
-# define usleep(x) 0
-#endif
+# if !HAVE_USLEEP
+#  define usleep(x) 0
+# endif
   return usleep (micro % 1000000);
+#endif
 }