| 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 |
#include "CWatchDogThread.h" |
| 21 |
|
| 22 |
|
| 23 |
#include "Events/CWatchDogFeedbackEvent.h" |
| 24 |
#include "Events/CRequestFeedbackEvent.h" |
| 25 |
|
| 26 |
|
| 27 |
namespace Konclude { |
| 28 |
|
| 29 |
namespace Concurrent { |
| 30 |
|
| 31 |
CWatchDogThread * CWatchDogThread::watchDogInstance = 0; |
| 32 |
QSemaphore * CWatchDogThread::syncSemaphore = new QSemaphore(1); |
| 33 |
|
| 34 |
|
| 35 |
CWatchDogThread::CWatchDogThread() : CIntervalThread("WatchDog") { |
| 36 |
startThread(); |
| 37 |
currentUpdateNumber = 0; |
| 38 |
} |
| 39 |
|
| 40 |
CWatchDogThread::~CWatchDogThread() { |
| 41 |
stopThread(); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
CWatchDogThread *CWatchDogThread::getInstance() { |
| 46 |
if (watchDogInstance == 0) { |
| 47 |
syncSemaphore->acquire(); |
| 48 |
if (watchDogInstance == 0) { |
| 49 |
watchDogInstance = new CWatchDogThread(); |
| 50 |
} |
| 51 |
syncSemaphore->release(); |
| 52 |
} |
| 53 |
return watchDogInstance; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
bool CWatchDogThread::registerThread(CThread *thread) { |
| 58 |
regSync.lockForWrite(); |
| 59 |
|
| 60 |
qint64 threadID = thread->getThreadID(); |
| 61 |
|
| 62 |
CWatchDogThreadData *data = 0; |
| 63 |
if (threadWatch.contains(threadID)) { |
| 64 |
data = threadWatch.value(threadID); |
| 65 |
} else { |
| 66 |
data = new CWatchDogThreadData(); |
| 67 |
threadWatch.insert(threadID,data); |
| 68 |
|
| 69 |
data->setRegisterThread(true); |
| 70 |
data->setThreadID(threadID); |
| 71 |
data->setThreadName(thread->getThreadName()); |
| 72 |
data->setThread(thread); |
| 73 |
} |
| 74 |
data->setThreadActive(true); |
| 75 |
|
| 76 |
data->setRunTime(thread->getRunTimeSecs()); |
| 77 |
data->setWaitTime(thread->getWaitTimeSecs()); |
| 78 |
data->setTaskDescription(thread->getTaskDescription()); |
| 79 |
|
| 80 |
|
| 81 |
regSync.unlock(); |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
void CWatchDogThread::feedbackThread(CThread *thread, qint64 updateNumber) { |
| 86 |
CWatchDogFeedbackEvent *wdFEvent = new CWatchDogFeedbackEvent(thread->getThreadID(),updateNumber); |
| 87 |
postEvent(wdFEvent); |
| 88 |
} |
| 89 |
|
| 90 |
void CWatchDogThread::checkoutThread(CThread *thread) { |
| 91 |
regSync.lockForWrite(); |
| 92 |
|
| 93 |
qint64 threadID = thread->getThreadID(); |
| 94 |
|
| 95 |
CWatchDogThreadData *data = 0; |
| 96 |
if (threadWatch.contains(threadID)) { |
| 97 |
data = threadWatch.value(threadID); |
| 98 |
} else { |
| 99 |
data = new CWatchDogThreadData(); |
| 100 |
threadWatch.insert(threadID,data); |
| 101 |
|
| 102 |
data->setRegisterThread(true); |
| 103 |
data->setThreadID(threadID); |
| 104 |
data->setThreadName(thread->getThreadName()); |
| 105 |
data->setThread(thread); |
| 106 |
} |
| 107 |
data->setThreadActive(false); |
| 108 |
data->setThreadError(false); |
| 109 |
|
| 110 |
data->setRunTime(thread->getRunTimeSecs()); |
| 111 |
data->setWaitTime(thread->getWaitTimeSecs()); |
| 112 |
data->setTaskDescription(thread->getTaskDescription()); |
| 113 |
|
| 114 |
regSync.unlock(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
void CWatchDogThread::crashedThread(CThread *thread) { |
| 119 |
regSync.lockForWrite(); |
| 120 |
|
| 121 |
qint64 threadID = thread->getThreadID(); |
| 122 |
|
| 123 |
CWatchDogThreadData *data = 0; |
| 124 |
if (threadWatch.contains(threadID)) { |
| 125 |
data = threadWatch.value(threadID); |
| 126 |
} else { |
| 127 |
data = new CWatchDogThreadData(); |
| 128 |
threadWatch.insert(threadID,data); |
| 129 |
|
| 130 |
data->setRegisterThread(true); |
| 131 |
data->setThreadID(threadID); |
| 132 |
data->setThreadName(thread->getThreadName()); |
| 133 |
data->setThread(thread); |
| 134 |
} |
| 135 |
data->setThreadActive(false); |
| 136 |
data->setThreadError(true); |
| 137 |
|
| 138 |
data->setRunTime(thread->getRunTimeSecs()); |
| 139 |
data->setWaitTime(thread->getWaitTimeSecs()); |
| 140 |
data->setTaskDescription(thread->getTaskDescription()); |
| 141 |
|
| 142 |
regSync.unlock(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
QLinkedList<CWatchDogThreadData> CWatchDogThread::getActiveThreadList() { |
| 150 |
QLinkedList<CWatchDogThreadData> retDataList; |
| 151 |
regSync.lockForRead(); |
| 152 |
QList<CWatchDogThreadData *> threadList = threadWatch.values(); |
| 153 |
foreach (CWatchDogThreadData *threadData, threadList) { |
| 154 |
if (threadData->isThreadActive()) { |
| 155 |
retDataList.append(*threadData); |
| 156 |
} |
| 157 |
} |
| 158 |
regSync.unlock(); |
| 159 |
return retDataList; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
// Thread functions |
| 166 |
|
| 167 |
|
| 168 |
void CWatchDogThread::threadStarted() { |
| 169 |
CIntervalThread::threadStarted(); |
| 170 |
startTimerWithInterval(REQUESTFEEDBACKTIMERID,1000); |
| 171 |
startTimerWithInterval(UPDATETHREADINFOTIMERID,500); |
| 172 |
} |
| 173 |
|
| 174 |
bool CWatchDogThread::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 175 |
if (CIntervalThread::processCustomsEvents(type,event)) { |
| 176 |
return true; |
| 177 |
} if (type == EVENTFEEDBACKTHREADWATCHDOG) { |
| 178 |
SETTASKDESCRIPTION("Process Thread Feedback"); |
| 179 |
CWatchDogFeedbackEvent *wdfe = (CWatchDogFeedbackEvent *)event; |
| 180 |
regSync.lockForWrite(); |
| 181 |
|
| 182 |
qint64 threadID = wdfe->getThreadID(); |
| 183 |
qint64 uNr = wdfe->getUpdateNumber(); |
| 184 |
|
| 185 |
CWatchDogThreadData *data = 0; |
| 186 |
if (threadWatch.contains(threadID)) { |
| 187 |
data = threadWatch.value(threadID); |
| 188 |
} |
| 189 |
if (data) { |
| 190 |
data->setUpdateNumber(uNr,QDateTime::currentDateTime()); |
| 191 |
} |
| 192 |
regSync.unlock(); |
| 193 |
return true; |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
bool CWatchDogThread::processTimer(qint64 timerID) { |
| 200 |
if (timerID == REQUESTFEEDBACKTIMERID) { |
| 201 |
SETTASKDESCRIPTION("Request Thread Feedbacks"); |
| 202 |
regSync.lockForRead(); |
| 203 |
QList<CWatchDogThreadData *> threadList = threadWatch.values(); |
| 204 |
foreach (CWatchDogThreadData *threadData, threadList) { |
| 205 |
if (threadData->isThreadActive()) { |
| 206 |
CThread *thread = threadData->getThread(); |
| 207 |
sendRequestFeedbackEvent(thread,currentUpdateNumber); |
| 208 |
} |
| 209 |
} |
| 210 |
regSync.unlock(); |
| 211 |
currentUpdateNumber++; |
| 212 |
return true; |
| 213 |
} else if (timerID == UPDATETHREADINFOTIMERID) { |
| 214 |
SETTASKDESCRIPTION("Update Thread Informations"); |
| 215 |
regSync.lockForWrite(); |
| 216 |
QList<CWatchDogThreadData *> threadList = threadWatch.values(); |
| 217 |
foreach (CWatchDogThreadData *threadData, threadList) { |
| 218 |
if (threadData->isThreadActive()) { |
| 219 |
CThread *thread = threadData->getThread(); |
| 220 |
updateThreadData(thread,threadData); |
| 221 |
} |
| 222 |
} |
| 223 |
regSync.unlock(); |
| 224 |
return true; |
| 225 |
} |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
void CWatchDogThread::sendRequestFeedbackEvent(CThread *thread, qint64 uNr) { |
| 232 |
thread->postEvent(new CRequestFeedbackEvent(this,uNr)); |
| 233 |
} |
| 234 |
|
| 235 |
void CWatchDogThread::updateThreadData(CThread *thread, CWatchDogThreadData *threadData) { |
| 236 |
threadData->setTaskDescription(thread->getTaskDescription()); |
| 237 |
threadData->setRunTime(thread->getRunTimeSecs()); |
| 238 |
threadData->setWaitTime(thread->getWaitTimeSecs()); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
}; // end namespace Concurrent |
| 243 |
|
| 244 |
}; // end namespace Konclude |