Debian Codegraph @

Source/Concurrent/CThread.cpp

raw
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 "CThread.h"
21
22
23
24 #include "Events/CRequestFeedbackEvent.h"
25
26 #include "Logger/CLogger.h"
27
28
29 namespace Konclude {
30
31 namespace Concurrent {
32
33
34 qint64 CThread::nextThreadID = 1;
35 QSemaphore *CThread::syncThreadID = new QSemaphore(1);
36
37 CThread::CThread(QString threadIdentifierName, CWatchDog *watchDogThread) {
38 threadName = threadIdentifierName;
39 waitTimeSecs = 0;
40 blockTimeSecs = 0;
41 runTimeSecs = 0;
42 threadID = 0;
43 mWatchDog = watchDogThread;
44 threadRuns = false;
45 mActiveEventProcessing = false;
46 }
47
48 CThread::~CThread() {
49 stopThread(true);
50 }
51
52
53 void CThread::setTaskDescription(QString description) {
54 lastTaskDescription = taskDescription;
55 taskDescription = description;
56 }
57
58
59 QString CThread::getLastTaskDescription() {
60 return lastTaskDescription;
61 }
62
63 QString CThread::getTaskDescription() {
64 return taskDescription;
65 }
66
67 QString CThread::getThreadName() {
68 return threadName;
69 }
70
71 qint64 CThread::getWaitTimeSecs() {
72 return waitTimeSecs;
73 }
74
75 qint64 CThread::getRunTimeSecs() {
76 return runTimeSecs;
77 }
78
79 qint64 CThread::getBlockTimeSecs() {
80 return blockTimeSecs;
81 }
82
83 qint64 CThread::getThreadID() {
84 return threadID;
85 }
86
87 void CThread::postEvent(QEvent *event, int priority) {
88 QCoreApplication::postEvent(this,event,priority);
89 }
90
91
92 void CThread::waitSynchronization() {
93 QSemaphore syncSemaphore(0);
94 postEvent(new CWaitSynchronizationEvent(&syncSemaphore));
95 syncSemaphore.acquire();
96 }
97
98
99 bool CThread::isThreadRunning() {
100 return threadRuns;
101 }
102
103
104 void CThread::restartThread() {
105 terminate();
106 wait();
107 startThread();
108 }
109
110
111
112 void CThread::run() {
113 bool registerd = false;
114
115 try {
116 // get thread id
117 syncThreadID->acquire();
118 threadID = nextThreadID++;
119 syncThreadID->release();
120
121
122 // register thread to watchdog
123 if (mWatchDog) {
124 registerd = mWatchDog->registerThread(this);
125 }
126
127
128 // workload measurement
129 TIMEMEASUREMENTBEGINWAITING();
130
131 threadStarted();
132 threadRuns = true;
133 exec();
134 threadRuns = false;
135 threadStopped();
136
137 // workload measurement
138 TIMEMEASUREMENTENDWAITING();
139
140 if (mWatchDog) {
141 mWatchDog->checkoutThread(this);
142 }
143
144 } catch (...) {
145 if (mWatchDog) {
146 mWatchDog->crashedThread(this);
147 }
148 }
149 }
150
151
152 void CThread::prepareExecution() {
153 runTimer.start();
154 }
155 void CThread::concludeExecution() {
156 runTimeSecs += runTimer.elapsed();
157 }
158
159 void CThread::prepareWaiting() {
160 waitTimer.start();
161 }
162
163 void CThread::concludeWaiting() {
164 waitTimeSecs += waitTimer.elapsed();
165 }
166
167
168 bool CThread::event(QEvent *event) {
169
170 try {
171 // workload measurement
172 TIMEMEASUREMENTENDWAITING();
173 TIMEMEASUREMENTBEGINEXECUTION();
174
175 SETTASKDESCRIPTION("Start Processing Events");
176
177 mActiveEventProcessing = true;
178 bool processed = processEvents(event);
179
180 if (!processed) {
181 // event processing
182 QEvent::Type type = event->type();
183 if (type >= 1200 && type < 2000) {
184 processControlEvents(type,(CControlEvent *)event);
185 processed = true;
186 } else if (type >= 2000) {
187 processCustomsEvents(type,(CCustomEvent *)event);
188 processed = true;
189 }
190 }
191 mActiveEventProcessing = false;
192
193
194 // workload measurement
195 TIMEMEASUREMENTENDEXECUTION();
196 TIMEMEASUREMENTBEGINWAITING();
197
198 SETTASKDESCRIPTION("Wait for Events");
199
200
201 return processed;
202 } catch (exception& e) {
203
204 LOG(ERROR, "::Konclude::Thread", logTr("Unhandled standard exception %1, thread %2 stopped.").arg(e.what()).arg(threadName), this);
205
206 return false;
207 } catch (...) {
208
209 LOG(ERROR,"::Konclude::Thread",logTr("Unhandled exception, thread %1 stopped.").arg(threadName),this);
210
211 return false;
212 }
213
214 }
215
216 bool CThread::isThreadProcessingEvents() {
217 return mActiveEventProcessing;
218 }
219
220
221 void CThread::prepareBlocking() {
222 runTimeSecs += runTimer.elapsed();
223 blockTimer.start();
224 }
225
226 void CThread::concludeBlocking() {
227 blockTimeSecs += blockTimer.elapsed();
228 runTimer.start();
229 }
230
231
232
233 void CThread::threadStarted() {
234 }
235
236 void CThread::threadStopped() {
237 }
238
239 void CThread::startThread(Priority priority) {
240 start(priority);
241 moveToThread(this);
242 }
243
244 void CThread::stopThread(bool waitStopped) {
245 quit();
246 if (waitStopped) {
247 wait();
248 }
249 }
250
251 bool CThread::processEvents(QEvent *event) {
252 return false;
253 }
254
255
256 bool CThread::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
257 return false;
258 }
259
260 bool CThread::processControlEvents(QEvent::Type type, CControlEvent *event) {
261 if (type == EVENTREQUESTFEEDBACKWATCHDOG) {
262 SETTASKDESCRIPTION("Send WatchDog Feedback");
263 CRequestFeedbackEvent *rfe = (CRequestFeedbackEvent *)event;
264 CWatchDog *watchDog = (CWatchDog *)rfe->getWatchDogThread();
265
266 qint64 updateNumber = rfe->getUpdateNumber();
267
268 watchDog->feedbackThread(this,updateNumber);
269 return true;
270 } else if (type == EVENTWAITSYNCHRONIZATION) {
271 SETTASKDESCRIPTION("Synchronize");
272 CWaitSynchronizationEvent *wse = (CWaitSynchronizationEvent *)event;
273 wse->synchronize();
274 return true;
275 }
276 return false;
277 }
278
279 }; // end namespace Concurrent
280
281 }; // end namespace Konclude