From 27548ae652ef817d6646c112389e77b13c0f7b9b Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 30 Mar 2020 23:39:37 +0100 Subject: [PATCH] Alternative approach to postponed rt events Signed-off-by: falkTX --- source/backend/plugin/CarlaPlugin.cpp | 3 ++ source/backend/plugin/CarlaPluginInternal.cpp | 6 ++-- source/backend/plugin/CarlaPluginInternal.hpp | 29 +++++++++++-------- source/utils/LinkedList.hpp | 15 ++++++---- source/utils/RtLinkedList.hpp | 26 +++-------------- 5 files changed, 36 insertions(+), 43 deletions(-) diff --git a/source/backend/plugin/CarlaPlugin.cpp b/source/backend/plugin/CarlaPlugin.cpp index 42b472492..4d2ae534e 100644 --- a/source/backend/plugin/CarlaPlugin.cpp +++ b/source/backend/plugin/CarlaPlugin.cpp @@ -2061,6 +2061,9 @@ void CarlaPlugin::idle() ProtectedData::PostRtEvents::Access rtEvents(pData->postRtEvents); + if (rtEvents.isEmpty()) + return; + for (RtLinkedList::Itenerator it = rtEvents.getDataIterator(); it.valid(); it.next()) { const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback)); diff --git a/source/backend/plugin/CarlaPluginInternal.cpp b/source/backend/plugin/CarlaPluginInternal.cpp index cb9b289b6..044432f11 100644 --- a/source/backend/plugin/CarlaPluginInternal.cpp +++ b/source/backend/plugin/CarlaPluginInternal.cpp @@ -581,9 +581,8 @@ void CarlaPlugin::ProtectedData::Latency::recreateBuffers(const uint32_t newChan CarlaPlugin::ProtectedData::PostRtEvents::PostRtEvents() noexcept : dataPool(128, 128), - dataPoolRT(128, 128), data(dataPool), - dataPendingRT(dataPoolRT), + dataPendingRT(dataPool), dataMutex(), dataPendingMutex() {} @@ -602,7 +601,6 @@ CarlaPlugin::ProtectedData::PostRtEvents::~PostRtEvents() noexcept void CarlaPlugin::ProtectedData::PostRtEvents::appendRT(const PluginPostRtEvent& e) noexcept { CARLA_SAFE_ASSERT_INT2_RETURN(dataPendingMutex.tryLock(), e.type, e.value1,); - dataPendingRT.append(e); dataPendingMutex.unlock(); } @@ -611,7 +609,7 @@ void CarlaPlugin::ProtectedData::PostRtEvents::trySplice() noexcept { const CarlaMutexTryLocker cmtl(dataPendingMutex); - if (cmtl.wasLocked() && dataPendingRT.count() > 0 && dataMutex.tryLock()) + if (cmtl.wasLocked() && dataPendingRT.isNotEmpty() && dataMutex.tryLock()) { dataPendingRT.moveTo(data, true); dataMutex.unlock(); diff --git a/source/backend/plugin/CarlaPluginInternal.hpp b/source/backend/plugin/CarlaPluginInternal.hpp index 7282892c7..fab3215ff 100644 --- a/source/backend/plugin/CarlaPluginInternal.hpp +++ b/source/backend/plugin/CarlaPluginInternal.hpp @@ -310,32 +310,37 @@ struct CarlaPlugin::ProtectedData { struct Access { Access(PostRtEvents& e) - : cml(e.dataMutex), - data(e.data), - it(e.data.begin2()) {} + : data2(e.dataPool) + { + const CarlaMutexLocker cml(e.dataMutex); + + if (e.data.isNotEmpty()) + e.data.moveTo(data2, true); + } ~Access() { - data.clear(); + data2.clear(); } - inline RtLinkedList::Itenerator& getDataIterator() noexcept + inline RtLinkedList::Itenerator getDataIterator() const noexcept { - return it; + return data2.begin2(); + } + + inline std::size_t isEmpty() const noexcept + { + return data2.isEmpty(); } private: - const CarlaMutexLocker cml; - RtLinkedList& data; - RtLinkedList::Itenerator it; + RtLinkedList data2; }; private: - RtLinkedList::Pool dataPool, dataPoolRT; + RtLinkedList::Pool dataPool; RtLinkedList data, dataPendingRT; CarlaMutex dataMutex; - - // TESTING for thread-safety, remove later? CarlaMutex dataPendingMutex; CARLA_DECLARE_NON_COPY_CLASS(PostRtEvents) diff --git a/source/utils/LinkedList.hpp b/source/utils/LinkedList.hpp index 14853539d..fb1a1c191 100644 --- a/source/utils/LinkedList.hpp +++ b/source/utils/LinkedList.hpp @@ -1,6 +1,6 @@ /* * High-level, templated, C++ doubly-linked list - * Copyright (C) 2013-2014 Filipe Coelho + * Copyright (C) 2013-2020 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -224,14 +224,19 @@ public: _init(); } - std::size_t count() const noexcept + inline std::size_t count() const noexcept { return fCount; } - bool isEmpty() const noexcept + inline bool isEmpty() const noexcept { - return (fCount == 0); + return fCount == 0; + } + + inline bool isNotEmpty() const noexcept + { + return fCount != 0; } bool append(const T& value) noexcept @@ -352,7 +357,7 @@ public: } // move data to a new list, and clear ourselves - bool moveTo(AbstractLinkedList& list, const bool inTail = true) noexcept + virtual bool moveTo(AbstractLinkedList& list, const bool inTail = true) noexcept { CARLA_SAFE_ASSERT_RETURN(fCount > 0, false); diff --git a/source/utils/RtLinkedList.hpp b/source/utils/RtLinkedList.hpp index 076b893cd..07cda5903 100644 --- a/source/utils/RtLinkedList.hpp +++ b/source/utils/RtLinkedList.hpp @@ -41,7 +41,8 @@ public: : kDataSize(sizeof(typename AbstractLinkedList::Data)), fHandle(nullptr) { - resize(minPreallocated, maxPreallocated); + rtsafe_memory_pool_create(&fHandle, nullptr, kDataSize, minPreallocated, maxPreallocated); + CARLA_SAFE_ASSERT(fHandle != nullptr); } ~Pool() noexcept @@ -70,18 +71,6 @@ public: rtsafe_memory_pool_deallocate(fHandle, dataPtr); } - void resize(const std::size_t minPreallocated, const std::size_t maxPreallocated) noexcept - { - if (fHandle != nullptr) - { - rtsafe_memory_pool_destroy(fHandle); - fHandle = nullptr; - } - - rtsafe_memory_pool_create(&fHandle, nullptr, kDataSize, minPreallocated, maxPreallocated); - CARLA_SAFE_ASSERT(fHandle != nullptr); - } - bool operator==(const Pool& pool) const noexcept { return (fHandle == pool.fHandle && kDataSize == pool.kDataSize); @@ -180,17 +169,10 @@ public: return _add_sleepy(value, false); } - void resize(const std::size_t minPreallocated, const std::size_t maxPreallocated) noexcept + bool moveTo(AbstractLinkedList& list, const bool inTail) noexcept override { - CARLA_SAFE_ASSERT(this->fCount == 0); - this->clear(); - - fMemPool.resize(minPreallocated, maxPreallocated); - } + CARLA_SAFE_ASSERT_RETURN(((RtLinkedList&)list).fMemPool == fMemPool, false); - bool moveTo(RtLinkedList& list, const bool inTail) noexcept - { - // FIXME add some checks? return AbstractLinkedList::moveTo(list, inTail); }