From 1779db1201f3c514997624381b520d89c7c4d5a7 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Tue, 29 Aug 2017 13:08:18 +0100 Subject: cWorld: Add entities without holding of m_CSEntitiesToAdd * Fixes deadlock when cWorld::AddEntity is called while holding chunk map CS. --- src/World.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/World.cpp b/src/World.cpp index 04ca1709e..acec3049e 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -995,18 +995,21 @@ void cWorld::Tick(std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_La } // Add entities waiting in the queue to be added: + cEntityList EntitiesToAdd; { + // Don't access chunkmap while holding lock cCSLock Lock(m_CSEntitiesToAdd); - for (auto & Entity : m_EntitiesToAdd) - { - Entity->SetWorld(this); - auto EntityPtr = Entity.get(); - m_ChunkMap->AddEntity(std::move(Entity)); - ASSERT(!EntityPtr->IsTicking()); - EntityPtr->SetIsTicking(true); - } - m_EntitiesToAdd.clear(); + std::swap(EntitiesToAdd, m_EntitiesToAdd); + } + for (auto & Entity : EntitiesToAdd) + { + Entity->SetWorld(this); + auto EntityPtr = Entity.get(); + m_ChunkMap->AddEntity(std::move(Entity)); + ASSERT(!EntityPtr->IsTicking()); + EntityPtr->SetIsTicking(true); } + EntitiesToAdd.clear(); // Add players waiting in the queue to be added: AddQueuedPlayers(); -- cgit v1.2.3