From e3b42fd990070f48c11b233cec0c198098d1a48f Mon Sep 17 00:00:00 2001 From: Jonathan Reams Date: Wed, 7 Sep 2016 14:18:23 -0400 Subject: SERVER-26002 Make sure javascript sleep isn't interrupted --- src/mongo/stdx/thread.h | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/mongo/stdx') diff --git a/src/mongo/stdx/thread.h b/src/mongo/stdx/thread.h index d81dc1bf315..c85c654445a 100644 --- a/src/mongo/stdx/thread.h +++ b/src/mongo/stdx/thread.h @@ -28,6 +28,8 @@ #pragma once +#include +#include #include #include #include @@ -105,7 +107,37 @@ inline void swap(thread& lhs, thread& rhs) noexcept { lhs.swap(rhs); } -namespace this_thread = ::std::this_thread; // NOLINT +namespace this_thread { +using std::this_thread::get_id; +using std::this_thread::yield; + +#ifdef _WIN32 +using std::this_thread::sleep_for; +using std::this_thread::sleep_until; +#else +template +inline void sleep_for(const std::chrono::duration& sleep_duration) { + if (sleep_duration <= sleep_duration.zero()) + return; + + const auto seconds = std::chrono::duration_cast(sleep_duration); + const auto nanoseconds = + std::chrono::duration_cast(sleep_duration - seconds); + struct timespec sleepVal = {static_cast(seconds.count()), + static_cast(nanoseconds.count())}; + struct timespec remainVal; + while (nanosleep(&sleepVal, &remainVal) == -1 && errno == EINTR) { + sleepVal = remainVal; + } +} + +template +void sleep_until(const std::chrono::time_point& sleep_time) { + const auto now = Clock::now(); + sleep_for(sleep_time - now); +} +#endif +} // namespace this_thread } // namespace stdx } // namespace mongo -- cgit v1.2.1