xrpld
Loading...
Searching...
No Matches
UptimeClock.cpp
1
2#include <xrpl/basics/UptimeClock.h>
3
4#include <atomic>
5#include <chrono>
6#include <thread>
7
8namespace xrpl {
9
10std::atomic<UptimeClock::rep> UptimeClock::kNow{0}; // seconds since start
11std::atomic<bool> UptimeClock::kStop{false}; // stop update thread
12
13// On xrpld shutdown, cancel and wait for the update thread
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
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 {
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
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
std::chrono::time_point< UptimeClock > time_point
Definition UptimeClock.h:23
static std::atomic< bool > kStop
Definition UptimeClock.h:34
static time_point now()
static UpdateThread startClock()
static std::atomic< rep > kNow
Definition UptimeClock.h:33
std::chrono::duration< rep, period > duration
Definition UptimeClock.h:22
T join(T... args)
T joinable(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T next(T... args)
T sleep_until(T... args)