rippled
Loading...
Searching...
No Matches
UptimeClock.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/UptimeClock.h>
21
22#include <atomic>
23#include <chrono>
24#include <thread>
25
26namespace ripple {
27
28std::atomic<UptimeClock::rep> UptimeClock::now_{0}; // seconds since start
29std::atomic<bool> UptimeClock::stop_{false}; // stop update thread
30
31// On rippled shutdown, cancel and wait for the update thread
33{
34 if (joinable())
35 {
36 stop_ = true;
37 // This join() may take up to a 1s, but happens only
38 // once at rippled shutdown.
39 join();
40 }
41}
42
43// Launch the update thread
46{
47 return update_thread{[] {
48 using namespace std;
49 using namespace std::chrono;
50
51 // Wake up every second and update now_
52 auto next = system_clock::now() + 1s;
53 while (!stop_)
54 {
55 this_thread::sleep_until(next);
56 next += 1s;
57 ++now_;
58 }
59 }};
60}
61
62// This actually measures time since first use, instead of since rippled start.
63// However the difference between these two epochs is a small fraction of a
64// second and unimportant.
65
68{
69 // start the update thread on first use
70 static auto const init = start_clock();
71
72 // Return the number of seconds since rippled start
73 return time_point{duration{now_}};
74}
75
76} // namespace ripple
static update_thread start_clock()
Definition: UptimeClock.cpp:45
static std::atomic< rep > now_
Definition: UptimeClock.h:52
static time_point now()
Definition: UptimeClock.cpp:67
static std::atomic< bool > stop_
Definition: UptimeClock.h:53
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:26
STL namespace.