diff options
author | UIS <uis9936@gmail.com> | 2020-12-10 13:23:59 +0100 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2021-06-24 10:48:13 +0200 |
commit | 33c756b38e94cef68b3f842c1c2f8bad9d4447a2 (patch) | |
tree | bbc7b421b350e50c37e55d951edf15dea5bf667a /src/Event.cpp | |
parent | Shader optimization, removed unused argument from GetBlockInfo function (diff) | |
download | AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.tar AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.tar.gz AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.tar.bz2 AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.tar.lz AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.tar.xz AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.tar.zst AltCraft-33c756b38e94cef68b3f842c1c2f8bad9d4447a2.zip |
Diffstat (limited to 'src/Event.cpp')
-rw-r--r-- | src/Event.cpp | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/Event.cpp b/src/Event.cpp index ea09af3..1ca933f 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -17,7 +17,7 @@ EventListener::~EventListener() { void EventListener::HandleEvent() { OPTICK_EVENT(); - if (!NotEmpty()) + if (Empty()) return; std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); @@ -31,11 +31,14 @@ void EventListener::HandleEvent() { void EventListener::HandleAllEvents() { OPTICK_EVENT(); - if (!NotEmpty()) - return; + //This mutexes will locked in PollEvents std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); - std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); + std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); + + if (Empty()) + return; + while (!events.empty()) { Event event = events.front(); events.pop(); @@ -45,11 +48,10 @@ void EventListener::HandleAllEvents() { } } -bool EventListener::NotEmpty() { - PollEvents(); +bool EventListener::Empty() { std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); - bool ret = !events.empty(); - return ret; + PollEvents(); + return events.empty(); } void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { @@ -59,16 +61,17 @@ void EventListener::RegisterHandler(size_t eventId, const EventListener::Handler void EventListener::PollEvents() { OPTICK_EVENT(); - std::lock_guard<std::recursive_mutex> rawLock (rawEventsMutex); - if (rawEvents.empty()) - return; - - std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); - std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex); - while (!rawEvents.empty()) { - Event event = rawEvents.front(); - rawEvents.pop(); - if (handlers[event.id]) - events.push(event); - } + std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex); + std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);//To prevent inverse lock order + + std::lock_guard<std::recursive_mutex> rawLock (rawEventsMutex); + if (rawEvents.empty()) + return; + + while (!rawEvents.empty()) { + Event event = rawEvents.front(); + rawEvents.pop(); + if (handlers[event.id]) + events.push(event); + } } |