[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Poor granularity of usleep impl on Win32

From: Eric Blake
Subject: Re: Poor granularity of usleep impl on Win32
Date: Wed, 23 Apr 2014 20:38:07 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0

On 04/23/2014 04:26 AM, Daniel P. Berrange wrote:
> The gnulib usleep replacement says
>   /* This file is _intentionally_ light-weight.  Rather than using
>      select or nanosleep, both of which drag in external libraries on
>      some platforms, this merely rounds up to the nearest second if
>      usleep() does not exist.  If sub-second resolution is important,
>      then use a more powerful interface to begin with.  */

Modern mingw provides usleep - it's a version that can't sleep more than
1 second, but it at least has millisecond resolution.  The fallback to
1-second granularity only occurs for platforms that lack usleep()

> And the code confirms it
>     int
>     usleep (useconds_t micro)
>     {
>       unsigned int seconds = micro / 1000000;
>       if (sizeof seconds < sizeof micro && micro / 1000000 != seconds)
>         {
>           errno = EINVAL;
>           return -1;
>         }
>       if (!HAVE_USLEEP && micro % 1000000)
>         seconds++;
>       while ((seconds = sleep (seconds)) != 0);
>     #undef usleep
>     #if !HAVE_USLEEP

Have you confirmed the value of HAVE_USLEEP in config.h for the build of
mingw that you are using?

>     # define usleep(x) 0
>     #endif
>       return usleep (micro % 1000000);
>     }
> The 'sleep' replacement on Win32 calls into the Win32-specific Sleep()
> function which allows milli-second granularity. Why doesn't usleep()
> call into Sleep() directly, so it gets milli-second granularity rather
> than rounding up to the nearest second ?

As far as I can tell, mingw would be the only platform that would
benefit by calling Sleep() - but if everyone these days is already using
new enough mingw that provides usleep(), then the granularity problem is
a red herring.  So at this point, I think there's nothing further worth
patching in gnulib.

Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]