Browse Source

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.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@291331 91177308-0d34-0410-b5e6-96231b3b80d8
Saleem Abdulrasool 8 years ago
parent
commit
cc1d780758
1 changed files with 7 additions and 0 deletions
  1. 7 0
      src/thread.cpp

+ 7 - 0
src/thread.cpp

@@ -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
     }
 }