xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
beast
clock
libxrpl/beast/clock/basic_seconds_clock.cpp
1
#include <xrpl/beast/clock/basic_seconds_clock.h>
2
3
#include <xrpl/beast/utility/instrumentation.h>
4
5
#include <
atomic
>
6
#include <
chrono
>
7
#include <
condition_variable
>
8
#include <
mutex
>
9
#include <
thread
>
10
11
namespace
beast
{
12
13
namespace
{
14
15
// Updates the clock
16
class
SecondsClockThread
17
{
18
using
Clock =
BasicSecondsClock::Clock
;
19
20
bool
stop_{
false
};
21
std::mutex mut_;
22
std::condition_variable cv_;
23
std::thread thread_;
24
std::atomic<Clock::time_point::rep> tp_;
25
26
public
:
27
~SecondsClockThread();
28
SecondsClockThread();
29
30
Clock::time_point
31
now();
32
33
private
:
34
void
35
run
();
36
};
37
38
static_assert
(std::atomic<std::chrono::steady_clock::rep>::is_always_lock_free);
39
40
SecondsClockThread::~SecondsClockThread()
41
{
42
XRPL_ASSERT(
43
thread_.
joinable
(),
"beast::SecondsClockThread::~SecondsClockThread : thread joinable"
);
44
{
45
std::scoped_lock
const
lock
(mut_);
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
52
SecondsClockThread::SecondsClockThread() : tp_{Clock::now().time_since_epoch().
count
()}
53
{
54
thread_ =
std::thread
(&SecondsClockThread::run,
this
);
55
}
56
57
SecondsClockThread::Clock::time_point
58
SecondsClockThread::now()
59
{
60
return
Clock::time_point{Clock::duration{tp_.load()}};
61
}
62
63
void
64
SecondsClockThread::run()
65
{
66
std::unique_lock
lock
(mut_);
67
while
(
true
)
68
{
69
using namespace
std::chrono
;
70
71
auto
now = Clock::now();
72
tp_ = now.time_since_epoch().count();
73
auto
const
when =
floor<seconds>
(now) + 1s;
74
if
(cv_.wait_until(
lock
, when, [
this
] { return stop_; }))
75
return
;
76
}
77
}
78
79
}
// unnamed namespace
80
81
BasicSecondsClock::time_point
82
BasicSecondsClock::now
()
83
{
84
static
SecondsClockThread kClk;
85
return
kClk.now();
86
}
87
88
}
// namespace beast
atomic
chrono
beast::BasicSecondsClock::now
static time_point now()
Definition
libxrpl/beast/clock/basic_seconds_clock.cpp:82
beast::BasicSecondsClock::Clock
std::chrono::steady_clock Clock
Definition
basic_seconds_clock.h:20
condition_variable
std::count
T count(T... args)
std::floor
T floor(T... args)
std::thread::join
T join(T... args)
std::thread::joinable
T joinable(T... args)
std::lock
T lock(T... args)
mutex
beast
Definition
base_uint.h:697
std::chrono
xrpl::run
int run(int argc, char **argv)
Definition
Main.cpp:354
std::condition_variable::notify_one
T notify_one(T... args)
thread
std::unique_lock
Generated by
1.17.0