From f090b30e587c7af51fde86f36c67ef139ed2ce6f Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 5 Jan 2018 18:12:13 +0300 Subject: Tried to fix deadlock --- src/Event.cpp | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/Event.cpp') diff --git a/src/Event.cpp b/src/Event.cpp index c857947..e8531d4 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -1,50 +1,55 @@ #include "Event.hpp" std::list EventSystem::listeners; -std::mutex EventSystem::listenersMutex; +std::recursive_mutex EventSystem::listenersMutex; EventListener::EventListener() { - std::lock_guard listenersLock(EventSystem::listenersMutex); + EventSystem::listenersMutex.lock(); EventSystem::listeners.push_back(this); + EventSystem::listenersMutex.unlock(); } EventListener::~EventListener() { - std::lock_guard listenersLock(EventSystem::listenersMutex); + EventSystem::listenersMutex.lock(); EventSystem::listeners.remove(this); + EventSystem::listenersMutex.unlock(); } void EventListener::HandleEvent() { - std::lock_guard lock(eventsQueueMutex); - std::lock_guard lockHandlers(handlersMutex); + mutex.lock(); Event event = events.front(); events.pop(); if (handlers[event.id]) { handlers[event.id](event); } + mutex.unlock(); } void EventListener::HandleAllEvents() { - std::lock_guard lock(eventsQueueMutex); - std::lock_guard lockHandlers(handlersMutex); + if (!NotEmpty()) + return; + + mutex.lock(); while (!events.empty()) { Event event = events.front(); events.pop(); if (handlers[event.id]) { handlers[event.id](event); } - } + } + mutex.unlock(); } bool EventListener::NotEmpty() { - std::lock_guard lock(eventsQueueMutex); - return !events.empty(); + bool ret = !events.empty(); + return ret; } void EventListener::WaitEvent() { - eventsQueueMutex.lock(); + mutex.lock(); while (events.empty()) { - eventsQueueMutex.unlock(); - eventsQueueMutex.lock(); + mutex.unlock(); + mutex.lock(); } - eventsQueueMutex.unlock(); + mutex.unlock(); } \ No newline at end of file -- cgit v1.2.3