From 607be749451aa40e3619e7ceab0927d1fcec8233 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Thu, 20 Jul 2023 19:10:41 +1000 Subject: Fix server hang while looking for starter world --- source/game/StarCelestialDatabase.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'source/game/StarCelestialDatabase.cpp') diff --git a/source/game/StarCelestialDatabase.cpp b/source/game/StarCelestialDatabase.cpp index d5ba7df..dd0bc51 100644 --- a/source/game/StarCelestialDatabase.cpp +++ b/source/game/StarCelestialDatabase.cpp @@ -206,9 +206,6 @@ bool CelestialMasterDatabase::coordinateValid(CelestialCoordinate const& coordin Maybe CelestialMasterDatabase::findRandomWorld(unsigned tries, unsigned trySpatialRange, function filter, Maybe seed) { - //RecursiveMutexLocker locker(m_mutex); - // We don't need this lock, the other calls are locking anyway. - // Having this here is just stopping other threads from having a go in here until we've found a world. RandomSource randSource; if (seed) randSource.init(*seed); @@ -299,7 +296,12 @@ List CelestialMasterDatabase::scanSystems(RectI const& regi List systems; for (auto const& chunkLocation : chunkIndexesFor(region)) { - auto const& chunkData = getChunk(chunkLocation); + auto const& chunkData = getChunk(chunkLocation, [&](std::function&& func) { + locker.unlock(); + func(); + locker.lock(); + }); + locker.unlock(); for (auto const& pair : chunkData.systemParameters) { Vec3I systemLocation = pair.first; if (region.contains(systemLocation.vec2())) { @@ -311,6 +313,7 @@ List CelestialMasterDatabase::scanSystems(RectI const& regi systems.append(CelestialCoordinate(systemLocation)); } } + locker.lock(); } return systems; } @@ -378,8 +381,8 @@ Maybe CelestialMasterDatabase::orbitRegion( return {}; } -CelestialChunk const& CelestialMasterDatabase::getChunk(Vec2I const& chunkIndex) { - return m_chunkCache.get(chunkIndex, [this](Vec2I const& chunkIndex) -> CelestialChunk { +CelestialChunk const& CelestialMasterDatabase::getChunk(Vec2I const& chunkIndex, UnlockDuringFunction unlockDuring) { + return m_chunkCache.get(chunkIndex, [&](Vec2I const& chunkIndex) -> CelestialChunk { auto versioningDatabase = Root::singleton().versioningDatabase(); if (m_database.isOpen()) { @@ -394,7 +397,12 @@ CelestialChunk const& CelestialMasterDatabase::getChunk(Vec2I const& chunkIndex) } } - auto newChunk = produceChunk(chunkIndex); + CelestialChunk newChunk; + auto producer = [&]() { newChunk = produceChunk(chunkIndex); }; + if (unlockDuring) + unlockDuring(producer); + else + producer(); if (m_database.isOpen()) { auto versionedChunk = versioningDatabase->makeCurrentVersionedJson("CelestialChunk", newChunk.toJson()); m_database.insert(DataStreamBuffer::serialize(chunkIndex), -- cgit v1.2.3