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