Make basic_seconds_clock::time_point atomic

* Just the rep is made atomic to workaround older compilers
This commit is contained in:
Howard Hinnant
2021-11-17 17:14:21 -05:00
committed by Nik Bougalis
parent daccb5b4c0
commit 32a26a65d9

View File

@@ -19,6 +19,7 @@
#include <ripple/beast/clock/basic_seconds_clock.h> #include <ripple/beast/clock/basic_seconds_clock.h>
#include <atomic>
#include <cassert> #include <cassert>
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
@@ -38,7 +39,7 @@ class seconds_clock_thread
std::mutex mut_; std::mutex mut_;
std::condition_variable cv_; std::condition_variable cv_;
std::thread thread_; std::thread thread_;
Clock::time_point tp_; std::atomic<Clock::time_point::rep> tp_;
public: public:
~seconds_clock_thread(); ~seconds_clock_thread();
@@ -52,6 +53,8 @@ private:
run(); run();
}; };
static_assert(std::atomic<std::chrono::steady_clock::rep>::is_always_lock_free);
seconds_clock_thread::~seconds_clock_thread() seconds_clock_thread::~seconds_clock_thread()
{ {
assert(thread_.joinable()); assert(thread_.joinable());
@@ -63,7 +66,8 @@ seconds_clock_thread::~seconds_clock_thread()
thread_.join(); 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); 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::Clock::time_point
seconds_clock_thread::now() seconds_clock_thread::now()
{ {
std::lock_guard lock(mut_); return Clock::time_point{Clock::duration{tp_.load()}};
return tp_;
} }
void void
@@ -83,8 +86,9 @@ seconds_clock_thread::run()
{ {
using namespace std::chrono; using namespace std::chrono;
tp_ = Clock::now(); auto now = Clock::now();
auto const when = floor<seconds>(tp_) + 1s; tp_ = now.time_since_epoch().count();
auto const when = floor<seconds>(now) + 1s;
if (cv_.wait_until(lock, when, [this] { return stop_; })) if (cv_.wait_until(lock, when, [this] { return stop_; }))
return; return;
} }