| 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 "CTaskProcessorThreadBase.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Scheduler { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CTaskProcessorThreadBase::CTaskProcessorThreadBase(CTaskHandleAlgorithm* taskHandleAlgo, const QString &threadStringName) : CThread(threadStringName) { |
| 31 |
mTaskProcessingQueue = nullptr; |
| 32 |
mTaskSchedulingQueue = nullptr; |
| 33 |
mTaskCompletionQueue = nullptr; |
| 34 |
mMemoryAllocator = nullptr; |
| 35 |
mTaskProcessorContext = nullptr; |
| 36 |
mTaskStatusPropagator = nullptr; |
| 37 |
mCallbackExecuter = nullptr; |
| 38 |
mTaskHandleAlgo = taskHandleAlgo; |
| 39 |
mProcessingBlocked = true; |
| 40 |
mEventHandler = new CQueuedLinkedEventHandler(this,this); |
| 41 |
|
| 42 |
CXLinker<CEventHandler*>* eventHandlerLinker = new CXLinker<CEventHandler*>(mEventHandler,nullptr); |
| 43 |
mEventHandlerLinker = eventHandlerLinker; |
| 44 |
|
| 45 |
mTaskReserveQueueConsumer = nullptr; |
| 46 |
|
| 47 |
mProcessingStopped = false; |
| 48 |
mThreadBlocked = false; |
| 49 |
|
| 50 |
mStatComputionTime = 0; |
| 51 |
mStatBlockingTime = 0; |
| 52 |
|
| 53 |
mLastProcessingStartedTag = 0; |
| 54 |
mLastProcessingStartRequestTag = 0; |
| 55 |
mTaskProcessingCount = 0; |
| 56 |
mEventSignalized = false; |
| 57 |
mProcessTaskRequesting = true; |
| 58 |
|
| 59 |
mTaskSchedulingCount = 0; |
| 60 |
mCriticalSchedulingCount = 3; |
| 61 |
mCriticalSchedulingThresholdCount = 5; |
| 62 |
mCriticalSchedulingNotified = true; |
| 63 |
mRecievedTasks = 0; |
| 64 |
mTaskReserveQueueCount = 0; |
| 65 |
mTaskParentRelevantCount = 0; |
| 66 |
|
| 67 |
mConfScheduleTasksDistribution = false; |
| 68 |
mConfProcessingTasksDistribution = false; |
| 69 |
mConfUseReserveTasksQueue = true; |
| 70 |
|
| 71 |
mMinimalPriorityTaskInvalidated = false; |
| 72 |
|
| 73 |
mMinTaskPriority = 0.; |
| 74 |
mMinPriorityTask = nullptr; |
| 75 |
|
| 76 |
mConfProcessSchedulingTasks = true; |
| 77 |
mConfProcessReserveTasks = true; |
| 78 |
|
| 79 |
// for debugging only |
| 80 |
mLastProcessedTask = nullptr; |
| 81 |
mLastDispendedTask = nullptr; |
| 82 |
mLastProcessedEvent = nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
CTaskProcessorThreadBase::~CTaskProcessorThreadBase() { |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
CTaskHandleAlgorithm* CTaskProcessorThreadBase::getTaskHandleAlgorithm() { |
| 90 |
return mTaskHandleAlgo; |
| 91 |
} |
| 92 |
|
| 93 |
bool CTaskProcessorThreadBase::isProcessing() { |
| 94 |
return !mProcessingBlocked; |
| 95 |
} |
| 96 |
|
| 97 |
bool CTaskProcessorThreadBase::isBlocked() { |
| 98 |
return mProcessingBlocked; |
| 99 |
} |
| 100 |
|
| 101 |
CTaskProcessorThreadBase* CTaskProcessorThreadBase::installCallbackExecuter(CTaskCallbackExecuter* callbackExecuter) { |
| 102 |
mCallbackExecuter = callbackExecuter; |
| 103 |
return this; |
| 104 |
} |
| 105 |
|
| 106 |
CTaskProcessorThreadBase* CTaskProcessorThreadBase::installStatusPropagator(CTaskStatusPropagator* statusPropagator) { |
| 107 |
mTaskStatusPropagator = statusPropagator; |
| 108 |
return this; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
CTaskProcessingStatistics* CTaskProcessorThreadBase::getTaskProcessingStatistics() { |
| 113 |
return &mStats; |
| 114 |
} |
| 115 |
|
| 116 |
cint64 CTaskProcessorThreadBase::getStatisticBlockingTime() { |
| 117 |
cint64 blockTime = mStatBlockingTime; |
| 118 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 119 |
if (mThreadBlocked) { |
| 120 |
blockTime += mBlockingTimer.elapsed(); |
| 121 |
} |
| 122 |
#endif |
| 123 |
return blockTime; |
| 124 |
} |
| 125 |
|
| 126 |
cint64 CTaskProcessorThreadBase::getStatisticComputionTime() { |
| 127 |
cint64 compTime = mStatComputionTime; |
| 128 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 129 |
if (!mThreadBlocked) { |
| 130 |
compTime += mComputionTimer.elapsed(); |
| 131 |
} |
| 132 |
#endif |
| 133 |
return compTime; |
| 134 |
} |
| 135 |
|
| 136 |
CEventHandler* CTaskProcessorThreadBase::getEventHandler() { |
| 137 |
return mEventHandler; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
bool CTaskProcessorThreadBase::canDispenseProcessingTasks() { |
| 142 |
return mTaskProcessingCount+mTaskSchedulingCount >= 2; |
| 143 |
} |
| 144 |
|
| 145 |
bool CTaskProcessorThreadBase::requiresProcessingTasks() { |
| 146 |
return mTaskProcessingCount+mTaskSchedulingCount <= 0; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
bool CTaskProcessorThreadBase::requiresSchedulingTasks() { |
| 151 |
return mCriticalSchedulingNotified; |
| 152 |
} |
| 153 |
|
| 154 |
cint64 CTaskProcessorThreadBase::countDispensableProcessingTasks() { |
| 155 |
return mTaskProcessingCount+mTaskSchedulingCount; |
| 156 |
} |
| 157 |
|
| 158 |
cint64 CTaskProcessorThreadBase::countScheduleableProcessingTasks() { |
| 159 |
return mTaskSchedulingCount; |
| 160 |
} |
| 161 |
|
| 162 |
cint64 CTaskProcessorThreadBase::getRecievedTasks() { |
| 163 |
return mRecievedTasks; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
CTaskProcessorThreadBase* CTaskProcessorThreadBase::startProcessing() { |
| 168 |
if (!isRunning()) { |
| 169 |
startThread(QThread::HighPriority); |
| 170 |
postEvent(new Concurrent::Events::CHandleEventsEvent()); |
| 171 |
} |
| 172 |
return this; |
| 173 |
} |
| 174 |
|
| 175 |
CTaskProcessorThreadBase* CTaskProcessorThreadBase::stopProcessing() { |
| 176 |
mProcessingStopped = true; |
| 177 |
return this; |
| 178 |
} |
| 179 |
|
| 180 |
CThreadActivator* CTaskProcessorThreadBase::signalizeEvent() { |
| 181 |
mEventSignalized = true; |
| 182 |
if (mProcessingBlocked) { |
| 183 |
// reactivate processing |
| 184 |
if (mLastProcessingStartedTag == mLastProcessingStartRequestTag) { |
| 185 |
++mLastProcessingStartRequestTag; |
| 186 |
mProcessingWakeUpSemaphore.release(); |
| 187 |
} |
| 188 |
} |
| 189 |
return this; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
bool CTaskProcessorThreadBase::processControlEvents(QEvent::Type type, CControlEvent *event) { |
| 194 |
if (CThread::processControlEvents(type,event)) { |
| 195 |
return true; |
| 196 |
} else { |
| 197 |
if (type == Concurrent::Events::CHandleEventsEvent::EVENTTYPE) { |
| 198 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 199 |
mComputionTimer.start(); |
| 200 |
#endif |
| 201 |
processingLoop(); |
| 202 |
return true; |
| 203 |
} |
| 204 |
} |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
bool CTaskProcessorThreadBase::processingLoop() { |
| 209 |
bool eventSafeguardProcessed = false; |
| 210 |
while (!mProcessingStopped) { |
| 211 |
if (!mTaskProcessingQueue && mProcessingBlocked) { |
| 212 |
// block until new events or task are available |
| 213 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 214 |
mStatComputionTime += mComputionTimer.elapsed(); |
| 215 |
mBlockingTimer.start(); |
| 216 |
#endif |
| 217 |
INCTASKPROCESSINGSTAT(mStats.incStatisticThreadsBlockedCount(1)); |
| 218 |
mThreadBlocked = true; |
| 219 |
mProcessingWakeUpSemaphore.tryAcquire(1,100); |
| 220 |
mThreadBlocked = false; |
| 221 |
#ifdef KONCLUDE_SCHEDULER_TASK_THREADS_TIME_STATISTICS |
| 222 |
mStatBlockingTime += mBlockingTimer.elapsed(); |
| 223 |
mComputionTimer.start(); |
| 224 |
#endif |
| 225 |
mLastProcessingStartedTag = mLastProcessingStartRequestTag; |
| 226 |
} |
| 227 |
mProcessingBlocked = false; |
| 228 |
eventSafeguardProcessed = false; |
| 229 |
while (mEventSignalized) { |
| 230 |
bool eventsProcessed = handleEvents(); |
| 231 |
} |
| 232 |
if (mTaskProcessingQueue) { |
| 233 |
CTask* processingTask = mTaskProcessingQueue; |
| 234 |
mTaskProcessingQueue = mTaskProcessingQueue->getNext(); |
| 235 |
--mTaskProcessingCount; |
| 236 |
cint64 taskDepth = processingTask->getTaskDepth(); |
| 237 |
bool continueProcessing = processTask(processingTask); |
| 238 |
if (continueProcessing) { |
| 239 |
addProcessingTask(processingTask); |
| 240 |
} else { |
| 241 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksProcessedDepthCount(taskDepth,1)); |
| 242 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksProcessedCount(1)); |
| 243 |
} |
| 244 |
} |
| 245 |
if (!mTaskProcessingQueue && mTaskSchedulingQueue && mConfProcessSchedulingTasks) { |
| 246 |
CTask* nextTask = mTaskSchedulingQueue; |
| 247 |
mTaskSchedulingQueue = mTaskSchedulingQueue->getNext(); |
| 248 |
nextTask->clearNext(); |
| 249 |
--mTaskSchedulingCount; |
| 250 |
addProcessingTask(nextTask); |
| 251 |
if (!mCriticalSchedulingNotified && mTaskSchedulingCount <= mCriticalSchedulingCount) { |
| 252 |
mCriticalSchedulingNotified = true; |
| 253 |
processCriticalScheduleTask(); |
| 254 |
} |
| 255 |
} |
| 256 |
if (!mTaskProcessingQueue && mConfProcessReserveTasks) { |
| 257 |
if (mConfUseReserveTasksQueue && mTaskReserveQueueConsumer) { |
| 258 |
CTask* task = mTaskReserveQueueConsumer->getTask(); |
| 259 |
if (task) { |
| 260 |
++mTaskReserveQueueCount; |
| 261 |
addProcessingTask(task); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
processDispenseNotification(); |
| 266 |
if (!mTaskProcessingQueue) { |
| 267 |
while (mEventSignalized || !eventSafeguardProcessed) { |
| 268 |
bool eventsProcessed = handleEvents(); |
| 269 |
if (eventsProcessed) { |
| 270 |
eventSafeguardProcessed = false; |
| 271 |
mProcessingBlocked = false; |
| 272 |
} else if (!mProcessingBlocked) { |
| 273 |
mProcessingBlocked = true; |
| 274 |
} else if (!eventSafeguardProcessed) { |
| 275 |
eventSafeguardProcessed = true; |
| 276 |
} |
| 277 |
} |
| 278 |
if (!mTaskProcessingQueue && !mProcessTaskRequesting) { |
| 279 |
mProcessTaskRequesting = true; |
| 280 |
processRequestProcessTask(); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
bool CTaskProcessorThreadBase::addProcessingTask(CTask* task) { |
| 288 |
if (task) { |
| 289 |
|
| 290 |
CTask* taskIt = task; |
| 291 |
cint64 taskCount = 0; |
| 292 |
double minPriority = 0.; |
| 293 |
CTask* minPriorityTask = nullptr; |
| 294 |
while (taskIt) { |
| 295 |
taskIt->getTaskStatus()->setTaskQUEUEDState(); |
| 296 |
if (!minPriorityTask || taskIt->getTaskPriority() < minPriority) { |
| 297 |
minPriority = taskIt->getTaskPriority(); |
| 298 |
minPriorityTask = taskIt; |
| 299 |
} |
| 300 |
++taskCount; |
| 301 |
taskIt = taskIt->getNext(); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
if (minPriorityTask) { |
| 306 |
if (minPriority < mMinTaskPriority) { |
| 307 |
mMinimalPriorityTaskInvalidated = true; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
mTaskProcessingCount += taskCount; |
| 312 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksAddedCount(1)); |
| 313 |
|
| 314 |
if (!mTaskProcessingQueue) { |
| 315 |
mTaskProcessingQueue = task; |
| 316 |
task = task->getNext(); |
| 317 |
mTaskProcessingQueue->clearNext(); |
| 318 |
} |
| 319 |
if (task) { |
| 320 |
mTaskProcessingQueue = mTaskProcessingQueue->insertNextSorted(task); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
mProcessTaskRequesting = false; |
| 325 |
|
| 326 |
return true; |
| 327 |
} |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
bool CTaskProcessorThreadBase::processEvent(CEvent *event, CContext* handlerContext) { |
| 333 |
mLastProcessedEvent = event; |
| 334 |
cint64 eventID = event->getEventTypeID(); |
| 335 |
if (eventID == CSendTaskProcessEvent::EVENTTYPEID) { |
| 336 |
CSendTaskProcessEvent* sendTaskProcessEvent = (CSendTaskProcessEvent*)event; |
| 337 |
CTask* task = sendTaskProcessEvent->getTask(); |
| 338 |
bool schedulable = sendTaskProcessEvent->isSchedulingTask(); |
| 339 |
++mRecievedTasks; |
| 340 |
if (!mTaskProcessingQueue || !schedulable) { |
| 341 |
addProcessingTask(task); |
| 342 |
} else { |
| 343 |
++mTaskSchedulingCount; |
| 344 |
if (mTaskSchedulingQueue) { |
| 345 |
mTaskSchedulingQueue = mTaskSchedulingQueue->append(task); |
| 346 |
} else { |
| 347 |
mTaskSchedulingQueue = task; |
| 348 |
} |
| 349 |
if (mTaskSchedulingCount >= mCriticalSchedulingThresholdCount) { |
| 350 |
mCriticalSchedulingNotified = false; |
| 351 |
} |
| 352 |
} |
| 353 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 354 |
return true; |
| 355 |
} else if (eventID == CSendTaskCompleteEvent::EVENTTYPEID) { |
| 356 |
CTask* task = ((CSendTaskCompleteEvent*)event)->getTask(); |
| 357 |
processCompleteTask(task); |
| 358 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 359 |
return true; |
| 360 |
} else if (eventID == CTaskAdditionalAllocationEvent::EVENTTYPEID) { |
| 361 |
CTaskAdditionalAllocationEvent* taskAddAllocEvent = (CTaskAdditionalAllocationEvent*)event; |
| 362 |
CTask* task = taskAddAllocEvent->getTask(); |
| 363 |
CMemoryPool* addMemoryPools = taskAddAllocEvent->getAdditionalAllocatedMemoryPools(); |
| 364 |
processAdditionalTaskAllocation(task,addMemoryPools); |
| 365 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 366 |
return true; |
| 367 |
} else if (eventID == CRequestScheduleTaskEvent::EVENTTYPEID) { |
| 368 |
CTaskScheduleItem* taskScheduleItem = ((CRequestScheduleTaskEvent*)event)->getTaskScheduleItem(); |
| 369 |
mMemoryAllocator->releaseMemoryPoolContainer(event); |
| 370 |
CTask* responseTask = mTaskSchedulingQueue; |
| 371 |
bool schedulingTask = false; |
| 372 |
if (responseTask) { |
| 373 |
--mTaskSchedulingCount; |
| 374 |
schedulingTask = true; |
| 375 |
mTaskSchedulingQueue = mTaskSchedulingQueue->getNext(); |
| 376 |
if (!mCriticalSchedulingNotified && mTaskSchedulingCount <= mCriticalSchedulingCount) { |
| 377 |
mCriticalSchedulingNotified = true; |
| 378 |
processCriticalScheduleTask(); |
| 379 |
} |
| 380 |
} else { |
| 381 |
if (mConfProcessingTasksDistribution) { |
| 382 |
CTask* lastTask = nullptr; |
| 383 |
CTask* taskIt = mTaskProcessingQueue; |
| 384 |
// response least priority task |
| 385 |
while (taskIt && taskIt->hasNext()) { |
| 386 |
lastTask = taskIt; |
| 387 |
taskIt = taskIt->getNext(); |
| 388 |
} |
| 389 |
if (lastTask) { |
| 390 |
responseTask = lastTask->getNext(); |
| 391 |
lastTask->clearNext(); |
| 392 |
--mTaskProcessingCount; |
| 393 |
} |
| 394 |
} else if (mConfProcessingTasksDistribution) { |
| 395 |
CTask* lastBeforeRelevantTask = nullptr; |
| 396 |
|
| 397 |
CTask* lastTask = nullptr; |
| 398 |
CTask* taskIt = mTaskProcessingQueue; |
| 399 |
// response least priority task which is relevant |
| 400 |
while (taskIt && taskIt->hasNext()) { |
| 401 |
lastTask = taskIt; |
| 402 |
CTask* parentTask = taskIt->getParentTask(); |
| 403 |
if (!parentTask || parentTask->isTaskRelevant()) { |
| 404 |
lastBeforeRelevantTask = lastTask; |
| 405 |
} |
| 406 |
taskIt = taskIt->getNext(); |
| 407 |
} |
| 408 |
if (lastBeforeRelevantTask) { |
| 409 |
responseTask = lastBeforeRelevantTask->getNext(); |
| 410 |
lastBeforeRelevantTask->setNext(responseTask->getNext()); |
| 411 |
responseTask->clearNext(); |
| 412 |
--mTaskProcessingCount; |
| 413 |
--mTaskParentRelevantCount; |
| 414 |
} |
| 415 |
} |
| 416 |
if (responseTask) { |
| 417 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksRequestedCount(1)); |
| 418 |
responseTask->clearTaskOwner(); |
| 419 |
CTask* parentTask = responseTask->getParentTask(); |
| 420 |
while (parentTask) { |
| 421 |
if (parentTask->hasTaskOwner()) { |
| 422 |
parentTask->clearTaskOwner(); |
| 423 |
parentTask = parentTask->getParentTask(); |
| 424 |
} else { |
| 425 |
parentTask = nullptr; |
| 426 |
} |
| 427 |
} |
| 428 |
mLastDispendedTask = responseTask; |
| 429 |
responseTask->getTaskStatus()->setTaskSCHEDULINGState(); |
| 430 |
} |
| 431 |
} |
| 432 |
if (responseTask) { |
| 433 |
responseTask->clearNext(); |
| 434 |
} |
| 435 |
processResponseScheduleTask(responseTask,taskScheduleItem,schedulingTask); |
| 436 |
return true; |
| 437 |
} else if (eventID == CUpdateTaskStatusEvent::EVENTTYPEID) { |
| 438 |
CUpdateTaskStatusEvent* utse = (CUpdateTaskStatusEvent*)event; |
| 439 |
processUpdateTaskStatus(utse->getParentTask(),utse->getUpdateTask()); |
| 440 |
return true; |
| 441 |
} else { |
| 442 |
// not supported |
| 443 |
} |
| 444 |
return false; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
void CTaskProcessorThreadBase::organizeDispenseTasks(bool forceUpdate) { |
| 449 |
if (mMinimalPriorityTaskInvalidated || forceUpdate) { |
| 450 |
// count dispense task and find minimal priority |
| 451 |
CTask* taskIt = mTaskProcessingQueue; |
| 452 |
double minPriority = 0.; |
| 453 |
CTask* minPriorityTask = nullptr; |
| 454 |
cint64 dispenseTaskCount = 0; |
| 455 |
while (taskIt) { |
| 456 |
CTask* task = taskIt; |
| 457 |
if (task->isTaskDispenseMarked()) { |
| 458 |
++dispenseTaskCount; |
| 459 |
double priorty = task->getTaskPriority(); |
| 460 |
if (!minPriorityTask || minPriority < priorty) { |
| 461 |
minPriority = priorty; |
| 462 |
minPriorityTask = task; |
| 463 |
} |
| 464 |
} |
| 465 |
taskIt = taskIt->getNext(); |
| 466 |
} |
| 467 |
|
| 468 |
mMinTaskPriority = minPriority; |
| 469 |
mMinPriorityTask = minPriorityTask; |
| 470 |
mMinimalPriorityTaskInvalidated = false; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
CTaskProcessorCommunicator* CTaskProcessorThreadBase::communicateTaskComplete(CTask* task) { |
| 476 |
task->getTaskStatus()->setTaskFINISHEDState(); |
| 477 |
processCompleteTask(task); |
| 478 |
return this; |
| 479 |
} |
| 480 |
|
| 481 |
CTaskProcessorCommunicator* CTaskProcessorThreadBase::communicateTaskError(CTask* task) { |
| 482 |
task->getTaskStatus()->setTaskFINISHEDState(); |
| 483 |
processCompleteTask(task); |
| 484 |
return this; |
| 485 |
} |
| 486 |
|
| 487 |
CTaskProcessorCommunicator* CTaskProcessorThreadBase::communicateTaskCreation(CTask* newTask) { |
| 488 |
CTask* taskIt = newTask; |
| 489 |
while (taskIt) { |
| 490 |
if (!taskIt->hasTaskOwner()) { |
| 491 |
taskIt->setTaskOwner(this); |
| 492 |
} |
| 493 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksCreatedCount(1)); |
| 494 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksCreatedDepthCount(taskIt->getTaskDepth(),1)); |
| 495 |
taskIt = taskIt->getNext(); |
| 496 |
} |
| 497 |
addProcessingTask(newTask); |
| 498 |
return this; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
CTaskProcessorCommunicator* CTaskProcessorThreadBase::communicateTaskStatusUpdate(CTask* task) { |
| 503 |
processUpdateTaskStatus(task); |
| 504 |
return this; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
CTaskProcessorCommunicator* CTaskProcessorThreadBase::communicateTaskRelevant(CTask* task) { |
| 510 |
// TODO: mark task as relevant and prepare for distribution |
| 511 |
if (!task->isTaskRelevant()) { |
| 512 |
if (task->hasNoTaskOwnerAndNoParentTask() || task->hasTaskOwner(this)) { |
| 513 |
task->setTaskRelevant(true); |
| 514 |
CXNegLinker<CTask*>* refTaskLinker = task->getReferencedTaskLinker(); |
| 515 |
while (refTaskLinker) { |
| 516 |
bool refTaskCompleted = !refTaskLinker->isNegated(); |
| 517 |
if (!refTaskCompleted) { |
| 518 |
CTask* refTask = refTaskLinker->getData(); |
| 519 |
if (refTask->getTaskStatus()->isTaskStateQUEUED()) { |
| 520 |
++mTaskParentRelevantCount; |
| 521 |
} |
| 522 |
} |
| 523 |
refTaskLinker = refTaskLinker->getNext(); |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
return this; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
CTaskProcessorCommunicator* CTaskProcessorThreadBase::communicateTaskAdditionalAllocation(CTask* task, CMemoryPool* additionalAllocatedMemoryPool) { |
| 533 |
processAdditionalTaskAllocation(task,additionalAllocatedMemoryPool); |
| 534 |
return this; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
bool CTaskProcessorThreadBase::verifyContinueTaskProcessing(CTask* task) { |
| 539 |
if (mEventSignalized) { |
| 540 |
return false; |
| 541 |
} |
| 542 |
if (!task->getTaskStatus()->isProcessable()) { |
| 543 |
return false; |
| 544 |
} |
| 545 |
if (mTaskProcessingQueue && mTaskProcessingQueue->getTaskPriority() > task->getTaskPriority()) { |
| 546 |
return false; |
| 547 |
} |
| 548 |
return true; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
bool CTaskProcessorThreadBase::handleEvents() { |
| 553 |
bool handleNextRound = true; |
| 554 |
bool roundEventProcessed = false; |
| 555 |
bool eventProcessed = false; |
| 556 |
while (handleNextRound && !mProcessingStopped) { |
| 557 |
roundEventProcessed = false; |
| 558 |
CXLinker<CEventHandler*>* eventHandlerLinkerIt = mEventHandlerLinker; |
| 559 |
if (eventHandlerLinkerIt) { |
| 560 |
|
| 561 |
CEventHandler* eventHandler = eventHandlerLinkerIt->getData(); |
| 562 |
if (eventHandler->needEventProcessing()) { |
| 563 |
cint64 handledEventCount = eventHandler->handleEvents(mTaskProcessorContext); |
| 564 |
INCTASKPROCESSINGSTAT(mStats.incStatisticEventsProcessedCount(handledEventCount)); |
| 565 |
roundEventProcessed = handledEventCount > 0; |
| 566 |
} |
| 567 |
// round robin |
| 568 |
eventHandlerLinkerIt = eventHandlerLinkerIt->getNext(); |
| 569 |
if (!eventHandlerLinkerIt) { |
| 570 |
if (mEventSignalized || roundEventProcessed) { |
| 571 |
handleNextRound = true; |
| 572 |
mEventSignalized = false; |
| 573 |
} else { |
| 574 |
handleNextRound = false; |
| 575 |
} |
| 576 |
eventHandlerLinkerIt = mEventHandlerLinker; |
| 577 |
} |
| 578 |
eventProcessed |= roundEventProcessed; |
| 579 |
} else { |
| 580 |
handleNextRound = false; |
| 581 |
} |
| 582 |
} |
| 583 |
return eventProcessed; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
bool CTaskProcessorThreadBase::processTask(CTask* task) { |
| 589 |
mLastProcessedTask = task; |
| 590 |
task->clearNext(); |
| 591 |
task->getTaskStatus()->setTaskPROCESSINGState(); |
| 592 |
bool handleComp = mTaskHandleAlgo->handleTask(mTaskProcessorContext,task); |
| 593 |
return handleComp; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
}; // end namespace Scheduler |
| 599 |
|
| 600 |
}; // end namespace Konclude |