CFEL - ASG Software Suite  2.5.0
CASS
pausablethread.cpp
Go to the documentation of this file.
1 /**
2  * @file pausablethread.cpp definition of a pausable QThread
3  *
4  * @author Lutz Foucar
5  */
6 
7 #include <iostream>
8 
9 #include "pausablethread.h"
10 
11 #include "log.h"
12 
13 using namespace lmf;
14 using namespace std;
15 using namespace cass;
16 
18 {
19  if(isRunning())
20  {
21  terminate();
22  }
23  wait();
24 }
25 
27 {
28  try
29  {
30  runthis();
31  }
32  catch (const invalid_argument &error)
33  {
34  Log::add(Log::DEBUG4,string("PausableThread::run(): catch invalid argument exception '") +
35  error.what() + "'");
36  _exception_thrown = INVALID_ARGUMENT_EXCEPTION;
37  _invarg_excep = error;
38  }
39  catch (const runtime_error &error)
40  {
41  Log::add(Log::DEBUG4,string("PausableThread::run(): catch runtime error exception '") +
42  error.what() + "'");
43  _exception_thrown = RUNTIME_ERROR_EXCEPTION;
44  _runt_excep = error;
45  }
46  catch (const out_of_range &error)
47  {
48  Log::add(Log::DEBUG4,string("PausableThread::run(): catch out of range exception '") +
49  error.what() + "'");
50  _exception_thrown = OUT_OF_RANGE_EXCEPTION;
51  _outrange_excep = error;
52  }
53  catch (const logic_error &error)
54  {
55  Log::add(Log::DEBUG4,string("PausableThread::run(): catch logic error exception '") +
56  error.what() + "'");
57  _exception_thrown = LOGIC_ERROR_EXCEPTION;
58  _logic_excep = error;
59  }
60  catch (const exception &error)
61  {
62  Log::add(Log::DEBUG4,string("PausableThread::run(): catch standart exception '") +
63  error.what() + "'");
64  _exception_thrown = STANDART_EXCEPTION;
65  }
66  catch (...)
67  {
68  Log::add(Log::DEBUG4,string("PausableThread::run(): catch unknown exception"));
69  _exception_thrown = UNKNOWN_EXCEPTION;
70  }
71 }
72 
73 void PausableThread::pause(bool wait)
74 {
75  if (_control == _pause)
76  throw runtime_error("PausableThread::pause(): Thread is already told to pause");
77  _control = _pause;
78  ++_pausecount;
79  if (_status != notstarted && wait)
80  waitUntilPaused();
81 }
82 
84 {
85  QMutex mutex;
86  QMutexLocker lock(&mutex);
87  if (_control != _pause)
88  throw runtime_error("PausableThread::waitUntilPaused(): Threat is not told to be paused");
89  if(_status == paused)
90  return;
91  _waitUntilPausedCondition.wait(&mutex);
92 }
93 
95 {
96  QMutexLocker lock(&_pauseMutex);
97  if (_control == _run)
98  throw runtime_error("PausableThread::resume(): Thread is already told to resume");
99  if(_status == running)
100  throw runtime_error("PausableThread::resume(): Thread is already running");
101  --_pausecount;
102  if (_pausecount == 0)
103  {
104  _control = _run;
105  _pauseCondition.wakeAll();
106  }
107 }
108 
110 {
111  if (_control == _pause)
112  {
113  QMutexLocker lock(&_pauseMutex);
114  _status = paused;
115  _waitUntilPausedCondition.wakeOne();
116  _pauseCondition.wait(&_pauseMutex);
117  _status = running;
118  }
119 }
120 
122 {
123  switch (_exception_thrown)
124  {
125  case INVALID_ARGUMENT_EXCEPTION:
126  throw _invarg_excep;
127  break;
129  throw _runt_excep;
130  break;
132  throw _outrange_excep;
133  break;
135  throw _logic_excep;
136  break;
139  throw 0;
140  break;
142  default:
143  break;
144  }
145 }
declaration of a pausable QThread
void rethrowException() const
rethrow the thrown exception
void pause(bool block=false)
pause the thread
STL namespace.
void waitUntilPaused()
waits until thread is paused
virtual ~PausableThread()
destructor
void run()
run the thread
void resume()
resume the thread
void pausePoint()
point where the thread will be paused
contains a logger for cass