| 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 |
|
| 21 |
#include "CSingleThreadTaskProcessorUnit.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Scheduler { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CSingleThreadTaskProcessorUnit::CSingleThreadTaskProcessorUnit(CTaskHandleAlgorithm* taskHandleAlgo, CConsiderateMemoryPoolProvider* memoryPoolProvider) : CThread("TaskProcessingUnitThread") { |
| 31 |
mTaskProcessingQueue = nullptr; |
| 32 |
mTaskCompletionQueue = nullptr; |
| 33 |
mTaskStatusPropagator = nullptr; |
| 34 |
mCallbackExecuter = nullptr; |
| 35 |
mMemoryPoolProvider = memoryPoolProvider; |
| 36 |
if (!mMemoryPoolProvider) { |
| 37 |
mMemoryPoolProvider = new CNewAllocationMemoryPoolProvider(); |
| 38 |
} |
| 39 |
mMemoryAllocator = new CTaskHandleLimitedReserveMemoryPoolAllocationManager(mMemoryPoolProvider,30000,30); |
| 40 |
mTaskProcessorContext = new CTaskProcessorContextBase(this,mMemoryAllocator); |
| 41 |
mTaskHandleAlgo = taskHandleAlgo; |
| 42 |
mProcessingBlocked = true; |
| 43 |
mEventHandler = new CQueuedLinkedEventHandler(this,this); |
| 44 |
|
| 45 |
CXLinker<CEventHandler*>* eventHandlerLinker = new CXLinker<CEventHandler*>(mEventHandler,nullptr); |
| 46 |
mEventHandlerLinker = eventHandlerLinker; |
| 47 |
|
| 48 |
mProcessingStopped = false; |
| 49 |
|
| 50 |
mStatComputionTime = 0; |
| 51 |
mStatBlockingTime = 0; |
| 52 |
|
| 53 |
mLastProcessingStartedTag = 0; |
| 54 |
mLastProcessingStartRequestTag = 0; |
| 55 |
mTaskProcessingCount = 0; |
| 56 |
mTaskSchedulingCount = 0; |
| 57 |
mStatRecievedScheduleTasks = 0; |
| 58 |
mStatRemovedTasks = 0; |
| 59 |
mEventSignalized = false; |
| 60 |
mThreadBlocked = false; |
| 61 |
mDebugLastProcessedTask = nullptr; |
| 62 |
mDebugLastCompletedTask = nullptr; |
| 63 |
mTaskSchedulingQueue = nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
CSingleThreadTaskProcessorUnit::~CSingleThreadTaskProcessorUnit() { |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
cint64 CSingleThreadTaskProcessorUnit::countProcessingTasksMemoryPools() { |
| 71 |
cint64 memoryPoolCount = 0; |
| 72 |
CTask* taskProcessingQueueIt = mTaskProcessingQueue; |
| 73 |
while (taskProcessingQueueIt) { |
| 74 |
CMemoryPool* memoryPoolIt = taskProcessingQueueIt->getMemoryPools(); |
| 75 |
memoryPoolCount += memoryPoolIt->getCount(); |
| 76 |
taskProcessingQueueIt = taskProcessingQueueIt->getNext(); |
| 77 |
} |
| 78 |
return memoryPoolCount; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
cint64 CSingleThreadTaskProcessorUnit::countProcessedOpenTasksMemoryPools() { |
| 83 |
cint64 memoryPoolCount = 0; |
| 84 |
QSet<CTask*> taskSet; |
| 85 |
QList<CTask*> taskList; |
| 86 |
CTask* taskProcessingQueueIt = mTaskProcessingQueue; |
| 87 |
while (taskProcessingQueueIt) { |
| 88 |
if (taskProcessingQueueIt->getParentTask()) { |
| 89 |
if (!taskSet.contains(taskProcessingQueueIt->getParentTask())) { |
| 90 |
taskSet.insert(taskProcessingQueueIt->getParentTask()); |
| 91 |
taskList.append(taskProcessingQueueIt->getParentTask()); |
| 92 |
} |
| 93 |
} |
| 94 |
taskProcessingQueueIt = taskProcessingQueueIt->getNext(); |
| 95 |
} |
| 96 |
|
| 97 |
while (!taskList.isEmpty()) { |
| 98 |
CTask* task = taskList.takeFirst(); |
| 99 |
if (task->getParentTask()) { |
| 100 |
if (!taskSet.contains(task->getParentTask())) { |
| 101 |
taskSet.insert(task->getParentTask()); |
| 102 |
taskList.append(task->getParentTask()); |
| 103 |
} |
| 104 |
} |
| 105 |
CMemoryPool* memoryPoolIt = task->getMemoryPools(); |
| 106 |
memoryPoolCount += memoryPoolIt->getCount(); |
| 107 |
} |
| 108 |
|
| 109 |
return memoryPoolCount; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
cint64 CSingleThreadTaskProcessorUnit::closeOpenTasksMemoryPools() { |
| 117 |
cint64 closedMemoryPoolCount = 0; |
| 118 |
QSet<CTask*> taskSet; |
| 119 |
QList<CTask*> taskList; |
| 120 |
CTask* taskProcessingQueueIt = mTaskProcessingQueue; |
| 121 |
while (taskProcessingQueueIt) { |
| 122 |
if (!taskSet.contains(taskProcessingQueueIt)) { |
| 123 |
taskSet.insert(taskProcessingQueueIt); |
| 124 |
taskList.append(taskProcessingQueueIt); |
| 125 |
} |
| 126 |
taskProcessingQueueIt = taskProcessingQueueIt->getNext(); |
| 127 |
} |
| 128 |
|
| 129 |
while (!taskList.isEmpty()) { |
| 130 |
|
| 131 |
CTask* task = taskList.takeFirst(); |
| 132 |
CBooleanTaskResult* satResult = (CBooleanTaskResult*)task->getTaskResult(); |
| 133 |
if (satResult) { |
| 134 |
satResult->installResult(false); |
| 135 |
} |
| 136 |
CCallbackData* callbackLinkerIt = task->getCallbackLinker(); |
| 137 |
while (callbackLinkerIt) { |
| 138 |
CCallbackData* callback = callbackLinkerIt; |
| 139 |
callbackLinkerIt = callbackLinkerIt->getNext(); |
| 140 |
if (mCallbackExecuter) { |
| 141 |
mCallbackExecuter->executeCallback(task,callback); |
| 142 |
INCTASKPROCESSINGSTAT(mStats.incStatisticCallbacksExecutedCount(1)); |
| 143 |
} else { |
| 144 |
callback->doCallback(); |
| 145 |
} |
| 146 |
} |
| 147 |
if (task->getParentTask()) { |
| 148 |
if (!taskSet.contains(task->getParentTask())) { |
| 149 |
taskSet.insert(task->getParentTask()); |
| 150 |
taskList.append(task->getParentTask()); |
| 151 |
} |
| 152 |
} |
| 153 |
CMemoryPool* memoryPoolIt = task->getMemoryPools(); |
| 154 |
closedMemoryPoolCount += memoryPoolIt->getCount(); |
| 155 |
mMemoryAllocator->releaseMemoryPoolContainer(task); |
| 156 |
} |
| 157 |
|
| 158 |
mTaskProcessingQueue = 0; |
| 159 |
mTaskCompletionQueue = 0; |
| 160 |
|
| 161 |
return closedMemoryPoolCount; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
CTaskHandleAlgorithm* CSingleThreadTaskProcessorUnit::getTaskHandleAlgorithm() { |
| 166 |
return mTaskHandleAlgo; |
| 167 |
} |
| 168 |
|
| 169 |
CSingleThreadTaskProcessorUnit* CSingleThreadTaskProcessorUnit::installCallbackExecuter(CTaskCallbackExecuter* callbackExecuter) { |
| 170 |
mCallbackExecuter = callbackExecuter; |
| 171 |
return this; |
| 172 |
} |
| 173 |
|
| 174 |
CSingleThreadTaskProcessorUnit* CSingleThreadTaskProcessorUnit::installStatusPropagator(CTaskStatusPropagator* statusPropagator) { |
| 175 |
mTaskStatusPropagator = statusPropagator; |
| 176 |
return this; |
| 177 |
} |
| 178 |
|
| 179 |
cint64 CSingleThreadTaskProcessorUnit::getTaskProcessingCount() { |
| 180 |
return mTaskProcessingCount + mTaskSchedulingCount; |
| 181 |
} |
| 182 |
|
| 183 |
bool CSingleThreadTaskProcessorUnit::canDispenseProcessingTasks() { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
bool CSingleThreadTaskProcessorUnit::requiresProcessingTasks() { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
bool CSingleThreadTaskProcessorUnit::requiresSchedulingTasks() { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
cint64 CSingleThreadTaskProcessorUnit::countDispensableProcessingTasks() { |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
cint64 CSingleThreadTaskProcessorUnit::getRecievedTasks() { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
cint64 CSingleThreadTaskProcessorUnit::countScheduleableProcessingTasks() { |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
bool CSingleThreadTaskProcessorUnit::requiresTaskDispenseNotification(cint64 &updateDispenseNotificationTag) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
CTaskProcessingStatistics* CSingleThreadTaskProcessorUnit::getTaskProcessingStatistics() { |
| 213 |
return &mStats; |
| 214 |
} |
| 215 |
|
| 216 |
cint64 CSingleThreadTaskProcessorUnit::getStatisticBlockingTime() { |
| 217 |
cint64 blockTime = mStatBlockingTime; |
| 218 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 219 |
if (mThreadBlocked) { |
| 220 |
blockTime += mBlockingTimer.elapsed(); |
| 221 |
} |
| 222 |
#endif |
| 223 |
return blockTime; |
| 224 |
} |
| 225 |
|
| 226 |
cint64 CSingleThreadTaskProcessorUnit::getStatisticComputionTime() { |
| 227 |
cint64 compTime = mStatComputionTime; |
| 228 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 229 |
if (!mThreadBlocked) { |
| 230 |
compTime += mComputionTimer.elapsed(); |
| 231 |
} |
| 232 |
#endif |
| 233 |
return compTime; |
| 234 |
} |
| 235 |
|
| 236 |
CEventHandler* CSingleThreadTaskProcessorUnit::getEventHandler() { |
| 237 |
return mEventHandler; |
| 238 |
} |
| 239 |
|
| 240 |
CTaskEventHandlerBasedProcessor* CSingleThreadTaskProcessorUnit::installScheduler(CTaskEventHandlerBasedScheduler* scheduler) { |
| 241 |
return this; |
| 242 |
} |
| 243 |
|
| 244 |
CTaskSchedulerCommunicator* CSingleThreadTaskProcessorUnit::createSchedulerProcessorCommunication(CTaskEventHandlerBasedProcessor* taskProcessor) { |
| 245 |
return nullptr; |
| 246 |
} |
| 247 |
|
| 248 |
CTaskReserveQueueConsumer* CSingleThreadTaskProcessorUnit::createTaskReserveQueueConsumer(CTaskEventHandlerBasedProcessor* taskProcessor) { |
| 249 |
return nullptr; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
CSingleThreadTaskProcessorUnit* CSingleThreadTaskProcessorUnit::startProcessing() { |
| 254 |
if (!isRunning()) { |
| 255 |
startThread(); |
| 256 |
postEvent(new Concurrent::Events::CHandleEventsEvent()); |
| 257 |
} |
| 258 |
return this; |
| 259 |
} |
| 260 |
|
| 261 |
CSingleThreadTaskProcessorUnit* CSingleThreadTaskProcessorUnit::stopProcessing() { |
| 262 |
mProcessingStopped = true; |
| 263 |
return this; |
| 264 |
} |
| 265 |
|
| 266 |
CThreadActivator* CSingleThreadTaskProcessorUnit::signalizeEvent() { |
| 267 |
mEventSignalized = true; |
| 268 |
if (mProcessingBlocked) { |
| 269 |
// reactivate processing |
| 270 |
if (mLastProcessingStartedTag == mLastProcessingStartRequestTag) { |
| 271 |
++mLastProcessingStartRequestTag; |
| 272 |
mProcessingWakeUpSemaphore.release(); |
| 273 |
} |
| 274 |
} |
| 275 |
return this; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
bool CSingleThreadTaskProcessorUnit::processControlEvents(QEvent::Type type, CControlEvent *event) { |
| 280 |
if (CThread::processControlEvents(type,event)) { |
| 281 |
return true; |
| 282 |
} else { |
| 283 |
if (type == Concurrent::Events::CHandleEventsEvent::EVENTTYPE) { |
| 284 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 285 |
mComputionTimer.start(); |
| 286 |
#endif |
| 287 |
processingLoop(); |
| 288 |
return true; |
| 289 |
} |
| 290 |
} |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
bool CSingleThreadTaskProcessorUnit::processingLoop() { |
| 295 |
bool eventSafeguardProcessed = false; |
| 296 |
while (!mProcessingStopped) { |
| 297 |
if (!mTaskProcessingQueue && mProcessingBlocked) { |
| 298 |
// block until new events or task are available |
| 299 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 300 |
mStatComputionTime += mComputionTimer.elapsed(); |
| 301 |
mBlockingTimer.start(); |
| 302 |
#endif |
| 303 |
INCTASKPROCESSINGSTAT(mStats.incStatisticThreadsBlockedCount(1)); |
| 304 |
mThreadBlocked = true; |
| 305 |
mProcessingWakeUpSemaphore.acquire(1); |
| 306 |
mThreadBlocked = false; |
| 307 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 308 |
mStatBlockingTime += mBlockingTimer.elapsed(); |
| 309 |
mComputionTimer.start(); |
| 310 |
#endif |
| 311 |
mLastProcessingStartedTag = mLastProcessingStartRequestTag; |
| 312 |
} |
| 313 |
mProcessingBlocked = false; |
| 314 |
eventSafeguardProcessed = false; |
| 315 |
while (mEventSignalized) { |
| 316 |
bool eventsProcessed = handleEvents(); |
| 317 |
} |
| 318 |
if (mTaskProcessingQueue) { |
| 319 |
CTask* processingTask = mTaskProcessingQueue; |
| 320 |
mTaskProcessingQueue = mTaskProcessingQueue->getNext(); |
| 321 |
cint64 taskDepth = processingTask->getTaskDepth(); |
| 322 |
|
| 323 |
//cint64 memoryPoolCount1 = countProcessingTasksMemoryPools(); |
| 324 |
//cint64 memoryPoolCount2 = countProcessedOpenTasksMemoryPools(); |
| 325 |
|
| 326 |
bool continueProcessing = processTask(processingTask); |
| 327 |
if (continueProcessing) { |
| 328 |
processingTask->getTaskStatus()->setTaskQUEUEDState(); |
| 329 |
if (mTaskProcessingQueue) { |
| 330 |
mTaskProcessingQueue = mTaskProcessingQueue->insertNextSorted(processingTask); |
| 331 |
} else { |
| 332 |
mTaskProcessingQueue = processingTask; |
| 333 |
} |
| 334 |
} else { |
| 335 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksProcessedDepthCount(taskDepth,1)); |
| 336 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksProcessedCount(1)); |
| 337 |
--mTaskProcessingCount; |
| 338 |
++mStatRemovedTasks; |
| 339 |
} |
| 340 |
|
| 341 |
//if (mStatRemovedTasks > 100000) { |
| 342 |
// closeOpenTasksMemoryPools(); |
| 343 |
//} |
| 344 |
} |
| 345 |
if (!mTaskProcessingQueue && mTaskSchedulingQueue) { |
| 346 |
CTask* nextTask = mTaskSchedulingQueue; |
| 347 |
mTaskSchedulingQueue = mTaskSchedulingQueue->getNext(); |
| 348 |
nextTask->clearNext(); |
| 349 |
--mTaskSchedulingCount; |
| 350 |
addProcessingTask(nextTask); |
| 351 |
} |
| 352 |
|
| 353 |
if (!mTaskProcessingQueue) { |
| 354 |
while (mEventSignalized || !eventSafeguardProcessed) { |
| 355 |
bool eventsProcessed = handleEvents(); |
| 356 |
if (eventsProcessed) { |
| 357 |
eventSafeguardProcessed = false; |
| 358 |
mProcessingBlocked = false; |
| 359 |
} else if (!mProcessingBlocked) { |
| 360 |
mProcessingBlocked = true; |
| 361 |
} else if (!eventSafeguardProcessed) { |
| 362 |
eventSafeguardProcessed = true; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
bool CSingleThreadTaskProcessorUnit::processEvent(CEvent *event, CContext* handlerContext) { |
| 371 |
cint64 eventID = event->getEventTypeID(); |
| 372 |
if (eventID == CSendTaskProcessEvent::EVENTTYPEID) { |
| 373 |
CTask* task = ((CSendTaskProcessEvent*)event)->getTask(); |
| 374 |
addProcessingTask(task); |
| 375 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 376 |
return true; |
| 377 |
} else if (eventID == CSendTaskScheduleEvent::EVENTTYPEID) { |
| 378 |
++mStatRecievedScheduleTasks; |
| 379 |
CTask* task = ((CSendTaskScheduleEvent*)event)->getTask(); |
| 380 |
if (!mTaskProcessingQueue) { |
| 381 |
addProcessingTask(task); |
| 382 |
} else { |
| 383 |
++mTaskSchedulingCount; |
| 384 |
if (mTaskSchedulingQueue) { |
| 385 |
mTaskSchedulingQueue = mTaskSchedulingQueue->append(task); |
| 386 |
} else { |
| 387 |
mTaskSchedulingQueue = task; |
| 388 |
} |
| 389 |
} |
| 390 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 391 |
return true; |
| 392 |
} else if (eventID == CRequestProcessTaskEvent::EVENTTYPEID) { |
| 393 |
// not supported |
| 394 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 395 |
return true; |
| 396 |
} else if (eventID == CResponseScheduleTaskEvent::EVENTTYPEID) { |
| 397 |
// not supported |
| 398 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 399 |
return true; |
| 400 |
} else if (eventID == CSendTaskCompleteEvent::EVENTTYPEID) { |
| 401 |
CTask* task = ((CSendTaskCompleteEvent*)event)->getTask(); |
| 402 |
completeTask(task); |
| 403 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 404 |
return true; |
| 405 |
} else if (eventID == CTaskAdditionalAllocationEvent::EVENTTYPEID) { |
| 406 |
CTaskAdditionalAllocationEvent* taskAddAllocEvent = (CTaskAdditionalAllocationEvent*)event; |
| 407 |
CTask* task = taskAddAllocEvent->getTask(); |
| 408 |
CMemoryPool* addMemoryPools = taskAddAllocEvent->getAdditionalAllocatedMemoryPools(); |
| 409 |
task->appendMemoryPool(addMemoryPools); |
| 410 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 411 |
return true; |
| 412 |
} else if (eventID == CRequestScheduleTaskEvent::EVENTTYPEID) { |
| 413 |
// this unit is also the scheduler |
| 414 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 415 |
return true; |
| 416 |
} else { |
| 417 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 418 |
return true; |
| 419 |
} |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
CTaskProcessorCommunicator* CSingleThreadTaskProcessorUnit::communicateTaskComplete(CTask* task) { |
| 425 |
task->getTaskStatus()->setTaskFINISHEDState(); |
| 426 |
completeTask(task); |
| 427 |
return this; |
| 428 |
} |
| 429 |
|
| 430 |
CTaskProcessorCommunicator* CSingleThreadTaskProcessorUnit::communicateTaskError(CTask* task) { |
| 431 |
task->getTaskStatus()->setTaskFINISHEDState(); |
| 432 |
completeTask(task); |
| 433 |
return this; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
bool CSingleThreadTaskProcessorUnit::addProcessingTask(CTask* task) { |
| 438 |
CTask* newTask = task; |
| 439 |
CTask* newTaskIt = newTask; |
| 440 |
cint64 taskCount = 0; |
| 441 |
while (newTaskIt) { |
| 442 |
newTaskIt->getTaskStatus()->setTaskQUEUEDState(); |
| 443 |
newTaskIt = newTaskIt->getNext(); |
| 444 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksAddedCount(1)); |
| 445 |
++taskCount; |
| 446 |
} |
| 447 |
mTaskProcessingCount += taskCount; |
| 448 |
if (!mTaskProcessingQueue) { |
| 449 |
mTaskProcessingQueue = newTask; |
| 450 |
newTask = newTask->getNext(); |
| 451 |
mTaskProcessingQueue->clearNext(); |
| 452 |
} |
| 453 |
if (newTask) { |
| 454 |
mTaskProcessingQueue = mTaskProcessingQueue->insertNextSorted(newTask); |
| 455 |
} |
| 456 |
return task != nullptr; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
CTaskProcessorCommunicator* CSingleThreadTaskProcessorUnit::communicateTaskCreation(CTask* newTask) { |
| 461 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksCreatedCount(newTask->getCount())); |
| 462 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksCreatedDepthCount(newTask->getTaskDepth(),newTask->getCount())); |
| 463 |
|
| 464 |
addProcessingTask(newTask); |
| 465 |
return this; |
| 466 |
} |
| 467 |
|
| 468 |
CTaskProcessorCommunicator* CSingleThreadTaskProcessorUnit::communicateTaskAdditionalAllocation(CTask* task, CMemoryPool* additionalAllocatedMemoryPool) { |
| 469 |
task->appendMemoryPool(additionalAllocatedMemoryPool); |
| 470 |
return this; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
bool CSingleThreadTaskProcessorUnit::verifyContinueTaskProcessing(CTask* task) { |
| 475 |
if (mEventSignalized) { |
| 476 |
return false; |
| 477 |
} |
| 478 |
if (!task->getTaskStatus()->isProcessable()) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
if (mTaskProcessingQueue && mTaskProcessingQueue->getTaskPriority() > task->getTaskPriority()) { |
| 482 |
return false; |
| 483 |
} |
| 484 |
return true; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
bool CSingleThreadTaskProcessorUnit::handleEvents() { |
| 489 |
bool handleNextRound = true; |
| 490 |
bool roundEventProcessed = false; |
| 491 |
bool eventProcessed = false; |
| 492 |
while (handleNextRound && !mProcessingStopped) { |
| 493 |
roundEventProcessed = false; |
| 494 |
CXLinker<CEventHandler*>* eventHandlerLinkerIt = mEventHandlerLinker; |
| 495 |
if (eventHandlerLinkerIt) { |
| 496 |
|
| 497 |
CEventHandler* eventHandler = eventHandlerLinkerIt->getData(); |
| 498 |
if (eventHandler->needEventProcessing()) { |
| 499 |
cint64 handledEventCount = eventHandler->handleEvents(mTaskProcessorContext); |
| 500 |
INCTASKPROCESSINGSTAT(mStats.incStatisticEventsProcessedCount(handledEventCount)); |
| 501 |
roundEventProcessed = handledEventCount > 0; |
| 502 |
} |
| 503 |
// round robin |
| 504 |
eventHandlerLinkerIt = eventHandlerLinkerIt->getNext(); |
| 505 |
if (!eventHandlerLinkerIt) { |
| 506 |
if (mEventSignalized || roundEventProcessed) { |
| 507 |
handleNextRound = true; |
| 508 |
mEventSignalized = false; |
| 509 |
} else { |
| 510 |
handleNextRound = false; |
| 511 |
} |
| 512 |
eventHandlerLinkerIt = mEventHandlerLinker; |
| 513 |
} |
| 514 |
eventProcessed |= roundEventProcessed; |
| 515 |
} else { |
| 516 |
handleNextRound = false; |
| 517 |
} |
| 518 |
} |
| 519 |
return eventProcessed; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
cint64 CSingleThreadTaskProcessorUnit::completeTask(CTask* task) { |
| 525 |
cint64 completedCount = 0; |
| 526 |
mTaskCompletionQueue = task->getLastListLink()->setNext(mTaskCompletionQueue); |
| 527 |
while (mTaskCompletionQueue) { |
| 528 |
CTask* completionTask = mTaskCompletionQueue; |
| 529 |
mTaskCompletionQueue = mTaskCompletionQueue->getNext(); |
| 530 |
|
| 531 |
if (completionTask && !completionTask->hasActiveReferencedTask()) { |
| 532 |
mDebugLastCompletedTask = completionTask; |
| 533 |
|
| 534 |
CTask* parentTask = completionTask->getParentTask(); |
| 535 |
bool upPropagation = false; |
| 536 |
if (mTaskStatusPropagator && mTaskStatusPropagator->completeTaskStatus(completionTask,upPropagation)) { |
| 537 |
if (parentTask && upPropagation) { |
| 538 |
updateTaskStatus(parentTask); |
| 539 |
} |
| 540 |
} |
| 541 |
completionTask->completeTask(); |
| 542 |
completionTask->getTaskStatus()->setTaskCOMPLETEDState(); |
| 543 |
bool memoryReleaseable = completionTask->getTaskStatus()->isMemoryReleaseable(); |
| 544 |
if (parentTask) { |
| 545 |
parentTask->decActiveReferenceCount(); |
| 546 |
if (!parentTask->hasActiveReferencedTask()) { |
| 547 |
mTaskCompletionQueue = parentTask->setNext(mTaskCompletionQueue); |
| 548 |
} |
| 549 |
} |
| 550 |
if (completionTask) { |
| 551 |
// callbacks |
| 552 |
CCallbackData* callbackLinkerIt = completionTask->getCallbackLinker(); |
| 553 |
while (callbackLinkerIt) { |
| 554 |
CCallbackData* callback = callbackLinkerIt; |
| 555 |
callbackLinkerIt = callbackLinkerIt->getNext(); |
| 556 |
if (mCallbackExecuter) { |
| 557 |
mCallbackExecuter->executeCallback(completionTask,callback); |
| 558 |
INCTASKPROCESSINGSTAT(mStats.incStatisticCallbacksExecutedCount(1)); |
| 559 |
} else { |
| 560 |
callback->doCallback(); |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
if (memoryReleaseable) { |
| 565 |
for (CDeletionLinker* deletionLinkerIt = completionTask->takeDeletionLinker(); deletionLinkerIt; deletionLinkerIt = deletionLinkerIt->getNext()) { |
| 566 |
deletionLinkerIt->deleteObject(); |
| 567 |
} |
| 568 |
mMemoryAllocator->releaseMemoryPoolContainer(completionTask); |
| 569 |
} |
| 570 |
completedCount++; |
| 571 |
} |
| 572 |
} |
| 573 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksCompletedCount(completedCount)); |
| 574 |
return completedCount; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
bool CSingleThreadTaskProcessorUnit::processTask(CTask* task) { |
| 579 |
mDebugLastProcessedTask = task; |
| 580 |
task->clearNext(); |
| 581 |
task->getTaskStatus()->setTaskPROCESSINGState(); |
| 582 |
return mTaskHandleAlgo->handleTask(mTaskProcessorContext,task); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
CTaskProcessorCommunicator* CSingleThreadTaskProcessorUnit::communicateTaskRelevant(CTask* task) { |
| 587 |
// nothing to do for single processor unit |
| 588 |
return this; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
CTaskProcessorCommunicator* CSingleThreadTaskProcessorUnit::communicateTaskStatusUpdate(CTask* task) { |
| 593 |
updateTaskStatus(task); |
| 594 |
return this; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
bool CSingleThreadTaskProcessorUnit::updateTaskStatusDown(CTask* task, cint64 depth) { |
| 599 |
if (depth < 1000) { |
| 600 |
bool downProp = false; |
| 601 |
bool upProp = false; |
| 602 |
CTask* updateTask = task; |
| 603 |
updateTask->setTaskID(-1); |
| 604 |
if (mTaskStatusPropagator && mTaskStatusPropagator->updateTaskStatus(updateTask, downProp, upProp)) { |
| 605 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksUpdatedCount(1)); |
| 606 |
|
| 607 |
if (downProp) { |
| 608 |
CXNegLinker<CTask*>* refTaskIt = updateTask->getReferencedTaskLinker(); |
| 609 |
while (refTaskIt) { |
| 610 |
if (refTaskIt->isNegated()) { |
| 611 |
// task still exists, update task status |
| 612 |
// TODO: check better not recursive? |
| 613 |
CTask* refTask = refTaskIt->getData(); |
| 614 |
updateTaskStatusDown(refTask, depth + 1); |
| 615 |
} |
| 616 |
refTaskIt = refTaskIt->getNext(); |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
} else { |
| 622 |
QList<CTask*> updateTaskList; |
| 623 |
updateTaskList.append(task); |
| 624 |
while (!updateTaskList.isEmpty()) { |
| 625 |
bool downProp = false; |
| 626 |
bool upProp = false; |
| 627 |
CTask* updateTask = updateTaskList.takeFirst(); |
| 628 |
if (mTaskStatusPropagator && mTaskStatusPropagator->updateTaskStatus(updateTask, downProp, upProp)) { |
| 629 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksUpdatedCount(1)); |
| 630 |
|
| 631 |
if (downProp) { |
| 632 |
CXNegLinker<CTask*>* refTaskIt = updateTask->getReferencedTaskLinker(); |
| 633 |
while (refTaskIt) { |
| 634 |
if (refTaskIt->isNegated()) { |
| 635 |
// task still exists, update task status |
| 636 |
// TODO: check better not recursive? |
| 637 |
CTask* refTask = refTaskIt->getData(); |
| 638 |
updateTaskList.append(refTask); |
| 639 |
} |
| 640 |
refTaskIt = refTaskIt->getNext(); |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
} |
| 646 |
} |
| 647 |
return true; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
bool CSingleThreadTaskProcessorUnit::updateTaskStatus(CTask* task) { |
| 652 |
CTask* updateTask = task; |
| 653 |
bool downProp = false; |
| 654 |
bool upProp = false; |
| 655 |
while (updateTask) { |
| 656 |
if (mTaskStatusPropagator && mTaskStatusPropagator->updateTaskStatus(updateTask,downProp,upProp)) { |
| 657 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksUpdatedCount(1)); |
| 658 |
if (downProp) { |
| 659 |
CXNegLinker<CTask*>* refTaskIt = updateTask->getReferencedTaskLinker(); |
| 660 |
while (refTaskIt) { |
| 661 |
if (refTaskIt->isNegated()) { |
| 662 |
// task still exists, update task status |
| 663 |
// TODO: check better not recursive? |
| 664 |
CTask* refTask = refTaskIt->getData(); |
| 665 |
updateTaskStatusDown(refTask, 0); |
| 666 |
} |
| 667 |
refTaskIt = refTaskIt->getNext(); |
| 668 |
} |
| 669 |
|
| 670 |
} |
| 671 |
if (upProp) { |
| 672 |
// all parent task are owned by the completer |
| 673 |
//CTask* parentTask = updateTask->getParentTask(); |
| 674 |
//if (parentTask) { |
| 675 |
// updateTaskStatus(parentTask); |
| 676 |
//} |
| 677 |
updateTask = updateTask->getParentTask(); |
| 678 |
} else { |
| 679 |
updateTask = nullptr; |
| 680 |
} |
| 681 |
} else { |
| 682 |
updateTask = nullptr; |
| 683 |
} |
| 684 |
} |
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
}; // end namespace Scheduler |
| 691 |
|
| 692 |
}; // end namespace Konclude |