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.
llvm-svn: 291331
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: deaceefc4b7018d98e0d27c66a0e2e063e1f03f0
diff --git a/src/thread.cpp b/src/thread.cpp
index 4fb1dd2..290e2ae 100644
--- a/src/thread.cpp
+++ b/src/thread.cpp
@@ -117,6 +117,12 @@
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 @@
while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
;
+#endif
}
}