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
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user