| 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 "CMemoryAccessThread.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CMemoryAccessThread::CMemoryAccessThread(cint64 readWriteRatio, int* accessArray, cint64 arraySize, bool atomicOperations, cint64 minWriteIndex, cint64 maxWriteIndex, CMemoryPoolProvider* memoryPoolProvider) { |
| 31 |
mModificationSteps = 0; |
| 32 |
|
| 33 |
mContextBase = 0; |
| 34 |
mMemoryPoolAllocationMan = 0; |
| 35 |
mCanceled = false; |
| 36 |
|
| 37 |
mMemoryAccessArray = accessArray; |
| 38 |
mReadWriteRatio = readWriteRatio; |
| 39 |
mArraySize = arraySize; |
| 40 |
|
| 41 |
mMemoryPoolProvider = memoryPoolProvider; |
| 42 |
mMemoryPoolAllocationMan = new CMemoryPoolAllocationManager(mMemoryPoolProvider); |
| 43 |
mContextBase = new CContextBase(mMemoryPoolAllocationMan); |
| 44 |
|
| 45 |
mAtomicIncrements = atomicOperations; |
| 46 |
|
| 47 |
mLocalWriteStep = 3; |
| 48 |
mLocalWrite = true; |
| 49 |
|
| 50 |
mMinWriteIndex = minWriteIndex; |
| 51 |
mMaxWriteIndex = maxWriteIndex; |
| 52 |
|
| 53 |
mLocalWriteIndex = mMinWriteIndex; |
| 54 |
} |
| 55 |
|
| 56 |
CMemoryAccessThread::~CMemoryAccessThread() { |
| 57 |
quit(); |
| 58 |
wait(); |
| 59 |
delete mContextBase; |
| 60 |
delete mMemoryPoolAllocationMan; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void CMemoryAccessThread::startModifications() { |
| 65 |
start(); |
| 66 |
moveToThread(this); |
| 67 |
} |
| 68 |
|
| 69 |
void CMemoryAccessThread::stopModifications() { |
| 70 |
mCanceled = true; |
| 71 |
} |
| 72 |
|
| 73 |
cint64 CMemoryAccessThread::getModificationStepsCount() { |
| 74 |
return mModificationSteps; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
void CMemoryAccessThread::run() { |
| 79 |
while (!mCanceled) { |
| 80 |
int newValue = 0; |
| 81 |
for (cint64 i = 0; i < mReadWriteRatio; ++i) { |
| 82 |
qint64 arrayIndex = qrand() % mArraySize; |
| 83 |
int arrayValue = mMemoryAccessArray[arrayIndex]; |
| 84 |
newValue += arrayValue; |
| 85 |
} |
| 86 |
qint64 arrayIndex = 0; |
| 87 |
if (!mLocalWrite) { |
| 88 |
arrayIndex = qrand() % (mArraySize-100); |
| 89 |
} else { |
| 90 |
mLocalWriteIndex += mLocalWriteStep; |
| 91 |
if (mLocalWriteIndex >= mMaxWriteIndex) { |
| 92 |
mLocalWriteIndex = mMinWriteIndex; |
| 93 |
} |
| 94 |
arrayIndex = mLocalWriteIndex; |
| 95 |
} |
| 96 |
if (mAtomicIncrements) { |
| 97 |
QAtomicInt* atomicInt = (QAtomicInt*)&(mMemoryAccessArray[arrayIndex]); |
| 98 |
atomicInt->fetchAndAddOrdered(newValue); |
| 99 |
} else { |
| 100 |
mMemoryAccessArray[arrayIndex] += newValue; |
| 101 |
} |
| 102 |
++mModificationSteps; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
}; // end namespace Test |
| 110 |
|
| 111 |
}; // end namespace Konclude |