summaryrefslogtreecommitdiff
path: root/lib/usleep.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2019-07-02 19:57:03 +0200
committerBruno Haible <bruno@clisp.org>2019-07-02 19:57:37 +0200
commit54d342c75690664d6d8e5c34d8512e253db23cf6 (patch)
treea947cc2934b7877c4f353c77e35ecea78958831a /lib/usleep.c
parent77dc7fb4378059ac088ee2d16e8350c3643c1a64 (diff)
downloadgnulib-54d342c75690664d6d8e5c34d8512e253db23cf6.tar.gz
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.
Diffstat (limited to 'lib/usleep.c')
-rw-r--r--lib/usleep.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/usleep.c b/lib/usleep.c
index f7e248027f..480605fa95 100644
--- a/lib/usleep.c
+++ b/lib/usleep.c
@@ -28,6 +28,11 @@
#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
@@ -39,7 +44,20 @@
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
}