Use SC RT memory pool for RT cache mirror

nix
Owen Green 5 years ago
parent 5892a60391
commit 08be2a629f

@ -6,6 +6,7 @@
#include "Meta.hpp" #include "Meta.hpp"
#include "RealTimeBase.hpp" #include "RealTimeBase.hpp"
#include "SCBufferAdaptor.hpp" #include "SCBufferAdaptor.hpp"
#include "SCWorldAllocator.hpp"
#include <clients/common/FluidBaseClient.hpp> #include <clients/common/FluidBaseClient.hpp>
#include <data/FluidMeta.hpp> #include <data/FluidMeta.hpp>
#include <SC_PlugIn.hpp> #include <SC_PlugIn.hpp>
@ -47,10 +48,16 @@ class NonRealTime : public SCUnit
public: public:
using Cache = std::unordered_map<index, CacheEntryPointer>; using Cache = std::unordered_map<index, CacheEntryPointer>;
using RTCacheMirror = std::unordered_map<index, WeakCacheEntryPointer>; using RTCacheAllocator =
SCWorldAllocator<std::pair<const index, WeakCacheEntryPointer>>;
using RTCacheMirror =
std::unordered_map<index, WeakCacheEntryPointer, std::hash<index>,
std::equal_to<index>, RTCacheAllocator>;
using RTCachePointer =
std::unique_ptr<RTCacheMirror, std::function<void(RTCacheMirror*)>>;
static Cache mCache; static Cache mCache;
static RTCacheMirror mRTCache; static RTCachePointer mRTCache;
private: private:
static bool isNull(WeakCacheEntryPointer const& weak) static bool isNull(WeakCacheEntryPointer const& weak)
@ -70,8 +77,9 @@ private:
static WeakCacheEntryPointer rtget(index id) static WeakCacheEntryPointer rtget(index id)
{ {
auto lookup = mRTCache.find(id); if (!mRTCache) return {};
return lookup == mRTCache.end() ? WeakCacheEntryPointer() : lookup->second; auto lookup = mRTCache->find(id);
return lookup == mRTCache->end() ? WeakCacheEntryPointer() : lookup->second;
} }
using RawCacheEntry = typename Cache::value_type; using RawCacheEntry = typename Cache::value_type;
@ -82,8 +90,18 @@ private:
{ {
FifoMsg msg; FifoMsg msg;
auto add = [](FifoMsg* m) { auto add = [](FifoMsg* m) {
if (!mRTCache)
{
RTCacheMirror* tmp = rtalloc<RTCacheMirror>(
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<RawCacheEntry*>(m->mData); RawCacheEntry* r = static_cast<RawCacheEntry*>(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"; } if (!res.second) { std::cout << "ERROR: Could not add to RT cache"; }
}; };
msg.Set(w, add, nullptr, &r); msg.Set(w, add, nullptr, &r);
@ -105,8 +123,9 @@ private:
FifoMsg msg; FifoMsg msg;
auto remove = [](FifoMsg* m) { auto remove = [](FifoMsg* m) {
if (!mRTCache) return;
int* id = static_cast<int*>(m->mData); int* id = static_cast<int*>(m->mData);
mRTCache.erase(*id); mRTCache->erase(*id);
}; };
auto cleanup = [](FifoMsg* m) { delete static_cast<index*>(m->mData); }; auto cleanup = [](FifoMsg* m) { delete static_cast<index*>(m->mData); };
@ -366,7 +385,7 @@ private:
size_t completionMsgSize = mCompletionMsgSize; size_t completionMsgSize = mCompletionMsgSize;
char* completionMessage = mCompletionMessage; char* completionMessage = mCompletionMessage;
void* replyAddress = copyReplyAddress(NRTCommand::mReplyAddress); void* replyAddress = copyReplyAddress(NRTCommand::mReplyAddress);
auto callback = [world, id, completionMsgSize, completionMessage, auto callback = [world, id, completionMsgSize, completionMessage,
replyAddress]() { replyAddress]() {
doProcessCallback(world, id, completionMsgSize, completionMessage, doProcessCallback(world, id, completionMsgSize, completionMessage,
replyAddress); replyAddress);
@ -384,9 +403,7 @@ private:
bool error = mResult.status() == Result::Status::kError; bool error = mResult.status() == Result::Status::kError;
if (error) if (error) ptr->mDone.store(true, std::memory_order_relaxed);
ptr->mDone.store(true,
std::memory_order_relaxed);
bool toStage3 = mSynchronous && !error; bool toStage3 = mSynchronous && !error;
return toStage3; return toStage3;
} }
@ -436,7 +453,7 @@ private:
mCompletionMessage = message; mCompletionMessage = message;
} }
// private: // private:
Result mResult; Result mResult;
bool mSynchronous; bool mSynchronous;
size_t mCompletionMsgSize{0}; size_t mCompletionMsgSize{0};
@ -845,6 +862,7 @@ private:
void next(int) void next(int)
{ {
if (!mRTCache) return;
if (isNull(mRecord)) { mRecord = rtget(mID); }; if (isNull(mRecord)) { mRecord = rtget(mID); };
if (0 == mCounter++) if (0 == mCounter++)
@ -950,8 +968,8 @@ private:
bool blocking = mInBuf[mNumInputs - 1][0] > 0; bool blocking = mInBuf[mNumInputs - 1][0] > 0;
CommandProcess* cmd = rtalloc<CommandProcess>(mWorld, mID, blocking, CommandProcess* cmd =
&mParams); rtalloc<CommandProcess>(mWorld, mID, blocking, &mParams);
if (runAsyncCommand(mWorld, cmd, nullptr, 0, nullptr) != 0) if (runAsyncCommand(mWorld, cmd, nullptr, 0, nullptr) != 0)
{ {
std::cout << "ERROR: Async command failed in NRTTriggerUnit::next()" std::cout << "ERROR: Async command failed in NRTTriggerUnit::next()"
@ -1016,6 +1034,7 @@ private:
void init() void init()
{ {
mInit = false; mInit = false;
if (!mRTCache) return;
mInst = rtget(mID); mInst = rtget(mID);
if (auto ptr = mInst.lock()) if (auto ptr = mInst.lock())
{ {
@ -1027,8 +1046,8 @@ private:
void next(int) void next(int)
{ {
Wrapper::getInterfaceTable()->fClearUnitOutputs(this, Wrapper::getInterfaceTable()->fClearUnitOutputs(
asSigned(mNumOutputs)); this, static_cast<int>(mNumOutputs));
index id = static_cast<index>(in0(1)); index id = static_cast<index>(in0(1));
if (mID != id) init(); if (mID != id) init();
if (!mInit) return; if (!mInit) return;
@ -1070,10 +1089,7 @@ private:
template <typename CommandType> template <typename CommandType>
struct DefineCommandIf<true, CommandType> struct DefineCommandIf<true, CommandType>
{ {
void operator()() void operator()() { defineNRTCommand<CommandType>(); }
{
defineNRTCommand<CommandType>();
}
}; };
template <bool, typename UnitType> template <bool, typename UnitType>
@ -1170,8 +1186,8 @@ typename NonRealTime<Client, Wrapper>::Cache
NonRealTime<Client, Wrapper>::mCache{}; NonRealTime<Client, Wrapper>::mCache{};
template <typename Client, typename Wrapper> template <typename Client, typename Wrapper>
typename NonRealTime<Client, Wrapper>::RTCacheMirror typename NonRealTime<Client, Wrapper>::RTCachePointer
NonRealTime<Client, Wrapper>::mRTCache; NonRealTime<Client, Wrapper>::mRTCache{};
} // namespace impl } // namespace impl
} // namespace client } // namespace client

Loading…
Cancel
Save