IQRF Gateway Daemon
WatchDog.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 MICRORISC s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
20 #include <thread>
21 #include <mutex>
22 #include <condition_variable>
23 #include <functional>
24 #include <chrono>
25 
31 template <typename T>
32 class WatchDog
33 {
34 public:
36  };
37 
43  WatchDog(unsigned int millis, T callback) {
44  start(millis, callback);
45  }
46 
47  virtual ~WatchDog() {
48  stop();
49  }
50 
56  void start(unsigned int millis, T callback) {
57  std::unique_lock<std::mutex> lock(m_stopConditionMutex);
58  if (!m_running) {
59  m_running = true;
60  m_callback = callback;
61  m_timeout = std::chrono::milliseconds(millis);
62  m_lastRefreshTime = std::chrono::system_clock::now();
63  if (m_thread.joinable()) m_thread.join();
64  m_running = true;
65  m_thread = std::thread(&WatchDog::watch, this);
66  }
67  }
68 
72  void stop() {
73  {
74  std::unique_lock<std::mutex> lock(m_stopConditionMutex);
75  if (m_running) {
76  m_running = false;
77  m_stopConditionVariable.notify_all();
78  }
79  }
80  if (m_thread.joinable())
81  m_thread.join();
82  }
83 
87  void pet() {
88  std::unique_lock<std::mutex> lock(m_stopConditionMutex);
89  m_lastRefreshTime = std::chrono::system_clock::now();
90  }
91 
92 private:
94  void watch() {
95  {
96  std::unique_lock<std::mutex> lock(m_stopConditionMutex);
97  while (m_running && ((std::chrono::system_clock::now() - m_lastRefreshTime)) < m_timeout)
98  m_stopConditionVariable.wait_for(lock, m_timeout);
99  }
100  {
101  std::unique_lock<std::mutex> lock(m_stopConditionMutex);
102  if (m_running) {
103  m_running = false;
104  m_callback();
105  }
106  }
107  }
108 
109  bool m_running = false;
110  std::thread m_thread;
111  T m_callback;
112  std::mutex m_stopConditionMutex;
113  std::condition_variable m_stopConditionVariable;
114  std::chrono::system_clock::time_point m_lastRefreshTime;
115  std::chrono::milliseconds m_timeout;
116 
117 };
WatchDog()
Definition: WatchDog.h:35
void stop()
Stop watching thread.
Definition: WatchDog.h:72
void pet()
Pet watch dog.
Definition: WatchDog.h:87
WatchDog(unsigned int millis, T callback)
parametric constructor
Definition: WatchDog.h:43
virtual ~WatchDog()
Definition: WatchDog.h:47
Provides watch dog feature.
Definition: WatchDog.h:32
void start(unsigned int millis, T callback)
Start watching thread.
Definition: WatchDog.h:56