diff options
author | Mathias Stearn <mathias@10gen.com> | 2017-03-21 18:16:08 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2017-03-22 19:15:21 -0400 |
commit | 86bc5bdac397909e246f0ea19f5414387bb6b0a9 (patch) | |
tree | c99a95052141d4749fd24d22007ee60768feb087 /src/mongo/util/clock_source.h | |
parent | 51d8b9c2f5eafc457f889d9786ebd68e4398ba64 (diff) | |
download | mongo-86bc5bdac397909e246f0ea19f5414387bb6b0a9.tar.gz |
SERVER-28421 Implement ClockSource::waitForConditionUntil()
Diffstat (limited to 'src/mongo/util/clock_source.h')
-rw-r--r-- | src/mongo/util/clock_source.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/mongo/util/clock_source.h b/src/mongo/util/clock_source.h index c8f8d637f81..e38ce7c932f 100644 --- a/src/mongo/util/clock_source.h +++ b/src/mongo/util/clock_source.h @@ -28,7 +28,9 @@ #pragma once +#include "mongo/stdx/condition_variable.h" #include "mongo/stdx/functional.h" +#include "mongo/stdx/mutex.h" #include "mongo/util/time_support.h" namespace mongo { @@ -71,6 +73,43 @@ public: return _tracksSystemClock; } + /** + * Like cv.wait_until(m, deadline), but uses this ClockSource instead of + * stdx::chrono::system_clock to measure the passage of time. + */ + stdx::cv_status waitForConditionUntil(stdx::condition_variable& cv, + stdx::unique_lock<stdx::mutex>& m, + Date_t deadline); + + /** + * Like cv.wait_until(m, deadline, pred), but uses this ClockSource instead of + * stdx::chrono::system_clock to measure the passage of time. + */ + template <typename Pred> + bool waitForConditionUntil(stdx::condition_variable& cv, + stdx::unique_lock<stdx::mutex>& m, + Date_t deadline, + const Pred& pred) { + while (!pred()) { + if (waitForConditionUntil(cv, m, deadline) == stdx::cv_status::timeout) { + return pred(); + } + } + return true; + } + + /** + * Like cv.wait_for(m, duration, pred), but uses this ClockSource instead of + * stdx::chrono::system_clock to measure the passage of time. + */ + template <typename Duration, typename Pred> + bool waitForConditionFor(stdx::condition_variable& cv, + stdx::unique_lock<stdx::mutex>& m, + Duration duration, + const Pred& pred) { + return waitForConditionUntil(cv, m, now() + duration, pred); + } + protected: bool _tracksSystemClock = true; }; |