| 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 "CReuseCompletionGraphCache.h" |
| 22 |
#include "CReuseCompletionGraphCacheWriter.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Reasoner { |
| 28 |
|
| 29 |
namespace Kernel { |
| 30 |
|
| 31 |
namespace Cache { |
| 32 |
|
| 33 |
|
| 34 |
CReuseCompletionGraphCache::CReuseCompletionGraphCache(QString threadIdentifierName, CWatchDog *watchDogThread) : CThread(threadIdentifierName,watchDogThread) { |
| 35 |
startThread(QThread::HighestPriority); |
| 36 |
|
| 37 |
mSlotLinker = nullptr; |
| 38 |
mReaderLinker = nullptr; |
| 39 |
|
| 40 |
CMemoryAllocationManager* memMan = mContext.getMemoryAllocationManager(); |
| 41 |
mEntyHash = CObjectParameterizingAllocator< CReuseCompletionGraphCompatibilityEntryHash,CContext* >::allocateAndConstructAndParameterize(memMan,&mContext); |
| 42 |
mEntyList = CObjectParameterizingAllocator< CCACHINGLIST<CReuseCompletionGraphCacheEntry*>,CContext* >::allocateAndConstructAndParameterize(memMan,&mContext); |
| 43 |
mEntryCount = 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
CReuseCompletionGraphCache::~CReuseCompletionGraphCache() { |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void CReuseCompletionGraphCache::createReaderSlotUpdate(CReuseCompletionGraphCacheContext* context) { |
| 55 |
CMemoryPoolProvider* memProv = context->getMemoryPoolAllocationManager(); |
| 56 |
CReuseCompletionGraphCacheSlotItem* slot = CObjectMemoryPoolAllocator<CReuseCompletionGraphCacheSlotItem>::allocateAndConstructWithMemroyPool(memProv); |
| 57 |
CMemoryPoolContainerAllocationManager slotMemMan(slot,memProv); |
| 58 |
CMemoryAllocationManager* memMan = &slotMemMan; |
| 59 |
CContextBase tmpContext(memMan); |
| 60 |
|
| 61 |
CReuseCompletionGraphCompatibilityEntryHash* entyHash = CObjectParameterizingAllocator< CReuseCompletionGraphCompatibilityEntryHash,CContext* >::allocateAndConstructAndParameterize(memMan,&tmpContext); |
| 62 |
*entyHash = *mEntyHash; |
| 63 |
entyHash->detach(); |
| 64 |
slot->setEntryHash(entyHash); |
| 65 |
slot->setEntryCount(mEntryCount); |
| 66 |
|
| 67 |
if (mSlotLinker) { |
| 68 |
mSlotLinker->append(slot); |
| 69 |
} else { |
| 70 |
mSlotLinker = slot; |
| 71 |
} |
| 72 |
CReuseCompletionGraphCacheReader* readerLinkerIt = mReaderLinker; |
| 73 |
while (readerLinkerIt) { |
| 74 |
slot->incReader(); |
| 75 |
readerLinkerIt->updateSlot(slot); |
| 76 |
readerLinkerIt = readerLinkerIt->getNext(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
void CReuseCompletionGraphCache::cleanUnusedSlots(CReuseCompletionGraphCacheContext* context) { |
| 82 |
CMemoryPoolAllocationManager* memMan = context->getMemoryPoolAllocationManager(); |
| 83 |
CReuseCompletionGraphCacheSlotItem* slotLinkerIt = mSlotLinker; |
| 84 |
CReuseCompletionGraphCacheSlotItem* lastSlotLinker = nullptr; |
| 85 |
while (slotLinkerIt) { |
| 86 |
CReuseCompletionGraphCacheSlotItem* tmpSlotLinker = nullptr; |
| 87 |
if (!slotLinkerIt->hasCacheReaders()) { |
| 88 |
tmpSlotLinker = slotLinkerIt; |
| 89 |
} |
| 90 |
CReuseCompletionGraphCacheSlotItem* nextSlotLinker = slotLinkerIt->getNext(); |
| 91 |
if (tmpSlotLinker) { |
| 92 |
if (!lastSlotLinker) { |
| 93 |
mSlotLinker = nextSlotLinker; |
| 94 |
} else { |
| 95 |
lastSlotLinker->setNext(nextSlotLinker); |
| 96 |
} |
| 97 |
CMemoryPool* memoryPools = tmpSlotLinker->getMemoryPools(); |
| 98 |
memMan->releaseTemporaryMemoryPools(memoryPools); |
| 99 |
} else { |
| 100 |
lastSlotLinker = slotLinkerIt; |
| 101 |
} |
| 102 |
slotLinkerIt = nextSlotLinker; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
CReuseCompletionGraphCacheReader* CReuseCompletionGraphCache::createCacheReader() { |
| 109 |
CReuseCompletionGraphCacheReader* reader = new CReuseCompletionGraphCacheReader(); |
| 110 |
mReaderSyncMutex.lock(); |
| 111 |
mReaderLinker = reader->append(mReaderLinker); |
| 112 |
mReaderSyncMutex.unlock(); |
| 113 |
return reader; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
CReuseCompletionGraphCacheWriter* CReuseCompletionGraphCache::createCacheWriter() { |
| 118 |
CReuseCompletionGraphCacheWriter* writer = new CReuseCompletionGraphCacheWriter(this); |
| 119 |
return writer; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
CReuseCompletionGraphCache* CReuseCompletionGraphCache::writeExpandCache(CReuseCompletionGraphCacheEntryWriteData* writeData, CMemoryPool* memoryPools) { |
| 124 |
postEvent(new CWriteCachedDataEvent(writeData,memoryPools)); |
| 125 |
return this; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void CReuseCompletionGraphCache::writeExpandCacheData(CReuseCompletionGraphCacheContext* context, CCACHINGLIST<CCacheValue>* cacheList, CCACHINGLIST<CCacheValue>* minimalCacheList, CSatisfiableCalculationJobInstantiation* jobInst) { |
| 130 |
|
| 131 |
CMemoryAllocationManager* memMan = context->getMemoryAllocationManager(); |
| 132 |
CReuseCompletionGraphCacheEntry* newEntry = CObjectParameterizingAllocator< CReuseCompletionGraphCacheEntry,CReuseCompletionGraphCacheContext* >::allocateAndConstructAndParameterize(memMan,context); |
| 133 |
newEntry->setJobInstantiation(jobInst); |
| 134 |
mEntyList->append(newEntry); |
| 135 |
newEntry->setEntryID(mEntryCount); |
| 136 |
|
| 137 |
for (CCACHINGLIST<CCacheValue>::const_iterator it = cacheList->constBegin(), itEnd = cacheList->constEnd(); it != itEnd; ++it) { |
| 138 |
const CCacheValue& cacheValue = *it; |
| 139 |
CReuseCompletionGraphCompatibilityEntryHashData& hashData = (*mEntyHash)[cacheValue]; |
| 140 |
CXLinker<CReuseCompletionGraphCacheEntry*>* linker = CObjectAllocator< CXLinker<CReuseCompletionGraphCacheEntry*> >::allocateAndConstruct(memMan); |
| 141 |
linker->initLinker(newEntry); |
| 142 |
hashData.addEntailedEntyLinker(linker); |
| 143 |
newEntry->addEntailedValue(cacheValue); |
| 144 |
} |
| 145 |
|
| 146 |
for (CCACHINGLIST<CCacheValue>::const_iterator it = minimalCacheList->constBegin(), itEnd = minimalCacheList->constEnd(); it != itEnd; ++it) { |
| 147 |
const CCacheValue& cacheValue = *it; |
| 148 |
newEntry->addMinimalValue(cacheValue); |
| 149 |
} |
| 150 |
|
| 151 |
++mEntryCount; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
bool CReuseCompletionGraphCache::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 157 |
if (CThread::processCustomsEvents(type,event)) { |
| 158 |
return true; |
| 159 |
} if (type == CWriteCachedDataEvent::EVENTTYPE) { |
| 160 |
CWriteCachedDataEvent* wcde = (CWriteCachedDataEvent*)event; |
| 161 |
CMemoryPool* memoryPools = wcde->getMemoryPools(); |
| 162 |
|
| 163 |
CReuseCompletionGraphCacheEntryWriteData* dataLinkerIt = (CReuseCompletionGraphCacheEntryWriteData*)wcde->getWriteData(); |
| 164 |
while (dataLinkerIt) { |
| 165 |
if (dataLinkerIt->getCacheWriteDataType() == CReuseCompletionGraphCacheEntryWriteData::REUSECOMPLETIONGRAPHWRITEEXPANDDATATYPE) { |
| 166 |
CReuseCompletionGraphCacheEntryExpandWriteData* writeExpandData = (CReuseCompletionGraphCacheEntryExpandWriteData*)dataLinkerIt; |
| 167 |
|
| 168 |
CCACHINGLIST<CCacheValue>* cacheList = writeExpandData->getCacheValueList(); |
| 169 |
CCACHINGLIST<CCacheValue>* minimalCacheList = writeExpandData->getCacheValueList(); |
| 170 |
CSatisfiableCalculationJobInstantiation* jobInst = writeExpandData->getJobInstantiation(); |
| 171 |
|
| 172 |
writeExpandCacheData(&mContext,cacheList,minimalCacheList,jobInst); |
| 173 |
} |
| 174 |
dataLinkerIt = (CReuseCompletionGraphCacheEntryWriteData*)dataLinkerIt->getNext(); |
| 175 |
} |
| 176 |
|
| 177 |
createReaderSlotUpdate(&mContext); |
| 178 |
cleanUnusedSlots(&mContext); |
| 179 |
|
| 180 |
mContext.getMemoryPoolAllocationManager()->releaseTemporaryMemoryPools(memoryPools); |
| 181 |
} |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
}; // end namespace Cache |
| 187 |
|
| 188 |
}; // end namespace Kernel |
| 189 |
|
| 190 |
}; // end namespace Reasoner |
| 191 |
|
| 192 |
}; // end namespace Konclude |