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_;
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(),
43 "beast::seconds_clock_thread::~seconds_clock_thread : thread joinable");
44 {
46 stop_ = true;
47 } // publish stop_ asap so if waiting thread times-out, it will see it
48 cv_.notify_one();
49 thread_.join();
50}
51
52seconds_clock_thread::seconds_clock_thread()
53 : stop_{false}, tp_{Clock::now().time_since_epoch().count()}
54{
55 thread_ = std::thread(&seconds_clock_thread::run, this);
56}
57
58seconds_clock_thread::Clock::time_point
59seconds_clock_thread::now()
60{
61 return Clock::time_point{Clock::duration{tp_.load()}};
62}
63
64void
65seconds_clock_thread::run()
66{
68 while (true)
69 {
70 using namespace std::chrono;
71
72 auto now = Clock::now();
73 tp_ = now.time_since_epoch().count();
74 auto const when = floor<seconds>(now) + 1s;
75 if (cv_.wait_until(lock, when, [this] { return stop_; }))
76 return;
77 }
78}
79
80} // unnamed namespace
81
82basic_seconds_clock::time_point
83basic_seconds_clock::now()
84{
85 static seconds_clock_thread clk;
86 return clk.now();
87}
88
89} // 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:330