Skip to content

Commit

Permalink
Fix multithreading
Browse files Browse the repository at this point in the history
Reorder locks in Event.cpp
  • Loading branch information
uis246 committed Dec 11, 2020
1 parent fde8e2d commit 9f68184
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 12 additions & 9 deletions src/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ EventListener::~EventListener() {

void EventListener::HandleEvent() {
OPTICK_EVENT();
if (!NotEmpty())
if (Empty())
return;

std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
Expand All @@ -30,11 +30,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);

if (Empty())
return;

while (!events.empty()) {
Event event = events.front();
events.pop();
Expand All @@ -44,11 +47,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) {
Expand All @@ -58,12 +60,13 @@ void EventListener::RegisterHandler(size_t eventId, const EventListener::Handler

void EventListener::PollEvents() {
OPTICK_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;

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();
Expand Down
2 changes: 1 addition & 1 deletion src/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class EventListener {

void HandleAllEvents();

bool NotEmpty();
bool Empty();

void RegisterHandler(size_t eventId, const HandlerType &data);

Expand Down
4 changes: 2 additions & 2 deletions src/RendererWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 9f68184

Please sign in to comment.