From 32a26a65d903c7afe901b549bc410058ce9e157a Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Wed, 17 Nov 2021 17:14:21 -0500 Subject: [PATCH] Make basic_seconds_clock::time_point atomic * Just the rep is made atomic to workaround older compilers --- src/ripple/beast/clock/basic_seconds_clock.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ripple/beast/clock/basic_seconds_clock.cpp b/src/ripple/beast/clock/basic_seconds_clock.cpp index c1c97cb7d..7c55a3f8b 100644 --- a/src/ripple/beast/clock/basic_seconds_clock.cpp +++ b/src/ripple/beast/clock/basic_seconds_clock.cpp @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -38,7 +39,7 @@ class seconds_clock_thread std::mutex mut_; std::condition_variable cv_; std::thread thread_; - Clock::time_point tp_; + std::atomic tp_; public: ~seconds_clock_thread(); @@ -52,6 +53,8 @@ private: run(); }; +static_assert(std::atomic::is_always_lock_free); + seconds_clock_thread::~seconds_clock_thread() { assert(thread_.joinable()); @@ -63,7 +66,8 @@ seconds_clock_thread::~seconds_clock_thread() thread_.join(); } -seconds_clock_thread::seconds_clock_thread() : stop_{false}, tp_{Clock::now()} +seconds_clock_thread::seconds_clock_thread() + : stop_{false}, tp_{Clock::now().time_since_epoch().count()} { thread_ = std::thread(&seconds_clock_thread::run, this); } @@ -71,8 +75,7 @@ seconds_clock_thread::seconds_clock_thread() : stop_{false}, tp_{Clock::now()} seconds_clock_thread::Clock::time_point seconds_clock_thread::now() { - std::lock_guard lock(mut_); - return tp_; + return Clock::time_point{Clock::duration{tp_.load()}}; } void @@ -83,8 +86,9 @@ seconds_clock_thread::run() { using namespace std::chrono; - tp_ = Clock::now(); - auto const when = floor(tp_) + 1s; + auto now = Clock::now(); + tp_ = now.time_since_epoch().count(); + auto const when = floor(now) + 1s; if (cv_.wait_until(lock, when, [this] { return stop_; })) return; }