storage accessor mutex lock removed

This commit is contained in:
Robin Müller 2020-05-12 14:11:00 +02:00
parent e5c46c5ec1
commit 1946af64af
2 changed files with 282 additions and 328 deletions

View File

@ -1,154 +1,130 @@
//#include <framework/storagemanager/StorageAccessor.h> #include <framework/storagemanager/StorageAccessor.h>
//
//ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId): storeId(storeId) {} ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId):
// storeId(storeId) {}
//ConstStorageAccessor::~ConstStorageAccessor() {
// if(deleteData and store != nullptr) { ConstStorageAccessor::~ConstStorageAccessor() {
// sif::debug << "deleting store data" << std::endl; if(deleteData and store != nullptr) {
// store->deleteDataNonLocking(storeId); sif::debug << "deleting store data" << std::endl;
// } store->deleteData(storeId);
// if(mutexLock != nullptr) { }
// sif::debug << "unlocking mutex lock" << std::endl; }
// mutexLock.reset();
// } ConstStorageAccessor& ConstStorageAccessor::operator=(
//} ConstStorageAccessor&& other) {
// constDataPointer = other.constDataPointer;
//ConstStorageAccessor& ConstStorageAccessor::operator=( storeId = other.storeId;
// ConstStorageAccessor&& other) { store = other.store;
// constDataPointer = other.constDataPointer; size_ = other.size_;
// storeId = other.storeId; deleteData = other.deleteData;
// store = other.store; this->store = other.store;
// size_ = other.size_; // This prevents premature deletion
// deleteData = other.deleteData; other.store = nullptr;
// this->store = other.store; return *this;
// // Transfer ownership of the lock. }
// mutexLock = std::move(other.mutexLock);
// // This prevents double deletion of the resource StorageAccessor::StorageAccessor(store_address_t storeId):
// other.mutexLock = nullptr; ConstStorageAccessor(storeId) {
// // This prevent premature deletion }
// other.store = nullptr;
// return *this; StorageAccessor& StorageAccessor::operator =(
//} StorageAccessor&& other) {
// // Call the parent move assignment and also assign own member.
//StorageAccessor::StorageAccessor(store_address_t storeId): dataPointer = other.dataPointer;
// ConstStorageAccessor(storeId) { StorageAccessor::operator=(std::move(other));
//} return * this;
// }
//StorageAccessor& StorageAccessor::operator =(
// StorageAccessor&& other) { // Call the parent move ctor and also transfer own member.
// // Call the parent move assignment and also assign own member. StorageAccessor::StorageAccessor(StorageAccessor&& other):
// dataPointer = other.dataPointer; ConstStorageAccessor(std::move(other)), dataPointer(other.dataPointer) {
// StorageAccessor::operator=(std::move(other)); }
// return * this;
//} ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other):
// constDataPointer(other.constDataPointer), storeId(other.storeId),
//// Call the parent move ctor and also transfer own member. size_(other.size_), store(other.store), deleteData(other.deleteData),
//StorageAccessor::StorageAccessor(StorageAccessor&& other): internalState(other.internalState) {
// ConstStorageAccessor(std::move(other)), dataPointer(other.dataPointer) { // This prevent premature deletion
//} other.store = nullptr;
// }
//ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other):
// constDataPointer(other.constDataPointer), storeId(other.storeId), const uint8_t* ConstStorageAccessor::data() const {
// size_(other.size_), store(other.store), deleteData(other.deleteData), return constDataPointer;
// internalState(other.internalState) { }
// // Transfer ownership of the lock.
// mutexLock = std::move(other.mutexLock); size_t ConstStorageAccessor::size() const {
// // This prevents double deletion of the resource. Not strictly necessary, if(internalState == AccessState::UNINIT) {
// // from the testing I have conducted so far but I am not familiar enough sif::warning << "StorageAccessor: Not initialized!" << std::endl;
// // with move semantics so I will just set the other lock to nullptr for now. }
// other.mutexLock = nullptr; return size_;
// // This prevent premature deletion }
// other.store = nullptr;
//} ReturnValue_t ConstStorageAccessor::getDataCopy(uint8_t *pointer,
// size_t maxSize) {
//const uint8_t* ConstStorageAccessor::data() const { if(internalState == AccessState::UNINIT) {
// return constDataPointer; sif::warning << "StorageAccessor: Not initialized!" << std::endl;
//} return HasReturnvaluesIF::RETURN_FAILED;
// }
//size_t ConstStorageAccessor::size() const { if(size_ > maxSize) {
// if(internalState == AccessState::UNINIT) { sif::error << "StorageAccessor: Supplied buffer not large enough" << std::endl;
// sif::warning << "StorageAccessor: Not initialized!" << std::endl; return HasReturnvaluesIF::RETURN_FAILED;
// } }
// return size_; std::copy(constDataPointer, constDataPointer + size_, pointer);
//} return HasReturnvaluesIF::RETURN_OK;
// }
//void ConstStorageAccessor::getDataCopy(uint8_t *pointer) {
// if(internalState == AccessState::UNINIT) { void ConstStorageAccessor::release() {
// sif::warning << "StorageAccessor: Not initialized!" << std::endl; deleteData = false;
// return; }
// }
// std::copy(constDataPointer, constDataPointer + size_, pointer); store_address_t ConstStorageAccessor::getId() const {
//} return storeId;
// }
//void ConstStorageAccessor::release() {
// deleteData = false; void ConstStorageAccessor::print() const {
//} if(internalState == AccessState::UNINIT) {
// sif::warning << "StorageAccessor: Not initialized!" << std::endl;
//ReturnValue_t ConstStorageAccessor::lock(MutexIF* mutex, uint32_t mutexTimeout) { return;
// if(mutexLock == nullptr) { }
// mutexLock = std::unique_ptr<MutexHelper>(new MutexHelper(mutex, mutexTimeout)); sif::info << "StorageAccessor: Printing data: [";
// return mutexLock.get()->getResult(); for(uint16_t iPool = 0; iPool < size_; iPool++) {
// } sif::info << std::hex << (int)constDataPointer[iPool];
// else { if(iPool < size_ - 1){
// sif::warning << "StorageAccessor: Attempted to lock twice. Check code!" << std::endl; sif::info << " , ";
// return HasReturnvaluesIF::RETURN_FAILED; }
// } }
//} sif::info << " ] " << std::endl;
// }
//void ConstStorageAccessor::unlock() {
// if(mutexLock != nullptr) { void ConstStorageAccessor::assignStore(StorageManagerIF* store) {
// mutexLock.reset(); internalState = AccessState::READ;
// } this->store = store;
//} }
//
//store_address_t ConstStorageAccessor::getId() const {
// return storeId; uint8_t* StorageAccessor::data() {
//} if(internalState == AccessState::UNINIT) {
// sif::warning << "StorageAccessor: Not initialized!" << std::endl;
//void ConstStorageAccessor::print() const { }
// if(internalState == AccessState::UNINIT) { return dataPointer;
// sif::warning << "StorageAccessor: Not initialized!" << std::endl; }
// return;
// } ReturnValue_t StorageAccessor::write(uint8_t *data, size_t size,
// sif::info << "StorageAccessor: Printing data: ["; uint16_t offset) {
// for(uint16_t iPool = 0; iPool < size_; iPool++) { if(internalState == AccessState::UNINIT) {
// sif::info << std::hex << (int)constDataPointer[iPool]; sif::warning << "StorageAccessor: Not initialized!" << std::endl;
// if(iPool < size_ - 1){ return HasReturnvaluesIF::RETURN_FAILED;
// sif::info << " , "; }
// } if(offset + size > size_) {
// } sif::error << "StorageAccessor: Data too large for pool entry!" << std::endl;
// sif::info << " ] " << std::endl; return HasReturnvaluesIF::RETURN_FAILED;
//} }
// std::copy(data, data + size, dataPointer);
//void ConstStorageAccessor::assignStore(StorageManagerIF* store) { return HasReturnvaluesIF::RETURN_OK;
// internalState = AccessState::READ; }
// this->store = store;
//} void StorageAccessor::assignConstPointer() {
// constDataPointer = dataPointer;
// }
//uint8_t* StorageAccessor::data() {
// if(internalState == AccessState::UNINIT) {
// sif::warning << "StorageAccessor: Not initialized!" << std::endl;
// }
// return dataPointer;
//}
//
//ReturnValue_t StorageAccessor::write(uint8_t *data, size_t size,
// uint16_t offset) {
// if(internalState == AccessState::UNINIT) {
// sif::warning << "StorageAccessor: Not initialized!" << std::endl;
// return HasReturnvaluesIF::RETURN_FAILED;
// }
// if(offset + size > size_) {
// sif::error << "StorageAccessor: Data too large for pool entry!" << std::endl;
// return HasReturnvaluesIF::RETURN_FAILED;
// }
// std::copy(data, data + size, dataPointer);
// return HasReturnvaluesIF::RETURN_OK;
//}
//
//void StorageAccessor::assignConstPointer() {
// constDataPointer = dataPointer;
//}
//
//

View File

@ -1,174 +1,152 @@
///** /**
// * @brief Helper classes to facilitate safe access to storages which is also * @brief Helper classes to facilitate safe access to storages which is also
// * conforming to RAII principles * conforming to RAII principles
// * @details These helper can be used together with the * @details These helper can be used together with the
// * StorageManager classes to manage access to a storage. * StorageManager classes to manage access to a storage.
// * It can take care of thread-safety while also providing * It can take care of thread-safety while also providing
// * mechanisms to automatically clear storage data and unlocking the * mechanisms to automatically clear storage data and unlocking the
// * pool. * pool.
// */ */
//#ifndef TEST_PROTOTYPES_STORAGEACCESSOR_H_ #ifndef TEST_PROTOTYPES_STORAGEACCESSOR_H_
//#define TEST_PROTOTYPES_STORAGEACCESSOR_H_ #define TEST_PROTOTYPES_STORAGEACCESSOR_H_
//
//#include <framework/ipc/MutexHelper.h> #include <framework/ipc/MutexHelper.h>
//#include <framework/storagemanager/StorageManagerIF.h> #include <framework/storagemanager/StorageManagerIF.h>
//#include <memory> #include <memory>
//
//
///** /**
// * @brief Accessor class which can be returned by pool managers * @brief Accessor class which can be returned by pool managers
// * or passed and set by pool managers to have safe access to the pool * or passed and set by pool managers to have safe access to the pool
// * resources. * resources.
// */ */
//class ConstStorageAccessor { class ConstStorageAccessor {
// //! StorageManager classes have exclusive access to private variables. //! StorageManager classes have exclusive access to private variables.
// template<uint8_t NUMBER_OF_POOLS> template<uint8_t NUMBER_OF_POOLS>
// friend class PoolManager; friend class PoolManager;
// template<uint8_t NUMBER_OF_POOLS> template<uint8_t NUMBER_OF_POOLS>
// friend class LocalPool; friend class LocalPool;
//public: public:
// /** /**
// * @brief Simple constructor which takes the store ID of the storage * @brief Simple constructor which takes the store ID of the storage
// * entry to access. * entry to access.
// * @param storeId * @param storeId
// */ */
// ConstStorageAccessor(store_address_t storeId); ConstStorageAccessor(store_address_t storeId);
//
// /** /**
// * @brief Move ctor and move assignment allow returning accessors as * @brief Move ctor and move assignment allow returning accessors as
// * a returnvalue. They prevent resource being free prematurely. * a returnvalue. They prevent resource being free prematurely.
// * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/ * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/
// * move-constructors-and-move-assignment-operators-cpp.md * move-constructors-and-move-assignment-operators-cpp.md
// * @param * @param
// * @return * @return
// */ */
// ConstStorageAccessor& operator= (ConstStorageAccessor&&); ConstStorageAccessor& operator= (ConstStorageAccessor&&);
// ConstStorageAccessor (ConstStorageAccessor&&); ConstStorageAccessor (ConstStorageAccessor&&);
//
// //! The copy ctor and copy assignemnt should be deleted implicitely //! The copy ctor and copy assignemnt should be deleted implicitely
// //! according to https://foonathan.net/2019/02/special-member-functions/ //! according to https://foonathan.net/2019/02/special-member-functions/
// //! but I still deleted them to make it more explicit. (remember rule of 5). //! but I still deleted them to make it more explicit. (remember rule of 5).
// ConstStorageAccessor& operator= (ConstStorageAccessor&) = delete; ConstStorageAccessor& operator= (ConstStorageAccessor&) = delete;
// ConstStorageAccessor (ConstStorageAccessor&) = delete; ConstStorageAccessor (ConstStorageAccessor&) = delete;
//
// /** /**
// * @brief The destructor in default configuration takes care of * @brief The destructor in default configuration takes care of
// * deleting the accessed pool entry and unlocking the mutex * deleting the accessed pool entry and unlocking the mutex
// */ */
// virtual ~ConstStorageAccessor(); virtual ~ConstStorageAccessor();
//
// /** /**
// * @brief Returns a pointer to the read-only data * @brief Returns a pointer to the read-only data
// * @return * @return
// */ */
// const uint8_t* data() const; const uint8_t* data() const;
//
// /** /**
// * @brief Copies the read-only data to the supplied pointer * @brief Copies the read-only data to the supplied pointer
// * @param pointer * @param pointer
// */ */
// void getDataCopy(uint8_t *pointer); ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize);
//
// /** /**
// * @brief Calling this will prevent the Accessor from deleting the data * @brief Calling this will prevent the Accessor from deleting the data
// * when the destructor is called. * when the destructor is called.
// */ */
// void release(); void release();
// /**
// * @brief Locks the supplied mutex. /**
// * @details * Get the size of the data
// * The mutex will be unlocked automatically * @return
// * when this class is destroyed (for example when exiting the scope). */
// * Only one mutex can be locked at a time! size_t size() const;
// * @param mutex
// * @param mutexTimeout /**
// * @return * Get the storage ID.
// */ * @return
// ReturnValue_t lock(MutexIF* mutex, */
// uint32_t mutexTimeout = MutexIF::NO_TIMEOUT); store_address_t getId() const;
// /**
// * @brief Unlocks the mutex (if one has been locked previously). void print() const;
// * Unless this function is called, the mutex is unlocked protected:
// * when the class exits the scope. const uint8_t* constDataPointer = nullptr;
// */ store_address_t storeId;
// void unlock(); size_t size_ = 0;
// //! Managing pool, has to assign itself.
// StorageManagerIF* store = nullptr;
// /** bool deleteData = true;
// * Get the size of the data
// * @return enum class AccessState {
// */ UNINIT,
// size_t size() const; READ
// };
// /** //! Internal state for safety reasons.
// * Get the storage ID. AccessState internalState = AccessState::UNINIT;
// * @return /**
// */ * Used by the pool manager instances to assign themselves to the
// store_address_t getId() const; * accessor. This is necessary to delete the data when the acessor
// * exits the scope ! The internal state will be considered read
// void print() const; * when this function is called, so take care all data is set properly as
//protected: * well.
// const uint8_t* constDataPointer = nullptr; * @param
// store_address_t storeId; */
// size_t size_ = 0; void assignStore(StorageManagerIF*);
// //! Managing pool, has to assign itself.
// StorageManagerIF* store = nullptr; };
// //! Unique pointer to the mutex lock instance. Is initialized by
// //! the pool manager.
// std::unique_ptr<MutexHelper> mutexLock = nullptr; /**
// bool deleteData = true; * @brief Child class for modifyable data. Also has a normal pointer member.
// */
// enum class AccessState { class StorageAccessor: public ConstStorageAccessor {
// UNINIT, //! StorageManager classes have exclusive access to private variables.
// READ template<uint8_t NUMBER_OF_POOLS>
// }; friend class PoolManager;
// //! Internal state for safety reasons. template<uint8_t NUMBER_OF_POOLS>
// AccessState internalState = AccessState::UNINIT; friend class LocalPool;
// /** public:
// * Used by the pool manager instances to assign themselves to the StorageAccessor(store_address_t storeId);
// * accessor. This is necessary to delete the data when the acessor /**
// * exits the scope ! The internal state will be considered read * @brief Move ctor and move assignment allow returning accessors as
// * when this function is called, so take care all data is set properly as * a returnvalue. They prevent resource being free prematurely.
// * well. * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/
// * @param * move-constructors-and-move-assignment-operators-cpp.md
// */ * @param
// void assignStore(StorageManagerIF*); * @return
// */
//}; StorageAccessor& operator= (StorageAccessor&&);
// StorageAccessor (StorageAccessor&&);
//
///** ReturnValue_t write(uint8_t *data, size_t size,
// * @brief Child class for modifyable data. Also has a normal pointer member. uint16_t offset);
// */ uint8_t* data();
//class StorageAccessor: public ConstStorageAccessor {
// //! StorageManager classes have exclusive access to private variables. private:
// template<uint8_t NUMBER_OF_POOLS> //! Non-const pointer for modifyable data.
// friend class PoolManager; uint8_t* dataPointer = nullptr;
// template<uint8_t NUMBER_OF_POOLS> //! For modifyable data, the const pointer is assigned to the normal
// friend class LocalPool; //! pointer by the pool manager so both access functions can be used safely
//public: void assignConstPointer();
// StorageAccessor(store_address_t storeId); };
// /**
// * @brief Move ctor and move assignment allow returning accessors as #endif /* TEST_PROTOTYPES_STORAGEACCESSOR_H_ */
// * a returnvalue. They prevent resource being free prematurely.
// * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/
// * move-constructors-and-move-assignment-operators-cpp.md
// * @param
// * @return
// */
// StorageAccessor& operator= (StorageAccessor&&);
// StorageAccessor (StorageAccessor&&);
//
// ReturnValue_t write(uint8_t *data, size_t size,
// uint16_t offset);
// uint8_t* data();
//
//private:
// //! Non-const pointer for modifyable data.
// uint8_t* dataPointer = nullptr;
// //! For modifyable data, the const pointer is assigned to the normal
// //! pointer by the pool manager so both access functions can be used safely
// void assignConstPointer();
//};
//
//#endif /* TEST_PROTOTYPES_STORAGEACCESSOR_H_ */