-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWatchdog.cpp
76 lines (63 loc) · 1.41 KB
/
Watchdog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Watchdog.cpp
*
* Created on: 11 Feb 2010
* Author: sbutler
*/
#include "Watchdog.h"
#include <ctime>
#include <boost/bind.hpp>
#include <iostream>
#include <signal.h>
void termination_handler (int /*signum*/) {
exit(2);
}
Watchdog::Watchdog(int timeout):
mStopRequested(false),
mTimeout(timeout)
{
//initialise start time
mStartTime = time(NULL);
mTime = 0;
struct sigaction new_action;
/* Set up the structure to specify the new action. */
new_action.sa_handler = termination_handler;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction (SIGUSR1, &new_action, NULL);
}
Watchdog::~Watchdog() {
stop();
}
// Create the thread and start work
void Watchdog::go() {
mThread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&Watchdog::watch, this)));
}
void Watchdog::stop() {
mStopRequested = true;
std::cout << "Watchdog: Stopping" << std::endl;
mThread->join();
}
void Watchdog::frame(double tick) {
boost::mutex::scoped_lock l(mMutex);
mTime = (int)tick;
}
void Watchdog::watch() {
while (!mStopRequested) {
int t;
{
//get last tick time
boost::mutex::scoped_lock l(mMutex);
t = mTime + mStartTime;
}
if (t + mTimeout < time(NULL)) {
//no ticks in the last 10 seconds, so exit
std::cout << "Watchdog: Simulation timeout" << std::endl;
system("killall -s SIGUSR1 hammerQt");
break;
} else {
//wait
sleep(1);
}
}
}