diff options
author | Bruno Haible <bruno@clisp.org> | 2019-07-02 19:57:03 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2019-07-02 19:57:37 +0200 |
commit | 54d342c75690664d6d8e5c34d8512e253db23cf6 (patch) | |
tree | a947cc2934b7877c4f353c77e35ecea78958831a /lib/usleep.c | |
parent | 77dc7fb4378059ac088ee2d16e8350c3643c1a64 (diff) | |
download | gnulib-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.c | 26 |
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 } |