watchdog.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 #ifndef PSEN_SCAN_V2_STANDALONE_WATCHDOG_H
16 #define PSEN_SCAN_V2_STANDALONE_WATCHDOG_H
17 
18 #include <chrono>
19 #include <functional>
20 #include <thread>
21 #include <atomic>
22 #include <condition_variable>
23 
25 
27 {
31 namespace util
32 {
39 class Watchdog
40 {
41 public:
42  using Timeout = const std::chrono::high_resolution_clock::duration;
43 
44 public:
45  Watchdog(const Timeout& timeout, const std::function<void()>& timeout_callback);
46  ~Watchdog();
47 
48 public:
50  void reset();
51 
52 private:
61  std::cv_status waitFor(const Timeout& timeout);
62 
63 private:
65  std::atomic_bool terminated_{ false };
66  std::condition_variable cv_;
67  std::mutex cv_m_;
68  std::thread timer_thread_;
69 };
70 
71 inline Watchdog::Watchdog(const Timeout& timeout, const std::function<void()>& timeout_callback)
72  : timer_thread_([this, timeout, timeout_callback]() {
74  while (!terminated_)
75  {
76  if ((this->waitFor(timeout) == std::cv_status::timeout) && !terminated_)
77  {
78  timeout_callback();
79  }
80  }
81  })
82 {
83  // The timer_thread does not always immediately start because the system schedules threads
84  // "at a whim". To ensure that the thread is running after the completion of the constructor,
85  // we wait until the first command of the thread is executed.
86  if (!thread_startetd_barrier_.waitTillRelease(timeout))
87  {
88  // Difficult to test because this is a timing problem.
89  // LCOV_EXCL_START
90  throw std::runtime_error("Timeout while waiting for timer thread to start");
91  // LCOV_EXCL_STOP
92  }
93 }
94 
95 inline std::cv_status Watchdog::waitFor(const Timeout& timeout)
96 {
97  std::unique_lock<std::mutex> lk(cv_m_);
98  return cv_.wait_for(lk, timeout);
99 }
100 
101 inline void Watchdog::reset()
102 {
103  cv_.notify_all();
104 }
105 
107 {
108  terminated_ = true;
109  // Notify timeout thread to wake up and end execution
110  cv_.notify_all();
111  if (timer_thread_.joinable())
112  {
113  timer_thread_.join();
114  }
115 }
116 
117 } // namespace util
118 } // namespace psen_scan_v2_standalone
119 
120 #endif // PSEN_SCAN_V2_STANDALONE_WATCHDOG_H
psen_scan_v2_standalone::util::Watchdog::reset
void reset()
Restarts the watchdog timer.
Definition: watchdog.h:101
psen_scan_v2_standalone::util::Watchdog::Timeout
const std::chrono::high_resolution_clock::duration Timeout
Definition: watchdog.h:42
async_barrier.h
psen_scan_v2_standalone::util::Barrier
Helper class to simplify the synchronization between different threads.
Definition: async_barrier.h:30
psen_scan_v2_standalone::util::Watchdog::terminated_
std::atomic_bool terminated_
Definition: watchdog.h:65
psen_scan_v2_standalone::util::Watchdog::Watchdog
Watchdog(const Timeout &timeout, const std::function< void()> &timeout_callback)
Definition: watchdog.h:71
psen_scan_v2_standalone::util::Watchdog::cv_
std::condition_variable cv_
Definition: watchdog.h:66
psen_scan_v2_standalone::util::Watchdog::waitFor
std::cv_status waitFor(const Timeout &timeout)
Definition: watchdog.h:95
psen_scan_v2_standalone::util::Watchdog::thread_startetd_barrier_
util::Barrier thread_startetd_barrier_
Definition: watchdog.h:64
psen_scan_v2_standalone::util::Watchdog::~Watchdog
~Watchdog()
Definition: watchdog.h:106
psen_scan_v2_standalone::util::Watchdog::cv_m_
std::mutex cv_m_
Definition: watchdog.h:67
psen_scan_v2_standalone::util::Watchdog::timer_thread_
std::thread timer_thread_
Definition: watchdog.h:68
psen_scan_v2_standalone::util::Watchdog
Watchdog which continuously calls the specified timeout callback.
Definition: watchdog.h:39
psen_scan_v2_standalone
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41
psen_scan_v2_standalone::util::Barrier::release
void release()
Definition: async_barrier.h:43


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Jun 22 2024 02:46:12