xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
basics
UptimeClock.cpp
1
2
#include <xrpl/basics/UptimeClock.h>
3
4
#include <
atomic
>
5
#include <
chrono
>
6
#include <
thread
>
7
8
namespace
xrpl
{
9
10
std::atomic<UptimeClock::rep>
UptimeClock::kNow
{0};
// seconds since start
11
std::atomic<bool>
UptimeClock::kStop
{
false
};
// stop update thread
12
13
// On xrpld shutdown, cancel and wait for the update thread
14
UptimeClock::UpdateThread::~UpdateThread
()
15
{
16
if
(
joinable
())
17
{
18
kStop
=
true
;
19
// This join() may take up to a 1s, but happens only
20
// once at xrpld shutdown.
21
join
();
22
}
23
}
24
25
// Launch the update thread
26
UptimeClock::UpdateThread
27
UptimeClock::startClock
()
28
{
29
return
UpdateThread
{[] {
30
using namespace
std
;
31
using namespace
std::chrono
;
32
33
// Wake up every second and update kNow
34
auto
next
=
system_clock::now
() + 1s;
35
while
(!
kStop
)
36
{
37
this_thread::sleep_until
(
next
);
38
next
+= 1s;
39
++
kNow
;
40
}
41
}};
42
}
43
44
// This actually measures time since first use, instead of since xrpld start.
45
// However the difference between these two epochs is a small fraction of a
46
// second and unimportant.
47
48
UptimeClock::time_point
49
UptimeClock::now
()
50
{
51
// start the update thread on first use
52
static
auto
const
kInit =
startClock
();
53
54
// Return the number of seconds since xrpld start
55
return
time_point
{
duration
{
kNow
}};
56
}
57
58
}
// namespace xrpl
atomic
chrono
xrpl::UptimeClock::time_point
std::chrono::time_point< UptimeClock > time_point
Definition
UptimeClock.h:24
xrpl::UptimeClock::kStop
static std::atomic< bool > kStop
Definition
UptimeClock.h:35
xrpl::UptimeClock::now
static time_point now()
Definition
UptimeClock.cpp:49
xrpl::UptimeClock::startClock
static UpdateThread startClock()
Definition
UptimeClock.cpp:27
xrpl::UptimeClock::kNow
static std::atomic< rep > kNow
Definition
UptimeClock.h:34
xrpl::UptimeClock::duration
std::chrono::duration< rep, period > duration
Definition
UptimeClock.h:23
std::thread::join
T join(T... args)
std::thread::joinable
T joinable(T... args)
std::chrono
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
std::next
T next(T... args)
std::chrono::system_clock::now
T now(T... args)
std::this_thread::sleep_until
T sleep_until(T... args)
xrpl::UptimeClock::UpdateThread
Definition
UptimeClock.h:38
xrpl::UptimeClock::UpdateThread::~UpdateThread
~UpdateThread()
Definition
UptimeClock.cpp:14
thread
Generated by
1.17.0