rippled
Loading...
Searching...
No Matches
UptimeClock.cpp
1#include <xrpl/basics/UptimeClock.h>
2
3#include <atomic>
4#include <chrono>
5#include <thread>
6
7namespace ripple {
8
10std::atomic<bool> UptimeClock::stop_{false}; // stop update thread
11
12// On rippled shutdown, cancel and wait for the 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
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
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
static update_thread start_clock()
static std::atomic< rep > now_
Definition UptimeClock.h:33
static time_point now()
static std::atomic< bool > stop_
Definition UptimeClock.h:34
T join(T... args)
T joinable(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.