Tidied SyncSafe template class

This commit is contained in:
Nav
2022-04-14 22:58:00 +01:00
parent 8be1446e72
commit 75c28ba803
4 changed files with 24 additions and 35 deletions

View File

@@ -7,7 +7,8 @@ namespace Bloom
using namespace Bloom::Events; using namespace Bloom::Events;
std::set<Events::EventType> EventListener::getRegisteredEventTypes() { std::set<Events::EventType> EventListener::getRegisteredEventTypes() {
return this->registeredEventTypes.getValue(); auto lock = this->registeredEventTypes.acquireLock();
return this->registeredEventTypes.getReference();
} }
void EventListener::registerEvent(SharedGenericEventPointer event) { void EventListener::registerEvent(SharedGenericEventPointer event) {

View File

@@ -57,11 +57,11 @@ namespace Bloom
template <class EventType> template <class EventType>
bool isEventTypeRegistered() { bool isEventTypeRegistered() {
return this->registeredEventTypes.getReference().contains(EventType::type); return this->registeredEventTypes.getValue().contains(EventType::type);
} }
bool isEventTypeRegistered(Events::EventType eventType) { bool isEventTypeRegistered(Events::EventType eventType) {
return this->registeredEventTypes.getReference().contains(eventType); return this->registeredEventTypes.getValue().contains(eventType);
}; };
/** /**
@@ -76,13 +76,13 @@ namespace Bloom
template<class EventType> template<class EventType>
void registerEventType() { void registerEventType() {
auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); auto registeredEventTypesLock = this->registeredEventTypes.acquireLock();
this->registeredEventTypes.getReference().insert(EventType::type); this->registeredEventTypes.getValue().insert(EventType::type);
} }
template<class EventType> template<class EventType>
void deRegisterEventType() { void deRegisterEventType() {
auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); 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 mappingLock = this->eventTypeToCallbacksMapping.acquireLock();
auto& mapping = this->eventTypeToCallbacksMapping.getReference(); auto& mapping = this->eventTypeToCallbacksMapping.getValue();
mapping[EventType::type].push_back(parentCallback); mapping[EventType::type].push_back(parentCallback);
this->template registerEventType<EventType>(); this->template registerEventType<EventType>();
@@ -138,7 +138,7 @@ namespace Bloom
{ {
auto mappingLock = this->eventTypeToCallbacksMapping.acquireLock(); auto mappingLock = this->eventTypeToCallbacksMapping.acquireLock();
auto& mapping = this->eventTypeToCallbacksMapping.getReference(); auto& mapping = this->eventTypeToCallbacksMapping.getValue();
if (mapping.contains(EventType::type)) { if (mapping.contains(EventType::type)) {
mapping.at(EventType::type).clear(); mapping.at(EventType::type).clear();
@@ -147,11 +147,11 @@ namespace Bloom
{ {
auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); auto registeredEventTypesLock = this->registeredEventTypes.acquireLock();
this->registeredEventTypes.getReference().erase(EventType::type); this->registeredEventTypes.getValue().erase(EventType::type);
} }
auto queueLock = this->eventQueueByEventType.acquireLock(); auto queueLock = this->eventQueueByEventType.acquireLock();
auto& eventQueueByType = this->eventQueueByEventType.getReference(); auto& eventQueueByType = this->eventQueueByEventType.getValue();
if (eventQueueByType.contains(EventType::type)) { if (eventQueueByType.contains(EventType::type)) {
eventQueueByType.erase(EventType::type); eventQueueByType.erase(EventType::type);
@@ -212,7 +212,7 @@ namespace Bloom
ReturnType output = std::nullopt; ReturnType output = std::nullopt;
auto queueLock = this->eventQueueByEventType.acquireLock(); auto queueLock = this->eventQueueByEventType.acquireLock();
auto& eventQueueByType = this->eventQueueByEventType.getReference(); auto& eventQueueByType = this->eventQueueByEventType.getValue();
auto eventTypes = std::set<Events::EventType>({EventTypeA::type}); auto eventTypes = std::set<Events::EventType>({EventTypeA::type});
auto eventTypesToDeRegister = std::set<Events::EventType>(); auto eventTypesToDeRegister = std::set<Events::EventType>();
@@ -235,7 +235,7 @@ namespace Bloom
{ {
auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); auto registeredEventTypesLock = this->registeredEventTypes.acquireLock();
auto& registeredEventTypes = this->registeredEventTypes.getReference(); auto& registeredEventTypes = this->registeredEventTypes.getValue();
for (const auto& eventType : eventTypes) { for (const auto& eventType : eventTypes) {
if (!registeredEventTypes.contains(eventType)) { if (!registeredEventTypes.contains(eventType)) {
@@ -281,7 +281,7 @@ namespace Bloom
if (!eventTypesToDeRegister.empty()) { if (!eventTypesToDeRegister.empty()) {
auto registeredEventTypesLock = this->registeredEventTypes.acquireLock(); auto registeredEventTypesLock = this->registeredEventTypes.acquireLock();
auto& registeredEventTypes = this->registeredEventTypes.getReference(); auto& registeredEventTypes = this->registeredEventTypes.getValue();
for (const auto& eventType : eventTypesToDeRegister) { for (const auto& eventType : eventTypesToDeRegister) {
registeredEventTypes.erase(eventType); registeredEventTypes.erase(eventType);

View File

@@ -10,8 +10,6 @@ namespace Bloom
* Just a convenient template that allows us to create thread safe types without having to write * Just a convenient template that allows us to create thread safe types without having to write
* the bloat of mutexes, unique_locks, etc etc. * 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 * @tparam Type
*/ */
template<typename Type> template<typename Type>
@@ -20,36 +18,25 @@ namespace Bloom
public: public:
SyncSafe() = default; 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); auto lock = std::unique_lock(this->mutex);
this->value = value; this->value = value;
}; }
Type getValue() { Type& getValue() {
auto lock = std::unique_lock(this->mutex);
return this->value; return this->value;
}; }
Type& getReference() {
return this->value;
};
void lock() {
this->mutex.lock();
};
void unlock() {
this->mutex.unlock();
};
std::unique_lock<std::mutex> acquireLock() { std::unique_lock<std::mutex> acquireLock() {
return std::unique_lock(this->mutex); return std::unique_lock(this->mutex);
}; }
private: private:
std::mutex mutex;
Type value; Type value;
std::mutex mutex;
}; };
} }

View File

@@ -29,8 +29,9 @@ namespace Bloom
Thread& operator = (Thread&& other) = delete; Thread& operator = (Thread&& other) = delete;
virtual ThreadState getThreadState() { virtual ThreadState getThreadState() {
auto lock = this->state.acquireLock();
return this->state.getValue(); return this->state.getValue();
}; }
protected: protected:
virtual void setThreadState(ThreadState state) { virtual void setThreadState(ThreadState state) {