rippled
Loading...
Searching...
No Matches
basic_seconds_clock.cpp
1#include <xrpl/beast/clock/basic_seconds_clock.h>
2#include <xrpl/beast/utility/instrumentation.h>
3
4#include <atomic>
5#include <chrono>
6#include <condition_variable>
7#include <mutex>
8#include <thread>
9
10namespace beast {
11
12namespace {
13
14// Updates the clock
15class seconds_clock_thread
16{
17 using Clock = basic_seconds_clock::Clock;
18
19 bool stop_{false};
20 std::mutex mut_;
22 std::thread thread_;
24
25public:
26 ~seconds_clock_thread();
27 seconds_clock_thread();
28
29 Clock::time_point
30 now();
31
32private:
33 void
34 run();
35};
36
38
39seconds_clock_thread::~seconds_clock_thread()
40{
41 XRPL_ASSERT(
42 thread_.joinable(), "beast::seconds_clock_thread::~seconds_clock_thread : thread joinable");
43 {
44 std::lock_guard const lock(mut_);
45 stop_ = true;
46 } // publish stop_ asap so if waiting thread times-out, it will see it
47 cv_.notify_one();
48 thread_.join();
49}
50
51seconds_clock_thread::seconds_clock_thread() : tp_{Clock::now().time_since_epoch().count()}
52{
53 thread_ = std::thread(&seconds_clock_thread::run, this);
54}
55
56seconds_clock_thread::Clock::time_point
57seconds_clock_thread::now()
58{
59 return Clock::time_point{Clock::duration{tp_.load()}};
60}
61
62void
63seconds_clock_thread::run()
64{
66 while (true)
67 {
68 using namespace std::chrono;
69
70 auto now = Clock::now();
71 tp_ = now.time_since_epoch().count();
72 auto const when = floor<seconds>(now) + 1s;
73 if (cv_.wait_until(lock, when, [this] { return stop_; }))
74 return;
75 }
76}
77
78} // unnamed namespace
79
80basic_seconds_clock::time_point
81basic_seconds_clock::now()
82{
83 static seconds_clock_thread clk;
84 return clk.now();
85}
86
87} // namespace beast
std::chrono::steady_clock Clock
T count(T... args)
T join(T... args)
T joinable(T... args)
T lock(T... args)
int run(int argc, char **argv)
Definition Main.cpp:332