| 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 "CIntervalThread.h" |
| 21 |
|
| 22 |
|
| 23 |
namespace Konclude { |
| 24 |
|
| 25 |
namespace Concurrent { |
| 26 |
|
| 27 |
|
| 28 |
CIntervalThread::CIntervalThread(QString threadIdentifierName, CWatchDog *watchDog) : CThread(threadIdentifierName,watchDog),mSyncTimerMutex(QMutex::Recursive) { |
| 29 |
} |
| 30 |
|
| 31 |
CIntervalThread::~CIntervalThread() { |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
void CIntervalThread::setTimerInterval(qint64 timerID, qint64 timerIntervalMilliSecs) { |
| 37 |
postEvent(new CTimerIntervalEvent(timerID,timerIntervalMilliSecs)); |
| 38 |
} |
| 39 |
|
| 40 |
void CIntervalThread::startTimerWithInterval(qint64 timerID, qint64 timerIntervalMilliSecs) { |
| 41 |
postEvent(new CTimerIntervalEvent(timerID,timerIntervalMilliSecs,true)); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
void CIntervalThread::startTimerWithIntervalLimited(qint64 timerID, qint64 timerIntervalMilliSecs, qint64 remainingCallCount) { |
| 46 |
postEvent(new CTimerIntervalEvent(timerID,timerIntervalMilliSecs,true,false,remainingCallCount)); |
| 47 |
} |
| 48 |
|
| 49 |
void CIntervalThread::startTimer(qint64 timerID) { |
| 50 |
postEvent(new CTimerIntervalEvent(timerID,-1,true)); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
void CIntervalThread::stopTimer(qint64 timerID) { |
| 56 |
|
| 57 |
mSyncTimerMutex.lock(); |
| 58 |
|
| 59 |
if (timers.contains(timerID)) { |
| 60 |
CIntervalThreadData *timerData = timers.value(timerID); |
| 61 |
timerData->incDeactivateCount(); |
| 62 |
} |
| 63 |
|
| 64 |
mSyncTimerMutex.unlock(); |
| 65 |
|
| 66 |
postEvent(new CTimerIntervalEvent(timerID,-1,false,true)); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
void CIntervalThread::threadStarted() { |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
void CIntervalThread::threadStopped() { |
| 76 |
mSyncTimerMutex.lock(); |
| 77 |
QList<CIntervalThreadData *> timerList = timers.values(); |
| 78 |
foreach (CIntervalThreadData *timerData, timerList) { |
| 79 |
bool isActive = timerData->isTimerActive(); |
| 80 |
if (isActive) { |
| 81 |
int timerThreadID = timerData->getTimerThreadID(); |
| 82 |
QObject::killTimer(timerThreadID); |
| 83 |
} |
| 84 |
timerData->setTimerActive(false); |
| 85 |
} |
| 86 |
mSyncTimerMutex.unlock(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
bool CIntervalThread::processEvents(QEvent *event) { |
| 91 |
QEvent *ev = event; |
| 92 |
if (CThread::processEvents(event)) { |
| 93 |
return true; |
| 94 |
} else if (event->type() == QEvent::Timer) { |
| 95 |
SETTASKDESCRIPTION("Process Timer Timeout"); |
| 96 |
QTimerEvent *te = (QTimerEvent *)event; |
| 97 |
int timerThreadID = te->timerId(); |
| 98 |
mSyncTimerMutex.lock(); |
| 99 |
if (timerMapping.contains(timerThreadID)) { |
| 100 |
qint64 timerID = timerMapping.value(timerThreadID); |
| 101 |
if (timers.contains(timerID)) { |
| 102 |
CIntervalThreadData *timerData = timers.value(timerID); |
| 103 |
if (timerData->isTimerActive() && timerData->getDeactivateCount() <= 0) { |
| 104 |
if (timerData->hasRemainingTimerInvocations()) { |
| 105 |
timerData->decRemainingTimerInvocations(1); |
| 106 |
processTimer(timerID); |
| 107 |
} else { |
| 108 |
int timerThreadID = timerData->getTimerThreadID(); |
| 109 |
QObject::killTimer(timerThreadID); |
| 110 |
timerData->setTimerActive(false); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
mSyncTimerMutex.unlock(); |
| 116 |
return true; |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
bool CIntervalThread::processControlEvents(QEvent::Type type, CControlEvent *event) { |
| 123 |
if (CThread::processControlEvents(type,event)) { |
| 124 |
return true; |
| 125 |
} else if (type == EVENTTIMERINTERVAL) { |
| 126 |
SETTASKDESCRIPTION("Configure Timer Settings"); |
| 127 |
CTimerIntervalEvent *tie = (CTimerIntervalEvent *)event; |
| 128 |
|
| 129 |
qint64 timerID = tie->getTimerID(); |
| 130 |
qint64 timerInterval = tie->getTimerInterval(); |
| 131 |
|
| 132 |
bool activateTimer = tie->getTimerActive(); |
| 133 |
bool deactivateTimer = tie->getTimerDeactive(); |
| 134 |
|
| 135 |
qint64 remTimerInvocs = tie->getRemainingTimerInvocations(); |
| 136 |
|
| 137 |
|
| 138 |
CIntervalThreadData *timerData = 0; |
| 139 |
mSyncTimerMutex.lock(); |
| 140 |
|
| 141 |
bool isNew = false; |
| 142 |
|
| 143 |
if (timers.contains(timerID)) { |
| 144 |
timerData = timers.value(timerID); |
| 145 |
} else { |
| 146 |
timerData = new CIntervalThreadData(); |
| 147 |
timers.insert(timerID,timerData); |
| 148 |
isNew = true; |
| 149 |
} |
| 150 |
|
| 151 |
if (deactivateTimer) { |
| 152 |
timerData->decDeactivateCount(); |
| 153 |
} |
| 154 |
|
| 155 |
bool reactivateTimer = false; |
| 156 |
|
| 157 |
if (timerInterval >= 0) { |
| 158 |
timerData->setInterval(timerInterval); |
| 159 |
bool isActive = timerData->isTimerActive(); |
| 160 |
if (isActive) { |
| 161 |
reactivateTimer = true; |
| 162 |
} |
| 163 |
} |
| 164 |
timerData->setRemainingTimerInvocations(remTimerInvocs); |
| 165 |
|
| 166 |
if (deactivateTimer) { |
| 167 |
reactivateTimer = false; |
| 168 |
|
| 169 |
int timerThreadID = timerData->getTimerThreadID(); |
| 170 |
bool isActive = timerData->isTimerActive(); |
| 171 |
if (isActive) { |
| 172 |
QObject::killTimer(timerThreadID); |
| 173 |
} |
| 174 |
timerData->setTimerActive(false); |
| 175 |
} |
| 176 |
|
| 177 |
if (activateTimer || reactivateTimer) { |
| 178 |
qint64 deactivateCount = timerData->getDeactivateCount(); |
| 179 |
if (deactivateCount <= 0) { |
| 180 |
bool isActive = timerData->isTimerActive(); |
| 181 |
int timerThreadID = timerData->getTimerThreadID(); |
| 182 |
if (isActive) { |
| 183 |
QObject::killTimer(timerThreadID); |
| 184 |
} |
| 185 |
timerThreadID = QObject::startTimer(timerData->getInterval()); |
| 186 |
timerData->setTimerThreadID(timerThreadID); |
| 187 |
timerData->setTimerActive(true); |
| 188 |
timerMapping.insert(timerThreadID,timerID); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
mSyncTimerMutex.unlock(); |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
}; // end namespace Concurrent |
| 202 |
|
| 203 |
}; // end namespace Konclude |