From e31d70f05934028b07e36da10f5bc7a6ca2ad15d Mon Sep 17 00:00:00 2001 From: phosit Date: Sun, 18 Jun 2023 12:19:26 +0000 Subject: [PATCH] Removes waiting from MapGenerator. This makes the game window more responsible during map generating. - remove `SDL_Delay(100)` - the progress is now an `std::atomic` (not protected by the mutex anymore) Based On Patch By: @vladislavbelov Accepted By: @wraitii Differential Revision: https://code.wildfiregames.com/D3676 This was SVN commit r27717. --- source/graphics/MapGenerator.cpp | 43 +++++++++++++++----------------- source/graphics/MapGenerator.h | 11 +++++--- source/graphics/MapReader.cpp | 11 +------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/source/graphics/MapGenerator.cpp b/source/graphics/MapGenerator.cpp index 799b746129..074cb93aae 100644 --- a/source/graphics/MapGenerator.cpp +++ b/source/graphics/MapGenerator.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -62,10 +62,7 @@ MapGeneratorInterruptCallback(JSContext* UNUSED(cx)) CMapGeneratorWorker::CMapGeneratorWorker(ScriptInterface* scriptInterface) : m_ScriptInterface(scriptInterface) -{ - // If something happens before we initialize, that's a failure - m_Progress = -1; -} +{} CMapGeneratorWorker::~CMapGeneratorWorker() { @@ -78,7 +75,7 @@ void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::strin std::lock_guard lock(m_WorkerMutex); // Set progress to positive value - m_Progress = 1; + m_Progress.store(1); m_ScriptPath = scriptFile; m_Settings = settings; @@ -94,11 +91,10 @@ void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::strin m_ScriptInterface = new ScriptInterface("Engine", "MapGenerator", mapgenContext); // Run map generation scripts - if (!Run() || m_Progress > 0) + if (!Run() || m_Progress.load() > 0) { // Don't leave progress in an unknown state, if generator failed, set it to -1 - std::lock_guard lock(m_WorkerMutex); - m_Progress = -1; + m_Progress.store(-1); } SAFE_DELETE(m_ScriptInterface); @@ -210,10 +206,9 @@ void CMapGeneratorWorker::RegisterScriptFunctions_MapGenerator() #undef REGISTER_MAPGEN_FUNC #undef REGISTER_MAPGEN_FUNC_NAME -int CMapGeneratorWorker::GetProgress() +int CMapGeneratorWorker::GetProgress() const { - std::lock_guard lock(m_WorkerMutex); - return m_Progress; + return m_Progress.load(); } double CMapGeneratorWorker::GetMicroseconds() @@ -229,21 +224,23 @@ Script::StructuredClone CMapGeneratorWorker::GetResults() void CMapGeneratorWorker::ExportMap(JS::HandleValue data) { - // Copy results - std::lock_guard lock(m_WorkerMutex); - m_MapData = Script::WriteStructuredClone(ScriptRequest(m_ScriptInterface), data); - m_Progress = 0; + { + // Copy results + std::lock_guard lock(m_WorkerMutex); + m_MapData = Script::WriteStructuredClone(ScriptRequest(m_ScriptInterface), data); + } + m_Progress.store(0); } void CMapGeneratorWorker::SetProgress(int progress) { - // Copy data - std::lock_guard lock(m_WorkerMutex); - - if (progress >= m_Progress) - m_Progress = progress; + // When the task is started, `m_Progress` is only mutated by this thread. + const int currentProgress = m_Progress.load(); + if (progress >= currentProgress) + m_Progress.store(progress); else - LOGWARNING("The random map script tried to reduce the loading progress from %d to %d", m_Progress, progress); + LOGWARNING("The random map script tried to reduce the loading progress from %d to %d", + currentProgress, progress); } CParamNode CMapGeneratorWorker::GetTemplate(const std::string& templateName) @@ -413,7 +410,7 @@ void CMapGenerator::GenerateMap(const VfsPath& scriptFile, const std::string& se m_Worker->Initialize(scriptFile, settings); } -int CMapGenerator::GetProgress() +int CMapGenerator::GetProgress() const { return m_Worker->GetProgress(); } diff --git a/source/graphics/MapGenerator.h b/source/graphics/MapGenerator.h index fc8df8ad96..eef9ec0256 100644 --- a/source/graphics/MapGenerator.h +++ b/source/graphics/MapGenerator.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -23,6 +23,7 @@ #include "ps/TemplateLoader.h" #include "scriptinterface/StructuredClone.h" +#include #include #include #include @@ -57,7 +58,7 @@ public: * * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error */ - int GetProgress(); + int GetProgress() const; /** * Get random map data, according to this format: @@ -100,7 +101,7 @@ public: * * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error */ - int GetProgress(); + int GetProgress() const; /** * Get random map data, according to this format: @@ -198,8 +199,10 @@ private: /** * Current map generation progress. + * Initialize to `-1`. If something happens before we start, that's a + * failure. */ - int m_Progress; + std::atomic m_Progress{-1}; /** * Provides the script context. diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index f9dc99ac5f..1519f3fc04 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -29,7 +29,6 @@ #include "graphics/TerrainTextureEntry.h" #include "graphics/TerrainTextureManager.h" #include "lib/timer.h" -#include "lib/external_libraries/libsdl.h" #include "maths/MathUtil.h" #include "ps/CLogger.h" #include "ps/Loader.h" @@ -1319,14 +1318,6 @@ int CMapReader::GenerateMap() m_MapData.init(rq.cx, data); } } - else - { - // Still working - - // Sleep for a while, slowing down the rendering thread - // to allow more CPU for the map generator thread - SDL_Delay(100); - } // return progress return progress;