rippled
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 <ripple/basics/UptimeClock.h>
21 
22 namespace ripple {
23 
24 std::atomic<UptimeClock::rep> UptimeClock::now_{0}; // seconds since start
25 std::atomic<bool> UptimeClock::stop_{false}; // stop update thread
26 
27 // On rippled shutdown, cancel and wait for the update thread
29 {
30  if (joinable())
31  {
32  stop_ = true;
33  // This join() may take up to a 1s, but happens only
34  // once at rippled shutdown.
35  join();
36  }
37 }
38 
39 // Launch the update thread
42 {
43  return update_thread{[] {
44  using namespace std;
45  using namespace std::chrono;
46 
47  // Wake up every second and update now_
48  auto next = system_clock::now() + 1s;
49  while (!stop_)
50  {
51  this_thread::sleep_until(next);
52  next += 1s;
53  ++now_;
54  }
55  }};
56 }
57 
58 // This actually measures time since first use, instead of since rippled start.
59 // However the difference between these two epochs is a small fraction of a
60 // second and unimportant.
61 
64 {
65  // start the update thread on first use
66  static const auto init = start_clock();
67 
68  // Return the number of seconds since rippled start
69  return time_point{duration{now_}};
70 }
71 
72 } // namespace ripple
ripple::UptimeClock::stop_
static std::atomic< bool > stop_
Definition: UptimeClock.h:53
std::chrono::duration< rep, period >
std::thread::joinable
T joinable(T... args)
ripple::UptimeClock::update_thread::~update_thread
~update_thread()
Definition: UptimeClock.cpp:28
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:63
std::chrono::time_point
std::atomic
STL class.
ripple::UptimeClock::update_thread
Definition: UptimeClock.h:55
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std
STL namespace.
ripple::UptimeClock::now_
static std::atomic< rep > now_
Definition: UptimeClock.h:52
std::thread::join
T join(T... args)
ripple::UptimeClock::start_clock
static update_thread start_clock()
Definition: UptimeClock.cpp:41
std::next
T next(T... args)
std::chrono