Diana Software
QWatchdogTimer.cc
Go to the documentation of this file.
1 #include "QWatchdogTimer.hh"
2 
3 #include "QError.hh"
4 #include <boost/date_time/posix_time/posix_time.hpp>
5 #include <boost/thread/mutex.hpp>
6 #include <boost/thread/locks.hpp>
7 
8 //_____________________________________________________________________________
10 // kMutexTimeoutMillisec(500),
11  fMutex(NULL),
12  fTimeout(NULL),
13  fLastStartTime(NULL)
14 {
15  fMutex = new boost::mutex();
16 }
17 
18 
19 //_____________________________________________________________________________
21 {
22  delete fMutex;
23  if(fTimeout != NULL)
24  delete fTimeout;
25  if(fLastStartTime != NULL)
26  delete fLastStartTime;
27 }
28 
29 
30 //_____________________________________________________________________________
32 {
33  boost::lock_guard<boost::mutex> lock(*fMutex);
34  if(NULL == fTimeout)
35  fTimeout = new boost::posix_time::time_duration();
36  *fTimeout = boost::posix_time::millisec((int)(1000*timeout));
37 
38  return QERR_SUCCESS;
39 }
40 
41 
42 //_____________________________________________________________________________
44 {
45  boost::lock_guard<boost::mutex> lock(*fMutex);
46 
47  elapsed = false;
48  if(NULL == fTimeout) // timeout was never set
49  return QERR_SUCCESS;
50  if(NULL == fLastStartTime) // timer is stopped (was never started)
51  return QERR_SUCCESS;
52  if(fLastStartTime->is_not_a_date_time()) // timer is stopped
53  return QERR_SUCCESS;
54 
55  boost::posix_time::ptime tNow
56  = boost::posix_time::microsec_clock::local_time();
57  if(tNow - (*fLastStartTime) > (*fTimeout) )
58  elapsed = true;
59 
60  return QERR_SUCCESS;
61 }
62 
63 
64 //_____________________________________________________________________________
66 {
67  boost::lock_guard<boost::mutex> lock(*fMutex);
68  if(NULL == fLastStartTime)
69  fLastStartTime = new boost::posix_time::ptime();
70 
71  *fLastStartTime = boost::posix_time::microsec_clock::local_time();
72 
73  return QERR_SUCCESS;
74 }
75 
76 
77 //_____________________________________________________________________________
79 {
80  boost::lock_guard<boost::mutex> lock(*fMutex);
81  if(fLastStartTime != NULL)
82  *fLastStartTime = boost::posix_time::not_a_date_time;
83 
84  return QERR_SUCCESS;
85 }
@ QERR_SUCCESS
Definition: QError.hh:27
error class with error type and description
Definition: QError.hh:115
QError SetTimeoutSec(double timeout)
virtual ~QWatchdogTimer()
QError TimeoutElapsed(bool &elapsed) const
boost::posix_time::time_duration * fTimeout
boost::mutex * fMutex
boost::posix_time::ptime * fLastStartTime