From 14a629c0a15dcc095a4d413e4782bc87fc568fdd Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 12 Jan 2018 11:54:50 +0500 Subject: Single mutex for event system --- src/Event.cpp | 25 ++++++++++++++----------- src/Event.hpp | 28 ++++++++++------------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/Event.cpp b/src/Event.cpp index e8531d4..7604609 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -4,32 +4,32 @@ std::list EventSystem::listeners; std::recursive_mutex EventSystem::listenersMutex; EventListener::EventListener() { - EventSystem::listenersMutex.lock(); + std::lock_guard lock(EventSystem::listenersMutex); EventSystem::listeners.push_back(this); - EventSystem::listenersMutex.unlock(); } EventListener::~EventListener() { - EventSystem::listenersMutex.lock(); + std::lock_guard lock(EventSystem::listenersMutex); EventSystem::listeners.remove(this); - EventSystem::listenersMutex.unlock(); } void EventListener::HandleEvent() { - mutex.lock(); + if (!NotEmpty()) + return; + + std::lock_guard lock(EventSystem::listenersMutex); Event event = events.front(); events.pop(); if (handlers[event.id]) { handlers[event.id](event); } - mutex.unlock(); } void EventListener::HandleAllEvents() { if (!NotEmpty()) return; - mutex.lock(); + std::lock_guard lock(EventSystem::listenersMutex); while (!events.empty()) { Event event = events.front(); events.pop(); @@ -37,7 +37,6 @@ void EventListener::HandleAllEvents() { handlers[event.id](event); } } - mutex.unlock(); } bool EventListener::NotEmpty() { @@ -46,10 +45,14 @@ bool EventListener::NotEmpty() { } void EventListener::WaitEvent() { - mutex.lock(); + std::lock_guard lock(EventSystem::listenersMutex); while (events.empty()) { mutex.unlock(); mutex.lock(); } - mutex.unlock(); -} \ No newline at end of file +} + +void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { + std::lock_guard lock(EventSystem::listenersMutex); + handlers[eventId] = data; +} diff --git a/src/Event.hpp b/src/Event.hpp index 38541a1..0f60d1a 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -8,6 +8,8 @@ #include #include +#include + size_t constexpr StrHash(char const *input) { return *input ? static_cast(*input) + 33 * StrHash(input + 1) : 5381; @@ -77,11 +79,7 @@ public: void WaitEvent(); - void RegisterHandler(size_t eventId, const HandlerType &data) { - mutex.lock(); - handlers[eventId] = data; - mutex.unlock(); - } + void RegisterHandler(size_t eventId, const HandlerType &data); void RegisterHandler(const char *eventId, const HandlerType & data) { RegisterHandler(StrHash(eventId), data); @@ -99,17 +97,13 @@ public: Event event(eventId, data); for (auto& listener : listeners) { - //if (!listener->mutex.try_lock()) throw std::runtime_error("WHY?!"); - listener->mutex.lock(); + std::lock_guard lock(EventSystem::listenersMutex); + auto it = listener->handlers.find(eventId); - if (it == listener->handlers.end()) { - listener->mutex.unlock(); + if (it == listener->handlers.end()) continue; - } listener->events.push(event); - - listener->mutex.unlock(); } } @@ -117,20 +111,18 @@ public: static void DirectEventCall(size_t eventId, T data) { Event event(eventId, data); - listenersMutex.lock(); for (auto & listener : listeners) { - listener->mutex.lock(); + std::lock_guard lock(EventSystem::listenersMutex); + auto it = listener->handlers.find(eventId); if (it == listener->handlers.end()) continue; it->second(event); - listener->mutex.unlock(); } - listenersMutex.unlock(); } }; -#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data) +#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data); LOG(INFO)<<"PUSH_EVENT "<