From b009754d1bc09d9cb26b35e41146029491044b6d Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 12 Jan 2018 16:48:42 +0500 Subject: Added more mutexes -> It's working fine now --- src/Event.cpp | 34 ++++++++++++++++++++++------------ src/Event.hpp | 32 +++++++++++++++----------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Event.cpp b/src/Event.cpp index 7604609..08c9ee7 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -17,9 +17,10 @@ void EventListener::HandleEvent() { if (!NotEmpty()) return; - std::lock_guard lock(EventSystem::listenersMutex); + std::lock_guard eventsLock (eventsMutex); Event event = events.front(); - events.pop(); + events.pop(); + std::lock_guard handlersLock (handlersMutex); if (handlers[event.id]) { handlers[event.id](event); } @@ -29,7 +30,8 @@ void EventListener::HandleAllEvents() { if (!NotEmpty()) return; - std::lock_guard lock(EventSystem::listenersMutex); + std::lock_guard eventsLock (eventsMutex); + std::lock_guard handlersLock (handlersMutex); while (!events.empty()) { Event event = events.front(); events.pop(); @@ -40,19 +42,27 @@ void EventListener::HandleAllEvents() { } bool EventListener::NotEmpty() { + PollEvents(); + std::lock_guard eventsLock (eventsMutex); bool ret = !events.empty(); return ret; } -void EventListener::WaitEvent() { - std::lock_guard lock(EventSystem::listenersMutex); - while (events.empty()) { - mutex.unlock(); - mutex.lock(); - } -} - void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { - std::lock_guard lock(EventSystem::listenersMutex); + std::lock_guard handlersLock (handlersMutex); handlers[eventId] = data; } + +void EventListener::PollEvents() { + std::lock_guard rawLock (rawEventsMutex); + if (rawEvents.empty()) + return; + std::lock_guard eventsLock (eventsMutex); + std::lock_guard handlersLock (handlersMutex); + while (!rawEvents.empty()) { + Event event = rawEvents.front(); + rawEvents.pop(); + if (handlers[event.id]) + events.push(event); + } +} diff --git a/src/Event.hpp b/src/Event.hpp index 0f60d1a..4cdcdc3 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -63,9 +63,12 @@ public: class EventListener { friend class EventSystem; using HandlerType = std::function; - std::queue events; std::map handlers; - std::recursive_mutex mutex; + std::recursive_mutex handlersMutex; + std::queue events; + std::recursive_mutex eventsMutex; + std::queue rawEvents; + std::recursive_mutex rawEventsMutex; public: EventListener(); @@ -77,13 +80,13 @@ public: bool NotEmpty(); - void WaitEvent(); - void RegisterHandler(size_t eventId, const HandlerType &data); void RegisterHandler(const char *eventId, const HandlerType & data) { RegisterHandler(StrHash(eventId), data); } + + void PollEvents(); }; class EventSystem { @@ -96,33 +99,28 @@ public: static void PushEvent(size_t eventId, T data) { Event event(eventId, data); + std::lock_guard lock(EventSystem::listenersMutex); for (auto& listener : listeners) { - std::lock_guard lock(EventSystem::listenersMutex); - - auto it = listener->handlers.find(eventId); - if (it == listener->handlers.end()) - continue; - - listener->events.push(event); + std::lock_guard rawEventLock (listener->rawEventsMutex); + listener->rawEvents.push(event); } } template static void DirectEventCall(size_t eventId, T data) { Event event(eventId, data); - + std::lock_guard lock(EventSystem::listenersMutex); for (auto & listener : listeners) { - std::lock_guard lock(EventSystem::listenersMutex); - + std::lock_guard handlersLock (listener->handlersMutex); auto it = listener->handlers.find(eventId); if (it == listener->handlers.end()) - continue; + continue; it->second(event); } } }; -#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data); LOG(INFO)<<"PUSH_EVENT "<