| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
#ifndef KONCLUDE_CONCURRENT_CTHREAD_H |
| 21 |
#define KONCLUDE_CONCURRENT_CTHREAD_H |
| 22 |
|
| 23 |
|
| 24 |
#include <QThread> |
| 25 |
#include <QSemaphore> |
| 26 |
#include <QTime> |
| 27 |
#include <QCoreApplication> |
| 28 |
|
| 29 |
|
| 30 |
#include "CWatchDog.h" |
| 31 |
#include "ConcurrentSettings.h" |
| 32 |
|
| 33 |
#include "Events/CCustomEvent.h" |
| 34 |
#include "Events/CControlEvent.h" |
| 35 |
|
| 36 |
|
| 37 |
#include "Events/CWaitSynchronizationEvent.h" |
| 38 |
|
| 39 |
|
| 40 |
namespace Konclude { |
| 41 |
|
| 42 |
namespace Concurrent { |
| 43 |
|
| 44 |
using namespace Events; |
| 45 |
|
| 46 |
//#define THREADTIMEMEASURE |
| 47 |
|
| 48 |
|
| 49 |
#ifdef DEBUG_THREADS |
| 50 |
#define SETTASKDESCRIPTION(a) setTaskDescription(a) |
| 51 |
#else |
| 52 |
#define SETTASKDESCRIPTION(a) |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef THREADTIMEMEASURE |
| 56 |
#define TIMEMEASUREMENTBEGINWAITING() prepareWaiting() |
| 57 |
#define TIMEMEASUREMENTENDWAITING() concludeWaiting() |
| 58 |
#define TIMEMEASUREMENTBEGINEXECUTION() prepareExecution() |
| 59 |
#define TIMEMEASUREMENTENDEXECUTION() concludeExecution() |
| 60 |
#define TIMEMEASUREMENTBEGINBLOCKING() prepareBlocking() |
| 61 |
#define TIMEMEASUREMENTENDBLOCKING() concludeBlocking() |
| 62 |
#else |
| 63 |
#define TIMEMEASUREMENTBEGINWAITING() |
| 64 |
#define TIMEMEASUREMENTENDWAITING() |
| 65 |
#define TIMEMEASUREMENTBEGINEXECUTION() |
| 66 |
#define TIMEMEASUREMENTENDEXECUTION() |
| 67 |
#define TIMEMEASUREMENTBEGINBLOCKING() |
| 68 |
#define TIMEMEASUREMENTENDBLOCKING() |
| 69 |
#endif |
| 70 |
|
| 71 |
class CWatchDog; |
| 72 |
|
| 73 |
|
| 74 |
/*! |
| 75 |
* \class CThread |
| 76 |
* \author Andreas Steigmiller |
| 77 |
* \version 0.1 |
| 78 |
* \brief |
| 79 |
*/ |
| 80 |
class CThread : public QThread { |
| 81 |
|
| 82 |
// public methods |
| 83 |
public: |
| 84 |
//! Constructor |
| 85 |
CThread(QString threadIdentifierName, CWatchDog *watchDogThread = 0); |
| 86 |
|
| 87 |
//! Destructor |
| 88 |
virtual ~CThread(); |
| 89 |
|
| 90 |
|
| 91 |
void startThread(Priority priority = InheritPriority); |
| 92 |
void stopThread(bool waitStopped = true); |
| 93 |
|
| 94 |
void waitSynchronization(); |
| 95 |
|
| 96 |
|
| 97 |
QString getThreadName(); |
| 98 |
QString getLastTaskDescription(); |
| 99 |
QString getTaskDescription(); |
| 100 |
qint64 getWaitTimeSecs(); |
| 101 |
qint64 getRunTimeSecs(); |
| 102 |
qint64 getBlockTimeSecs(); |
| 103 |
qint64 getThreadID(); |
| 104 |
|
| 105 |
|
| 106 |
//! Terminates and restarts the thread. NOTE: because of semaphores, mutex, ... this function is very critical |
| 107 |
void restartThread(); |
| 108 |
|
| 109 |
void postEvent(QEvent *event, int priority = Qt::NormalEventPriority); |
| 110 |
|
| 111 |
|
| 112 |
bool isThreadRunning(); |
| 113 |
|
| 114 |
|
| 115 |
// protected methods |
| 116 |
protected: |
| 117 |
|
| 118 |
virtual void run(); |
| 119 |
virtual bool event(QEvent *event); |
| 120 |
|
| 121 |
|
| 122 |
virtual void prepareBlocking(); |
| 123 |
virtual void concludeBlocking(); |
| 124 |
|
| 125 |
virtual void prepareExecution(); |
| 126 |
virtual void concludeExecution(); |
| 127 |
|
| 128 |
virtual void prepareWaiting(); |
| 129 |
virtual void concludeWaiting(); |
| 130 |
|
| 131 |
virtual void threadStarted(); |
| 132 |
virtual void threadStopped(); |
| 133 |
|
| 134 |
virtual bool processEvents(QEvent *event); |
| 135 |
|
| 136 |
virtual bool processControlEvents(QEvent::Type type, CControlEvent *event); |
| 137 |
virtual bool processCustomsEvents(QEvent::Type type, CCustomEvent *event); |
| 138 |
|
| 139 |
|
| 140 |
void setTaskDescription(QString description); |
| 141 |
|
| 142 |
bool isThreadProcessingEvents(); |
| 143 |
|
| 144 |
// protected methods |
| 145 |
protected: |
| 146 |
bool mActiveEventProcessing; |
| 147 |
|
| 148 |
// private methods |
| 149 |
private: |
| 150 |
|
| 151 |
|
| 152 |
// private variables |
| 153 |
private: |
| 154 |
|
| 155 |
QString threadName; |
| 156 |
QString taskDescription; |
| 157 |
QString lastTaskDescription; |
| 158 |
qint64 threadID; |
| 159 |
|
| 160 |
bool threadRuns; |
| 161 |
|
| 162 |
// workload measurement |
| 163 |
qint64 waitTimeSecs; |
| 164 |
qint64 runTimeSecs; |
| 165 |
qint64 blockTimeSecs; |
| 166 |
QTime runTimer; |
| 167 |
QTime waitTimer; |
| 168 |
QTime blockTimer; |
| 169 |
|
| 170 |
static qint64 nextThreadID; |
| 171 |
static QSemaphore *syncThreadID; |
| 172 |
CWatchDog *mWatchDog; |
| 173 |
}; |
| 174 |
|
| 175 |
}; // end namespace Concurrent |
| 176 |
|
| 177 |
}; // end namespace Konclude |
| 178 |
|
| 179 |
#endif // KONCLUDE_CONCURRENT_CTHREAD_H |