RCFProto
 All Classes Functions Typedefs
ProgramTimeLimit.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_TEST_PROGRAMTIMELIMIT_HPP
20 #define INCLUDE_RCF_TEST_PROGRAMTIMELIMIT_HPP
21 
22 #include <iostream>
23 #include <boost/bind.hpp>
24 #include <boost/config.hpp>
25 
26 #include <RCF/ThreadLibrary.hpp>
27 #include <RCF/ThreadPool.hpp>
28 #include <RCF/util/CommandLine.hpp>
29 
30 class ProgramTimeLimit
31 {
32 public:
33  ProgramTimeLimit(unsigned int timeLimitS)
34  {
35  mStartTimeMs = RCF::getCurrentTimeMs();
36  mTimeLimitMs = timeLimitS*1000;
37  mStopFlag = false;
38  if (timeLimitS)
39  {
40  mThreadPtr.reset( new RCF::Thread( boost::bind(&ProgramTimeLimit::poll, this)));
41  }
42  }
43 
44  ~ProgramTimeLimit()
45  {
46  if (mThreadPtr)
47  {
48  {
49  RCF::Lock lock(mStopMutex);
50  mStopFlag = true;
51  mStopCondition.notify_all(lock);
52  }
53  mThreadPtr->join();
54  }
55  }
56 
57 private:
58 
59  void poll()
60  {
61  // Set our thread name.
62  RCF::setWin32ThreadName("RCF Program Time Limit");
63 
64  while (true)
65  {
66  unsigned int pollIntervalMs = 1000;
67  RCF::Lock lock(mStopMutex);
68  mStopCondition.timed_wait(lock, pollIntervalMs);
69  if (mStopFlag)
70  {
71  break;
72  }
73  else
74  {
75  unsigned int currentTimeMs = RCF::getCurrentTimeMs();
76  if (currentTimeMs - mStartTimeMs > mTimeLimitMs)
77  {
78  std::cout
79  << "Time limit expired (" << mTimeLimitMs/1000 << " (s) ). Killing this test."
80  << std::endl;
81 
82 #if defined(_MSC_VER)
83 
84  // By simulating an access violation , we will trigger the
85  // creation of a minidump, which will aid postmortem analysis.
86 
87  int * pn = 0;
88  *pn = 1;
89 
90 #elif defined(BOOST_WINDOWS)
91 
92  TerminateProcess(GetCurrentProcess(), 1);
93 
94 #else
95 
96  abort();
97 
98 #endif
99  }
100  }
101  }
102  }
103 
104  unsigned int mStartTimeMs;
105  unsigned int mTimeLimitMs;
106  RCF::ThreadPtr mThreadPtr;
107  bool mStopFlag;
108 
109  RCF::Mutex mStopMutex;
110  RCF::Condition mStopCondition;
111 };
112 
113 ProgramTimeLimit * gpProgramTimeLimit = NULL;
114 
115 #endif // ! INCLUDE_RCF_TEST_PROGRAMTIMELIMIT_HPP