From 9f68184f4a4ad4a8bc5b34257435218134fa8834 Mon Sep 17 00:00:00 2001 From: UIS Date: Thu, 10 Dec 2020 15:23:59 +0300 Subject: [PATCH] Fix multithreading Reorder locks in Event.cpp --- src/AssetManager.cpp | 4 +--- src/Event.cpp | 21 ++++++++++++--------- src/Event.hpp | 2 +- src/RendererWorld.cpp | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 033daae8..632f0771 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -367,9 +367,7 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { blockFaces.faceDirectionVector[i] = Vector(roundf(vec.x), roundf(vec.y), roundf(vec.z)); } - blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)); - - return blockIdToBlockFaces.find(block)->second; + return blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)).first->second; } std::string AssetManager::GetAssetNameByBlockId(BlockId block) { diff --git a/src/Event.cpp b/src/Event.cpp index 7df71b17..7735d0e0 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -16,7 +16,7 @@ EventListener::~EventListener() { void EventListener::HandleEvent() { OPTICK_EVENT(); - if (!NotEmpty()) + if (Empty()) return; std::lock_guard eventsLock (eventsMutex); @@ -30,11 +30,14 @@ void EventListener::HandleEvent() { void EventListener::HandleAllEvents() { OPTICK_EVENT(); - if (!NotEmpty()) - return; + //This mutexes will locked in PollEvents std::lock_guard eventsLock (eventsMutex); std::lock_guard handlersLock (handlersMutex); + + if (Empty()) + return; + while (!events.empty()) { Event event = events.front(); events.pop(); @@ -44,11 +47,10 @@ void EventListener::HandleAllEvents() { } } -bool EventListener::NotEmpty() { - PollEvents(); +bool EventListener::Empty() { std::lock_guard eventsLock (eventsMutex); - bool ret = !events.empty(); - return ret; + PollEvents(); + return events.empty(); } void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { @@ -58,12 +60,13 @@ void EventListener::RegisterHandler(size_t eventId, const EventListener::Handler void EventListener::PollEvents() { OPTICK_EVENT(); + std::lock_guard eventsLock (eventsMutex); + std::lock_guard handlersLock (handlersMutex);//To prevent inverse lock order + 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(); diff --git a/src/Event.hpp b/src/Event.hpp index 49426ffd..15916792 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -77,7 +77,7 @@ class EventListener { void HandleAllEvents(); - bool NotEmpty(); + bool Empty(); void RegisterHandler(size_t eventId, const HandlerType &data); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 3a04d179..ed59cc1b 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -34,7 +34,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { LoopExecutionTimeController timer(std::chrono::milliseconds(50)); while (isRunning) { - while (tasksListener.NotEmpty() && isRunning) + while (!tasksListener.Empty() && isRunning) tasksListener.HandleEvent(); timer.Update(); } @@ -394,7 +394,7 @@ void RendererWorld::Render(RenderState & renderState) { entityShader->SetUniform("color", glm::vec3(0.7f, 0, 0)); else entityShader->SetUniform("color", glm::vec3(0, 0, 0.7f)); - glDrawArrays(GL_LINE_STRIP, 0, 36); + glDrawArrays(GL_LINES, 0, 24); } glCheckError(); }