DDS  ver. 3.4
TimeoutGuard.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // The header declares a CTimeoutGuard class.
4 //
5 #ifndef TIMEOUTGUARD_H_
6 #define TIMEOUTGUARD_H_
7 
8 // MiscCommon
9 #include "BOOSTHelper.h"
10 #include "Process.h"
11 
12 namespace MiscCommon
13 {
19  {
21  : m_IsInit(false)
22  , m_pid(0)
23  , m_secTimeOut(0)
24  {
25  }
26  ~CTimeoutGuard()
27  {
28  }
29 
30  public:
31  void Init(pid_t _pid, size_t _timeout)
32  {
33  if (m_IsInit)
34  throw std::logic_error("CTimeoutGuard is already initialized");
35 
36  m_pid = _pid;
37  m_secTimeOut = _timeout;
38  m_IsInit = true;
40  new boost::thread(boost::bind(&CTimeoutGuard::ThreadWorker, this)));
41  }
43  {
44  static CTimeoutGuard obj;
45  return obj;
46  }
47  void ThreadWorker() const
48  {
49  sleep(m_secTimeOut);
50  if (m_pid > 0 && IsProcessExist(m_pid))
51  {
52  // TODO: log me!
53  ::kill(m_pid, SIGTERM);
54  }
55  }
56 
57  private:
58  bool m_IsInit;
59  pid_t m_pid;
60  size_t m_secTimeOut;
62  };
63 }; // namespace MiscCommon
64 #endif /*TIMEOUTGUARD_H_*/
void Init(pid_t _pid, size_t _timeout)
Definition: TimeoutGuard.h:31
The class, which watches the running time of the process and sends SEGTERM when defined time-out is r...
Definition: TimeoutGuard.h:18
static CTimeoutGuard & Instance()
Definition: TimeoutGuard.h:42
boost::shared_ptr< boost::thread > Thread_PTR_t
A smart pointer wrapper for boost::thread pointers.
Definition: BOOSTHelper.h:50
void ThreadWorker() const
Definition: TimeoutGuard.h:47
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21