rippled
basic_seconds_clock.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2021, Howard Hinnant <howard.hinnant@gmail.com>
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/beast/clock/basic_seconds_clock.h>
21 
22 #include <atomic>
23 #include <cassert>
24 #include <chrono>
25 #include <condition_variable>
26 #include <mutex>
27 #include <thread>
28 
29 namespace beast {
30 
31 namespace {
32 
33 // Updates the clock
34 class seconds_clock_thread
35 {
36  using Clock = basic_seconds_clock::Clock;
37 
38  bool stop_;
39  std::mutex mut_;
41  std::thread thread_;
43 
44 public:
45  ~seconds_clock_thread();
46  seconds_clock_thread();
47 
48  Clock::time_point
49  now();
50 
51 private:
52  void
53  run();
54 };
55 
57 
58 seconds_clock_thread::~seconds_clock_thread()
59 {
60  assert(thread_.joinable());
61  {
62  std::lock_guard lock(mut_);
63  stop_ = true;
64  } // publish stop_ asap so if waiting thread times-out, it will see it
65  cv_.notify_one();
66  thread_.join();
67 }
68 
69 seconds_clock_thread::seconds_clock_thread()
70  : stop_{false}, tp_{Clock::now().time_since_epoch().count()}
71 {
72  thread_ = std::thread(&seconds_clock_thread::run, this);
73 }
74 
75 seconds_clock_thread::Clock::time_point
76 seconds_clock_thread::now()
77 {
78  return Clock::time_point{Clock::duration{tp_.load()}};
79 }
80 
81 void
82 seconds_clock_thread::run()
83 {
84  std::unique_lock lock(mut_);
85  while (true)
86  {
87  using namespace std::chrono;
88 
89  auto now = Clock::now();
90  tp_ = now.time_since_epoch().count();
91  auto const when = floor<seconds>(now) + 1s;
92  if (cv_.wait_until(lock, when, [this] { return stop_; }))
93  return;
94  }
95 }
96 
97 } // unnamed namespace
98 
99 basic_seconds_clock::time_point
100 basic_seconds_clock::now()
101 {
102  static seconds_clock_thread clk;
103  return clk.now();
104 }
105 
106 } // namespace beast
std::lock
T lock(T... args)
std::lock_guard
STL class.
std::thread::joinable
T joinable(T... args)
beast::basic_seconds_clock::Clock
std::chrono::steady_clock Clock
Definition: basic_seconds_clock.h:39
thread
chrono
std::unique_lock
STL class.
atomic
std::condition_variable::notify_one
T notify_one(T... args)
std::condition_variable::wait_until
T wait_until(T... args)
cassert
condition_variable
mutex
std::thread::join
T join(T... args)
beast
Definition: base_uint.h:641
std::chrono