[libcxx] r291331 - thread: implement sleep_for on Windows

Saleem Abdulrasool via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 7 13:18:22 PST 2017


I would really rather not introduce a `__libcpp_thread_nanosleep`.
Different systems may have different granularities for their sleep.  A
`__libcpp_sleep_for(std::chrono::duration)` sounds reasonable however.

On Fri, Jan 6, 2017 at 11:53 PM, Asiri Rathnayake <
asiri.rathnayake at gmail.com> wrote:

> Wouldn't it be better to introduce a __libcpp_thread_nanosleep() API call
> here?
>
> I bumped into a similar issue with a custom thread implementation and have
> a downstream patch like that.
>
> Cheers,
>
> / Asiri
>
>
> On 7 Jan 2017 2:59 a.m., "Saleem Abdulrasool via cfe-commits" <
> cfe-commits at lists.llvm.org> wrote:
>
> Author: compnerd
> Date: Fri Jan  6 20:48:30 2017
> New Revision: 291331
>
> URL: http://llvm.org/viewvc/llvm-project?rev=291331&view=rev
> Log:
> thread: implement sleep_for on Windows
>
> Windows does not provide an implementation of `nanosleep`.  Round up the
> time duration to the nearest ms and use `Sleep`.  Although this may
> over-sleep, there is no hard real-time guarantee on the wake, so
> sleeping a bit more is better than under-sleeping as it within the
> specification.
>
> Modified:
>     libcxx/trunk/src/thread.cpp
>
> Modified: libcxx/trunk/src/thread.cpp
> URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/thread.
> cpp?rev=291331&r1=291330&r2=291331&view=diff
> ============================================================
> ==================
> --- libcxx/trunk/src/thread.cpp (original)
> +++ libcxx/trunk/src/thread.cpp Fri Jan  6 20:48:30 2017
> @@ -117,6 +117,12 @@ sleep_for(const chrono::nanoseconds& ns)
>      using namespace chrono;
>      if (ns > nanoseconds::zero())
>      {
> +#if defined(_LIBCPP_WIN32API)
> +        milliseconds ms = duration_cast<milliseconds>(ns);
> +        if (ns > duration_cast<nanoseconds>(ms))
> +          ++ms;
> +        Sleep(ms.count());
> +#else
>          seconds s = duration_cast<seconds>(ns);
>          timespec ts;
>          typedef decltype(ts.tv_sec) ts_sec;
> @@ -134,6 +140,7 @@ sleep_for(const chrono::nanoseconds& ns)
>
>          while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
>              ;
> +#endif
>      }
>  }
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
>
>


-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170107/25e8f865/attachment.html>


More information about the cfe-commits mailing list