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 <cassert>
23 #include <chrono>
24 #include <condition_variable>
25 #include <mutex>
26 #include <thread>
27 
28 namespace beast {
29 
30 namespace {
31 
32 // Updates the clock
33 class seconds_clock_thread
34 {
35  using Clock = basic_seconds_clock::Clock;
36 
37  bool stop_;
38  std::mutex mut_;
40  std::thread thread_;
41  Clock::time_point tp_;
42 
43 public:
44  ~seconds_clock_thread();
45  seconds_clock_thread();
46 
47  Clock::time_point
48  now();
49 
50 private:
51  void
52  run();
53 };
54 
55 seconds_clock_thread::~seconds_clock_thread()
56 {
57  assert(thread_.joinable());
58  {
59  std::lock_guard lock(mut_);
60  stop_ = true;
61  } // publish stop_ asap so if waiting thread times-out, it will see it
62  cv_.notify_one();
63  thread_.join();
64 }
65 
66 seconds_clock_thread::seconds_clock_thread() : stop_{false}, tp_{Clock::now()}
67 {
68  thread_ = std::thread(&seconds_clock_thread::run, this);
69 }
70 
71 seconds_clock_thread::Clock::time_point
72 seconds_clock_thread::now()
73 {
74  std::lock_guard lock(mut_);
75  return tp_;
76 }
77 
78 void
79 seconds_clock_thread::run()
80 {
81  std::unique_lock lock(mut_);
82  while (true)
83  {
84  using namespace std::chrono;
85 
86  tp_ = Clock::now();
87  auto const when = floor<seconds>(tp_) + 1s;
88  if (cv_.wait_until(lock, when, [this] { return stop_; }))
89  return;
90  }
91 }
92 
93 } // unnamed namespace
94 
95 basic_seconds_clock::time_point
96 basic_seconds_clock::now()
97 {
98  static seconds_clock_thread clk;
99  return clk.now();
100 }
101 
102 } // 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.
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:654
std::chrono