From 6484c1a27697f1b3a8326dbae0d3fa5eb4be7b4f Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Sat, 8 Aug 2020 23:45:18 +0200 Subject: [PATCH 1/3] shared ring buffer simplified --- container/SharedRingBuffer.cpp | 46 +++++++--------------------------- container/SharedRingBuffer.h | 43 +++++++++++++++---------------- 2 files changed, 29 insertions(+), 60 deletions(-) diff --git a/container/SharedRingBuffer.cpp b/container/SharedRingBuffer.cpp index a8b06b01..afcd89c7 100644 --- a/container/SharedRingBuffer.cpp +++ b/container/SharedRingBuffer.cpp @@ -5,7 +5,7 @@ SharedRingBuffer::SharedRingBuffer(object_id_t objectId, const size_t size, bool overwriteOld, size_t maxExcessBytes, dur_millis_t mutexTimeout): SystemObject(objectId), SimpleRingBuffer(size, overwriteOld, - maxExcessBytes), mutexTimeout(mutexTimeout) { + maxExcessBytes) { mutex = MutexFactory::instance()->createMutex(); } @@ -13,47 +13,19 @@ SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer, const size_t size, bool overwriteOld, size_t maxExcessBytes, dur_millis_t mutexTimeout): SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld, - maxExcessBytes), mutexTimeout(mutexTimeout) { + maxExcessBytes) { mutex = MutexFactory::instance()->createMutex(); } -ReturnValue_t SharedRingBuffer::getFreeElementProtected(uint8_t** writePtr, - size_t amount) { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return SimpleRingBuffer::getFreeElement(writePtr,amount); +ReturnValue_t SharedRingBuffer::lockRingBufferMutex( + MutexIF::TimeoutType timeoutType, dur_millis_t timeout) { + return mutex->lockMutex(timeoutType, timeout); } -ReturnValue_t SharedRingBuffer::writeDataProtected(const uint8_t *data, - size_t amount) { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return SimpleRingBuffer::writeData(data,amount); +ReturnValue_t SharedRingBuffer::unlockRingBufferMutex() { + return mutex->unlockMutex(); } -ReturnValue_t SharedRingBuffer::readDataProtected(uint8_t *data, size_t amount, - bool incrementReadPtr, bool readRemaining, - size_t *trueAmount) { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return SimpleRingBuffer::readData(data,amount, incrementReadPtr, - readRemaining, trueAmount); -} - -ReturnValue_t SharedRingBuffer::deleteDataProtected(size_t amount, - bool deleteRemaining, size_t *trueAmount) { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return SimpleRingBuffer::deleteData(amount, deleteRemaining, trueAmount); -} - -size_t SharedRingBuffer::getExcessBytes() const { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return SimpleRingBuffer::getExcessBytes(); -} - -void SharedRingBuffer::moveExcessBytesToStart() { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return SimpleRingBuffer::moveExcessBytesToStart(); -} - -size_t SharedRingBuffer::getAvailableReadDataProtected(uint8_t n) const { - MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); - return ((write + size) - read[n]) % size; +MutexIF* SharedRingBuffer::getMutexHandle() const { + return mutex; } diff --git a/container/SharedRingBuffer.h b/container/SharedRingBuffer.h index 8d68b967..69a3d724 100644 --- a/container/SharedRingBuffer.h +++ b/container/SharedRingBuffer.h @@ -6,6 +6,13 @@ #include #include +/** + * @brief Ring buffer which can be shared among multiple objects + * @details + * This class offers a mutex to perform thread-safe operation on the ring + * buffer. It is still up to the developer to actually perform the lock + * and unlock operations. + */ class SharedRingBuffer: public SystemObject, public SimpleRingBuffer { public: @@ -32,34 +39,24 @@ public: bool overwriteOld, size_t maxExcessBytes, dur_millis_t mutexTimeout = 10); - void setMutexTimeout(dur_millis_t newTimeout); - - virtual size_t getExcessBytes() const override; /** - * Helper functions which moves any excess bytes to the start - * of the ring buffer. + * Unless a read-only constant value is read, all operations on the + * shared ring buffer should be protected by calling this function. + * @param timeoutType + * @param timeout * @return */ - virtual void moveExcessBytesToStart() override; + virtual ReturnValue_t lockRingBufferMutex(MutexIF::TimeoutType timeoutType, + dur_millis_t timeout); + /** + * Any locked mutex also has to be unlocked, otherwise, access to the + * shared ring buffer will be blocked. + * @return + */ + virtual ReturnValue_t unlockRingBufferMutex(); - /** Performs mutex protected SimpleRingBuffer::getFreeElement call */ - ReturnValue_t getFreeElementProtected(uint8_t** writePtr, size_t amount); - - /** Performs mutex protected SimpleRingBuffer::writeData call */ - ReturnValue_t writeDataProtected(const uint8_t* data, size_t amount); - - /** Performs mutex protected SimpleRingBuffer::readData call */ - ReturnValue_t readDataProtected(uint8_t *data, size_t amount, - bool incrementReadPtr = false, - bool readRemaining = false, size_t *trueAmount = nullptr); - - /** Performs mutex protected SimpleRingBuffer::deleteData call */ - ReturnValue_t deleteDataProtected(size_t amount, - bool deleteRemaining = false, size_t* trueAmount = nullptr); - - size_t getAvailableReadDataProtected (uint8_t n = 0) const; + MutexIF* getMutexHandle() const; private: - dur_millis_t mutexTimeout; MutexIF* mutex = nullptr; }; From f9ce1d9eb9f5fcf85a339fc28b3b5806cb1c644c Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Sat, 8 Aug 2020 23:46:21 +0200 Subject: [PATCH 2/3] more simplifications for shared ring buffer --- container/SharedRingBuffer.cpp | 5 ++--- container/SharedRingBuffer.h | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/container/SharedRingBuffer.cpp b/container/SharedRingBuffer.cpp index afcd89c7..001a21ed 100644 --- a/container/SharedRingBuffer.cpp +++ b/container/SharedRingBuffer.cpp @@ -3,15 +3,14 @@ #include SharedRingBuffer::SharedRingBuffer(object_id_t objectId, const size_t size, - bool overwriteOld, size_t maxExcessBytes, dur_millis_t mutexTimeout): + bool overwriteOld, size_t maxExcessBytes): SystemObject(objectId), SimpleRingBuffer(size, overwriteOld, maxExcessBytes) { mutex = MutexFactory::instance()->createMutex(); } SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer, - const size_t size, bool overwriteOld, size_t maxExcessBytes, - dur_millis_t mutexTimeout): + const size_t size, bool overwriteOld, size_t maxExcessBytes): SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld, maxExcessBytes) { mutex = MutexFactory::instance()->createMutex(); diff --git a/container/SharedRingBuffer.h b/container/SharedRingBuffer.h index 69a3d724..102cc6be 100644 --- a/container/SharedRingBuffer.h +++ b/container/SharedRingBuffer.h @@ -24,8 +24,7 @@ public: * will be overwritten. */ SharedRingBuffer(object_id_t objectId, const size_t size, - bool overwriteOld, size_t maxExcessBytes, - dur_millis_t mutexTimeout = 10); + bool overwriteOld, size_t maxExcessBytes); /** * This constructor takes an external buffer with the specified size. @@ -36,8 +35,7 @@ public: * will be overwritten. */ SharedRingBuffer(object_id_t objectId, uint8_t* buffer, const size_t size, - bool overwriteOld, size_t maxExcessBytes, - dur_millis_t mutexTimeout = 10); + bool overwriteOld, size_t maxExcessBytes); /** * Unless a read-only constant value is read, all operations on the From 5d09ae322176247536667994806ca8aa6bb3b1e5 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Sat, 8 Aug 2020 23:47:44 +0200 Subject: [PATCH 3/3] additional comment --- container/SharedRingBuffer.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/container/SharedRingBuffer.h b/container/SharedRingBuffer.h index 102cc6be..0b5be052 100644 --- a/container/SharedRingBuffer.h +++ b/container/SharedRingBuffer.h @@ -53,6 +53,11 @@ public: */ virtual ReturnValue_t unlockRingBufferMutex(); + /** + * The mutex handle can be accessed directly, for example to perform + * the lock with the #MutexHelper for a RAII compliant lock operation. + * @return + */ MutexIF* getMutexHandle() const; private: MutexIF* mutex = nullptr;