Merge branch 'mueller/master' into mueller/project/distribDatapool

This commit is contained in:
Robin Müller 2020-08-09 17:13:58 +02:00
commit a181f19c02
2 changed files with 38 additions and 67 deletions

View File

@ -3,57 +3,28 @@
#include <framework/ipc/MutexHelper.h> #include <framework/ipc/MutexHelper.h>
SharedRingBuffer::SharedRingBuffer(object_id_t objectId, const size_t size, 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, SystemObject(objectId), SimpleRingBuffer(size, overwriteOld,
maxExcessBytes), mutexTimeout(mutexTimeout) { maxExcessBytes) {
mutex = MutexFactory::instance()->createMutex(); mutex = MutexFactory::instance()->createMutex();
} }
SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer, SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer,
const size_t size, bool overwriteOld, size_t maxExcessBytes, const size_t size, bool overwriteOld, size_t maxExcessBytes):
dur_millis_t mutexTimeout):
SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld, SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld,
maxExcessBytes), mutexTimeout(mutexTimeout) { maxExcessBytes) {
mutex = MutexFactory::instance()->createMutex(); mutex = MutexFactory::instance()->createMutex();
} }
ReturnValue_t SharedRingBuffer::getFreeElementProtected(uint8_t** writePtr, ReturnValue_t SharedRingBuffer::lockRingBufferMutex(
size_t amount) { MutexIF::TimeoutType timeoutType, dur_millis_t timeout) {
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout); return mutex->lockMutex(timeoutType, timeout);
return SimpleRingBuffer::getFreeElement(writePtr,amount);
} }
ReturnValue_t SharedRingBuffer::writeDataProtected(const uint8_t *data, ReturnValue_t SharedRingBuffer::unlockRingBufferMutex() {
size_t amount) { return mutex->unlockMutex();
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
return SimpleRingBuffer::writeData(data,amount);
} }
ReturnValue_t SharedRingBuffer::readDataProtected(uint8_t *data, size_t amount, MutexIF* SharedRingBuffer::getMutexHandle() const {
bool incrementReadPtr, bool readRemaining, return mutex;
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;
} }

View File

@ -6,6 +6,13 @@
#include <framework/objectmanager/SystemObject.h> #include <framework/objectmanager/SystemObject.h>
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
/**
* @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, class SharedRingBuffer: public SystemObject,
public SimpleRingBuffer { public SimpleRingBuffer {
public: public:
@ -17,8 +24,7 @@ public:
* will be overwritten. * will be overwritten.
*/ */
SharedRingBuffer(object_id_t objectId, const size_t size, SharedRingBuffer(object_id_t objectId, const size_t size,
bool overwriteOld, size_t maxExcessBytes, bool overwriteOld, size_t maxExcessBytes);
dur_millis_t mutexTimeout = 10);
/** /**
* This constructor takes an external buffer with the specified size. * This constructor takes an external buffer with the specified size.
@ -29,37 +35,31 @@ public:
* will be overwritten. * will be overwritten.
*/ */
SharedRingBuffer(object_id_t objectId, uint8_t* buffer, const size_t size, SharedRingBuffer(object_id_t objectId, uint8_t* buffer, const size_t size,
bool overwriteOld, size_t maxExcessBytes, 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 * Unless a read-only constant value is read, all operations on the
* of the ring buffer. * shared ring buffer should be protected by calling this function.
* @param timeoutType
* @param timeout
* @return * @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); * The mutex handle can be accessed directly, for example to perform
* the lock with the #MutexHelper for a RAII compliant lock operation.
/** Performs mutex protected SimpleRingBuffer::writeData call */ * @return
ReturnValue_t writeDataProtected(const uint8_t* data, size_t amount); */
MutexIF* getMutexHandle() const;
/** 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;
private: private:
dur_millis_t mutexTimeout;
MutexIF* mutex = nullptr; MutexIF* mutex = nullptr;
}; };