From: Bruno Haible Date: Tue, 2 Jul 2019 17:57:03 +0000 (+0200) Subject: usleep: Implement with millisecond resolution on native Windows. X-Git-Tag: v1.0~4804 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=54d342c75690664d6d8e5c34d8512e253db23cf6;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index a65943e2f7..3c5f0a325f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2019-07-02 Bruno Haible + + 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 lstat tests: Fix test failure on MSVC. diff --git a/doc/pastposix-functions/usleep.texi b/doc/pastposix-functions/usleep.texi index 440a00e78c..14de4b2afa 100644 --- a/doc/pastposix-functions/usleep.texi +++ b/doc/pastposix-functions/usleep.texi @@ -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: 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 +#if defined _WIN32 && ! defined __CYGWIN__ +# define WIN32_LEAN_AND_MEAN /* avoid including junk */ +# include +#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 }