| 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 "CTaskProcessorThread.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Scheduler { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CTaskProcessorThread::CTaskProcessorThread(CTaskHandleAlgorithm* taskHandleAlgo, CTaskEventHandlerBasedDistributionCompletor* distributorCompletor, CConsiderateMemoryPoolProvider* memoryPoolProvider) : CTaskProcessorThreadBase(taskHandleAlgo,"TaskProcesserSchedulerThread") { |
| 31 |
mDistributorCompletorCommunicator = new CTaskDistributionCompletorCommunicator(distributorCompletor,this); |
| 32 |
mMemoryPoolReleaser = new CEventDistributionMemoryPoolReleaser(mDistributorCompletorCommunicator); |
| 33 |
mMemoryPoolProvider = memoryPoolProvider; |
| 34 |
if (!mMemoryPoolProvider) { |
| 35 |
//mMemoryPoolProvider = distributorCompletor->createDistributionMemoryPoolProvider(mMemoryPoolReleaser); |
| 36 |
mMemoryPoolProvider = new CNewAllocationMemoryPoolProvider(); |
| 37 |
} |
| 38 |
mMemoryAllocator = new CTaskHandleLimitedReserveMemoryPoolAllocationManager(mMemoryPoolProvider,80000,30); |
| 39 |
mTaskProcessorContext = new CTaskProcessorContextBase(this,mMemoryAllocator); |
| 40 |
|
| 41 |
mDispenseNotificationTag = 0; |
| 42 |
|
| 43 |
mSchedulerCommunicator = nullptr; |
| 44 |
} |
| 45 |
|
| 46 |
CTaskProcessorThread::~CTaskProcessorThread() { |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
CTaskEventHandlerBasedProcessor* CTaskProcessorThread::installScheduler(CTaskEventHandlerBasedScheduler* scheduler) { |
| 53 |
mTaskReserveQueueConsumer = scheduler->createTaskReserveQueueConsumer(this); |
| 54 |
CTaskSchedulerCommunicator* schedulerCommunicator = scheduler->createSchedulerProcessorCommunication(this); |
| 55 |
installSchedulerCommunicator(schedulerCommunicator); |
| 56 |
return this; |
| 57 |
} |
| 58 |
|
| 59 |
CTaskProcessorThread* CTaskProcessorThread::installSchedulerCommunicator(CTaskSchedulerCommunicator* schedulerCommunicator) { |
| 60 |
mSchedulerCommunicator = schedulerCommunicator; |
| 61 |
mSchedulerCommunicator->communicateRequestProcessTask(mMemoryAllocator); |
| 62 |
return this; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
bool CTaskProcessorThread::processEvent(CEvent *event, CContext* handlerContext) { |
| 68 |
cint64 eventID = event->getEventTypeID(); |
| 69 |
if (CTaskProcessorThreadBase::processEvent(event,handlerContext)) { |
| 70 |
return true; |
| 71 |
} else { |
| 72 |
// not supported |
| 73 |
} |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
bool CTaskProcessorThread::processDispenseNotification() { |
| 81 |
bool schedulerReqDis = mSchedulerCommunicator->requiresTaskDispenseNotification(mDispenseNotificationTag); |
| 82 |
organizeDispenseTasks(schedulerReqDis); |
| 83 |
if (canDispenseProcessingTasks() && schedulerReqDis) { |
| 84 |
mSchedulerCommunicator->communicateTaskDispenseNotification(mMemoryAllocator); |
| 85 |
} |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
bool CTaskProcessorThread::processRequestProcessTask() { |
| 91 |
mSchedulerCommunicator->communicateRequestProcessTask(mMemoryAllocator); |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
bool CTaskProcessorThread::processCriticalScheduleTask() { |
| 96 |
mSchedulerCommunicator->communicateCriticalScheduleTask(mMemoryAllocator); |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
bool CTaskProcessorThread::processCompleteTask(CTask* task) { |
| 102 |
// try locale to complete task |
| 103 |
cint64 completedCount = 0; |
| 104 |
mTaskCompletionQueue = task->getLastListLink()->setNext(mTaskCompletionQueue); |
| 105 |
while (mTaskCompletionQueue) { |
| 106 |
CTask* completionTask = mTaskCompletionQueue; |
| 107 |
mTaskCompletionQueue = mTaskCompletionQueue->getNext(); |
| 108 |
|
| 109 |
if (completionTask && !completionTask->hasActiveReferencedTask()) { |
| 110 |
CTask* parentTask = completionTask->getParentTask(); |
| 111 |
if (!parentTask || parentTask->hasNoTaskOwnerAndNoParentTask() || parentTask->hasTaskOwner(this)) { |
| 112 |
// locale task completion |
| 113 |
bool upPropagation = false; |
| 114 |
if (mTaskStatusPropagator && mTaskStatusPropagator->completeTaskStatus(completionTask,upPropagation)) { |
| 115 |
if (parentTask && upPropagation) { |
| 116 |
processUpdateTaskStatus(parentTask); |
| 117 |
} |
| 118 |
} |
| 119 |
completionTask->completeTask(); |
| 120 |
completionTask->getTaskStatus()->setTaskCOMPLETEDState(); |
| 121 |
bool memoryReleaseable = completionTask->getTaskStatus()->isMemoryReleaseable(); |
| 122 |
if (parentTask) { |
| 123 |
parentTask->decActiveReferenceCount(); |
| 124 |
if (!parentTask->hasActiveReferencedTask()) { |
| 125 |
mTaskCompletionQueue = parentTask->setNext(mTaskCompletionQueue); |
| 126 |
} |
| 127 |
} |
| 128 |
if (completionTask) { |
| 129 |
// callbacks |
| 130 |
CCallbackData* callbackLinkerIt = completionTask->getCallbackLinker(); |
| 131 |
while (callbackLinkerIt) { |
| 132 |
CCallbackData* callback = callbackLinkerIt; |
| 133 |
callbackLinkerIt = callbackLinkerIt->getNext(); |
| 134 |
if (mCallbackExecuter) { |
| 135 |
mCallbackExecuter->executeCallback(completionTask,callback); |
| 136 |
} else { |
| 137 |
callback->doCallback(); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
#ifndef KONCLUDE_SCHEDULER_DEBUG_TASK_MEMORY_RELEASE |
| 142 |
if (memoryReleaseable) { |
| 143 |
for (CDeletionLinker* deletionLinkerIt = completionTask->takeDeletionLinker(); deletionLinkerIt; deletionLinkerIt = deletionLinkerIt->getNext()) { |
| 144 |
deletionLinkerIt->deleteObject(); |
| 145 |
} |
| 146 |
mMemoryAllocator->releaseMemoryPoolContainer(completionTask); |
| 147 |
} |
| 148 |
#endif |
| 149 |
completedCount++; |
| 150 |
} else { |
| 151 |
// task completion by completer |
| 152 |
mDistributorCompletorCommunicator->communicateTaskComplete(completionTask,mMemoryAllocator); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
INCTASKPROCESSINGSTAT(mStats.incStatisticTasksCompletedCount(completedCount)); |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
bool CTaskProcessorThread::processAdditionalTaskAllocation(CTask* task, CMemoryPool* memoryPool) { |
| 162 |
CTask* parentTask = task->getParentTask(); |
| 163 |
if (!parentTask || parentTask->hasNoTaskOwnerAndNoParentTask() || parentTask->hasTaskOwner(this)) { |
| 164 |
task->appendMemoryPool(memoryPool); |
| 165 |
} else { |
| 166 |
mDistributorCompletorCommunicator->communicateAdditionalAllocationTask(task,memoryPool,mMemoryAllocator); |
| 167 |
} |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
bool CTaskProcessorThread::processResponseScheduleTask(CTask* task, CTaskScheduleItem* taskScheduleItem, bool schedulingTask) { |
| 172 |
mSchedulerCommunicator->communicateResponseScheduleTask(taskScheduleItem,task,schedulingTask,mMemoryAllocator); |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
bool CTaskProcessorThread::processUpdateTaskStatus(CTask* updateTask) { |
| 179 |
CTask* parentTask = updateTask->getParentTask(); |
| 180 |
if (!parentTask || parentTask->hasNoTaskOwnerAndNoParentTask() || parentTask->hasTaskOwner(this)) { |
| 181 |
// update status local |
| 182 |
bool downProp = false; |
| 183 |
bool upProp = false; |
| 184 |
if (mTaskStatusPropagator && mTaskStatusPropagator->updateTaskStatus(updateTask,downProp,upProp)) { |
| 185 |
if (downProp) { |
| 186 |
// update task status local |
| 187 |
CXNegLinker<CTask*>* refTaskIt = updateTask->getReferencedTaskLinker(); |
| 188 |
while (refTaskIt) { |
| 189 |
if (refTaskIt->isNegated()) { |
| 190 |
// task still exists, update task status |
| 191 |
// TODO: check better not recursive? |
| 192 |
CTask* refTask = refTaskIt->getData(); |
| 193 |
processUpdateTaskStatus(refTask); |
| 194 |
} |
| 195 |
refTaskIt = refTaskIt->getNext(); |
| 196 |
} |
| 197 |
} |
| 198 |
if (upProp && parentTask) { |
| 199 |
processUpdateTaskStatus(parentTask); |
| 200 |
} |
| 201 |
} |
| 202 |
} else { |
| 203 |
mDistributorCompletorCommunicator->communicateAlignedUpdateTaskStatus(updateTask,mMemoryAllocator); |
| 204 |
} |
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
bool CTaskProcessorThread::processUpdateTaskStatus(CTask* parentTask, CTask* updateTask) { |
| 210 |
if (parentTask->hasNoTaskOwnerAndNoParentTask() || parentTask->hasTaskOwner(this)) { |
| 211 |
// local task status update |
| 212 |
// check whether task is completed |
| 213 |
bool taskCompleted = false; |
| 214 |
CXNegLinker<CTask*>* refTaskLinkerIt = parentTask->getReferencedTaskLinker(); |
| 215 |
while (refTaskLinkerIt) { |
| 216 |
CTask* refTask = refTaskLinkerIt->getData(); |
| 217 |
if (refTask == updateTask) { |
| 218 |
if (!refTaskLinkerIt->isNegated()) { |
| 219 |
taskCompleted = true; |
| 220 |
} |
| 221 |
break; |
| 222 |
} |
| 223 |
refTaskLinkerIt = refTaskLinkerIt->getNext(); |
| 224 |
} |
| 225 |
if (!taskCompleted) { |
| 226 |
processUpdateTaskStatus(updateTask); |
| 227 |
} |
| 228 |
mDistributorCompletorCommunicator->communicateTaskStatusUpdated(parentTask,updateTask,true,mMemoryAllocator); |
| 229 |
} else { |
| 230 |
mDistributorCompletorCommunicator->communicateTaskStatusUpdated(parentTask,updateTask,false,mMemoryAllocator); |
| 231 |
} |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
}; // end namespace Scheduler |
| 238 |
|
| 239 |
}; // end namespace Konclude |