From 75c28ba80357885deec4ad62d6d3101188c92c2d Mon Sep 17 00:00:00 2001 From: Nav Date: Thu, 14 Apr 2022 22:58:00 +0100 Subject: [PATCH] Tidied SyncSafe template class --- src/EventManager/EventListener.cpp | 3 ++- src/EventManager/EventListener.hpp | 22 ++++++++++----------- src/Helpers/SyncSafe.hpp | 31 +++++++++--------------------- src/Helpers/Thread.hpp | 3 ++- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/EventManager/EventListener.cpp b/src/EventManager/EventListener.cpp index 7b9e850b..78650306 100644 --- a/src/EventManager/EventListener.cpp +++ b/src/EventManager/EventListener.cpp @@ -7,7 +7,8 @@ namespace Bloom using namespace Bloom::Events; std::set EventListener::getRegisteredEventTypes() { - return this->registeredEventTypes.getValue(); + auto lock = this->registeredEventTypes.acquireLock(); + return this->registeredEventTypes.getReference(); } void EventListener::registerEvent(SharedGenericEventPointer event) { diff --git a/src/EventManager/EventListener.hpp b/src/EventManager/EventListener.hpp index 6de56fbb..94b1adb1 100644 --- a/src/EventManager/EventListener.hpp +++ b/src/EventManager/EventListener.hpp @@ -57,11 +57,11 @@ namespace Bloom template bool isEventTypeRegistered() { - return this->registeredEventTypes.getReference().contains(EventType::type); + return this->registeredEventTypes.getValue().contains(EventType::type); } bool isEventTypeRegistered(Events::EventType eventType) { - return this->registeredEventTypes.getReference().contains(eventType); + return this->registeredEventTypes.getValue().contains(eventType); }; /** @@ -76,13 +76,13 @@ namespace Bloom template void registerEventType() { auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); - this->registeredEventTypes.getReference().insert(EventType::type); + this->registeredEventTypes.getValue().insert(EventType::type); } template void deRegisterEventType() { auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); - this->registeredEventTypes.getReference().erase(EventType::type); + this->registeredEventTypes.getValue().erase(EventType::type); } /** @@ -118,7 +118,7 @@ namespace Bloom ; auto mappingLock = this->eventTypeToCallbacksMapping.acquireLock(); - auto& mapping = this->eventTypeToCallbacksMapping.getReference(); + auto& mapping = this->eventTypeToCallbacksMapping.getValue(); mapping[EventType::type].push_back(parentCallback); this->template registerEventType(); @@ -138,7 +138,7 @@ namespace Bloom { auto mappingLock = this->eventTypeToCallbacksMapping.acquireLock(); - auto& mapping = this->eventTypeToCallbacksMapping.getReference(); + auto& mapping = this->eventTypeToCallbacksMapping.getValue(); if (mapping.contains(EventType::type)) { mapping.at(EventType::type).clear(); @@ -147,11 +147,11 @@ namespace Bloom { auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); - this->registeredEventTypes.getReference().erase(EventType::type); + this->registeredEventTypes.getValue().erase(EventType::type); } auto queueLock = this->eventQueueByEventType.acquireLock(); - auto& eventQueueByType = this->eventQueueByEventType.getReference(); + auto& eventQueueByType = this->eventQueueByEventType.getValue(); if (eventQueueByType.contains(EventType::type)) { eventQueueByType.erase(EventType::type); @@ -212,7 +212,7 @@ namespace Bloom ReturnType output = std::nullopt; auto queueLock = this->eventQueueByEventType.acquireLock(); - auto& eventQueueByType = this->eventQueueByEventType.getReference(); + auto& eventQueueByType = this->eventQueueByEventType.getValue(); auto eventTypes = std::set({EventTypeA::type}); auto eventTypesToDeRegister = std::set(); @@ -235,7 +235,7 @@ namespace Bloom { auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); - auto& registeredEventTypes = this->registeredEventTypes.getReference(); + auto& registeredEventTypes = this->registeredEventTypes.getValue(); for (const auto& eventType : eventTypes) { if (!registeredEventTypes.contains(eventType)) { @@ -281,7 +281,7 @@ namespace Bloom if (!eventTypesToDeRegister.empty()) { auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); - auto& registeredEventTypes = this->registeredEventTypes.getReference(); + auto& registeredEventTypes = this->registeredEventTypes.getValue(); for (const auto& eventType : eventTypesToDeRegister) { registeredEventTypes.erase(eventType); diff --git a/src/Helpers/SyncSafe.hpp b/src/Helpers/SyncSafe.hpp index 4885df0f..dcf82419 100644 --- a/src/Helpers/SyncSafe.hpp +++ b/src/Helpers/SyncSafe.hpp @@ -10,8 +10,6 @@ namespace Bloom * Just a convenient template that allows us to create thread safe types without having to write * the bloat of mutexes, unique_locks, etc etc. * - * @TODO Might be an idea to use an off-the-shelf solution for this, as there are a few available. - * * @tparam Type */ template @@ -20,36 +18,25 @@ namespace Bloom public: SyncSafe() = default; - explicit SyncSafe(Type value): value(value) {}; + explicit SyncSafe(Type value) + : value(value) + {} - void setValue(Type value) { + void setValue(const Type& value) { auto lock = std::unique_lock(this->mutex); this->value = value; - }; + } - Type getValue() { - auto lock = std::unique_lock(this->mutex); + Type& getValue() { return this->value; - }; - - Type& getReference() { - return this->value; - }; - - void lock() { - this->mutex.lock(); - }; - - void unlock() { - this->mutex.unlock(); - }; + } std::unique_lock acquireLock() { return std::unique_lock(this->mutex); - }; + } private: - std::mutex mutex; Type value; + std::mutex mutex; }; } diff --git a/src/Helpers/Thread.hpp b/src/Helpers/Thread.hpp index 5240bc4f..919b12f6 100644 --- a/src/Helpers/Thread.hpp +++ b/src/Helpers/Thread.hpp @@ -29,8 +29,9 @@ namespace Bloom Thread& operator = (Thread&& other) = delete; virtual ThreadState getThreadState() { + auto lock = this->state.acquireLock(); return this->state.getValue(); - }; + } protected: virtual void setThreadState(ThreadState state) {