[libcxx] r291331 - thread: implement sleep_for on Windows

Asiri Rathnayake via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 6 23:53:34 PST 2017


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170107/58df3829/attachment.html>


More information about the cfe-commits mailing list