From 08be2a629f4779cf3770f9ce782fc68376605cfe Mon Sep 17 00:00:00 2001 From: Owen Green Date: Tue, 22 Jun 2021 18:20:33 +0100 Subject: [PATCH] Use SC RT memory pool for RT cache mirror --- include/wrapper/NonRealtime.hpp | 62 +++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/include/wrapper/NonRealtime.hpp b/include/wrapper/NonRealtime.hpp index 7b002b9..7a912f3 100644 --- a/include/wrapper/NonRealtime.hpp +++ b/include/wrapper/NonRealtime.hpp @@ -6,6 +6,7 @@ #include "Meta.hpp" #include "RealTimeBase.hpp" #include "SCBufferAdaptor.hpp" +#include "SCWorldAllocator.hpp" #include #include #include @@ -47,10 +48,16 @@ class NonRealTime : public SCUnit public: using Cache = std::unordered_map; - using RTCacheMirror = std::unordered_map; + using RTCacheAllocator = + SCWorldAllocator>; + using RTCacheMirror = + std::unordered_map, + std::equal_to, RTCacheAllocator>; + using RTCachePointer = + std::unique_ptr>; - static Cache mCache; - static RTCacheMirror mRTCache; + static Cache mCache; + static RTCachePointer mRTCache; private: static bool isNull(WeakCacheEntryPointer const& weak) @@ -70,8 +77,9 @@ private: static WeakCacheEntryPointer rtget(index id) { - auto lookup = mRTCache.find(id); - return lookup == mRTCache.end() ? WeakCacheEntryPointer() : lookup->second; + if (!mRTCache) return {}; + auto lookup = mRTCache->find(id); + return lookup == mRTCache->end() ? WeakCacheEntryPointer() : lookup->second; } using RawCacheEntry = typename Cache::value_type; @@ -82,8 +90,18 @@ private: { FifoMsg msg; auto add = [](FifoMsg* m) { + if (!mRTCache) + { + RTCacheMirror* tmp = rtalloc( + m->mWorld, RTCacheAllocator{m->mWorld, getInterfaceTable()}); + World* w = m->mWorld; + mRTCache = RTCachePointer(tmp, [w](RTCacheMirror* x) { + if (w->mRunning) getInterfaceTable()->fRTFree(w, x); + }); + } + RawCacheEntry* r = static_cast(m->mData); - auto res = mRTCache.emplace(r->first, r->second); + auto res = mRTCache->emplace(r->first, r->second); if (!res.second) { std::cout << "ERROR: Could not add to RT cache"; } }; msg.Set(w, add, nullptr, &r); @@ -105,8 +123,9 @@ private: FifoMsg msg; auto remove = [](FifoMsg* m) { + if (!mRTCache) return; int* id = static_cast(m->mData); - mRTCache.erase(*id); + mRTCache->erase(*id); }; auto cleanup = [](FifoMsg* m) { delete static_cast(m->mData); }; @@ -366,7 +385,7 @@ private: size_t completionMsgSize = mCompletionMsgSize; char* completionMessage = mCompletionMessage; void* replyAddress = copyReplyAddress(NRTCommand::mReplyAddress); - auto callback = [world, id, completionMsgSize, completionMessage, + auto callback = [world, id, completionMsgSize, completionMessage, replyAddress]() { doProcessCallback(world, id, completionMsgSize, completionMessage, replyAddress); @@ -384,9 +403,7 @@ private: bool error = mResult.status() == Result::Status::kError; - if (error) - ptr->mDone.store(true, - std::memory_order_relaxed); + if (error) ptr->mDone.store(true, std::memory_order_relaxed); bool toStage3 = mSynchronous && !error; return toStage3; } @@ -436,7 +453,7 @@ private: mCompletionMessage = message; } - // private: + // private: Result mResult; bool mSynchronous; size_t mCompletionMsgSize{0}; @@ -845,6 +862,7 @@ private: void next(int) { + if (!mRTCache) return; if (isNull(mRecord)) { mRecord = rtget(mID); }; if (0 == mCounter++) @@ -950,8 +968,8 @@ private: bool blocking = mInBuf[mNumInputs - 1][0] > 0; - CommandProcess* cmd = rtalloc(mWorld, mID, blocking, - &mParams); + CommandProcess* cmd = + rtalloc(mWorld, mID, blocking, &mParams); if (runAsyncCommand(mWorld, cmd, nullptr, 0, nullptr) != 0) { std::cout << "ERROR: Async command failed in NRTTriggerUnit::next()" @@ -1015,7 +1033,8 @@ private: void init() { - mInit = false; + mInit = false; + if (!mRTCache) return; mInst = rtget(mID); if (auto ptr = mInst.lock()) { @@ -1027,8 +1046,8 @@ private: void next(int) { - Wrapper::getInterfaceTable()->fClearUnitOutputs(this, - asSigned(mNumOutputs)); + Wrapper::getInterfaceTable()->fClearUnitOutputs( + this, static_cast(mNumOutputs)); index id = static_cast(in0(1)); if (mID != id) init(); if (!mInit) return; @@ -1070,10 +1089,7 @@ private: template struct DefineCommandIf { - void operator()() - { - defineNRTCommand(); - } + void operator()() { defineNRTCommand(); } }; template @@ -1170,8 +1186,8 @@ typename NonRealTime::Cache NonRealTime::mCache{}; template -typename NonRealTime::RTCacheMirror - NonRealTime::mRTCache; +typename NonRealTime::RTCachePointer + NonRealTime::mRTCache{}; } // namespace impl } // namespace client