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