Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/MapGenerator.cpp
Show All 24 Lines | |||||
#include "lib/external_libraries/libsdl.h" | #include "lib/external_libraries/libsdl.h" | ||||
#include "lib/status.h" | #include "lib/status.h" | ||||
#include "lib/timer.h" | #include "lib/timer.h" | ||||
#include "lib/file/vfs/vfs_path.h" | #include "lib/file/vfs/vfs_path.h" | ||||
#include "maths/MathUtil.h" | #include "maths/MathUtil.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/FileIo.h" | #include "ps/FileIo.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "ps/Threading.h" | #include "ps/ThreadPool.h" | ||||
#include "ps/scripting/JSInterface_VFS.h" | #include "ps/scripting/JSInterface_VFS.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | #include "scriptinterface/FunctionWrapper.h" | ||||
#include "scriptinterface/ScriptContext.h" | #include "scriptinterface/ScriptContext.h" | ||||
#include "scriptinterface/ScriptConversions.h" | #include "scriptinterface/ScriptConversions.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "simulation2/helpers/MapEdgeTiles.h" | #include "simulation2/helpers/MapEdgeTiles.h" | ||||
#include <string> | #include <string> | ||||
Show All 21 Lines | CMapGeneratorWorker::CMapGeneratorWorker(ScriptInterface* scriptInterface) : | ||||
m_ScriptInterface(scriptInterface) | m_ScriptInterface(scriptInterface) | ||||
{ | { | ||||
// If something happens before we initialize, that's a failure | // If something happens before we initialize, that's a failure | ||||
m_Progress = -1; | m_Progress = -1; | ||||
} | } | ||||
CMapGeneratorWorker::~CMapGeneratorWorker() | CMapGeneratorWorker::~CMapGeneratorWorker() | ||||
{ | { | ||||
// Wait for thread to end | // Cancel or wait for the task to end. | ||||
if (m_WorkerThread.joinable()) | m_WorkerThread.CancelOrWait(); | ||||
wraitii: Need CancelOrWait here | |||||
m_WorkerThread.join(); | |||||
} | } | ||||
void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::string& settings) | void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::string& settings) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_WorkerMutex); | ||||
// Set progress to positive value | // Set progress to positive value | ||||
m_Progress = 1; | m_Progress = 1; | ||||
m_ScriptPath = scriptFile; | m_ScriptPath = scriptFile; | ||||
m_Settings = settings; | m_Settings = settings; | ||||
// Launch the worker thread | // Start generating the map asynchronously. | ||||
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this); | m_WorkerThread = ThreadPool::TaskManager::Instance().GetWorker().Submit([this]() { | ||||
} | PROFILE2("Map Generation"); | ||||
void CMapGeneratorWorker::RunThread(CMapGeneratorWorker* self) | |||||
{ | |||||
debug_SetThreadName("MapGenerator"); | |||||
Not Done Inline ActionsFor my personal culture, why can't a thread be renamed on the fly? Stan: For my personal culture, why can't a thread be renamed on the fly? | |||||
Done Inline ActionsIt can be renamed on the fly, but then the name would stick, and I'd have to re-rename thread in the thread pool, so it seems easier to just use Profile2 to identify what's going on. wraitii: It can be renamed on the fly, but then the name would stick, and I'd have to re-rename thread… | |||||
g_Profiler2.RegisterCurrentThread("MapGenerator"); | |||||
shared_ptr<ScriptContext> mapgenContext = ScriptContext::CreateContext(RMS_CONTEXT_SIZE); | shared_ptr<ScriptContext> mapgenContext = ScriptContext::CreateContext(RMS_CONTEXT_SIZE); | ||||
// Enable the script to be aborted | // Enable the script to be aborted | ||||
JS_AddInterruptCallback(mapgenContext->GetGeneralJSContext(), MapGeneratorInterruptCallback); | JS_AddInterruptCallback(mapgenContext->GetGeneralJSContext(), MapGeneratorInterruptCallback); | ||||
self->m_ScriptInterface = new ScriptInterface("Engine", "MapGenerator", mapgenContext); | m_ScriptInterface = new ScriptInterface("Engine", "MapGenerator", mapgenContext); | ||||
Not Done Inline ActionsShould be std::unique_ptr btw. vladislavbelov: Should be `std::unique_ptr` btw. | |||||
// Run map generation scripts | // Run map generation scripts | ||||
if (!self->Run() || self->m_Progress > 0) | if (!Run() || m_Progress > 0) | ||||
{ | { | ||||
// Don't leave progress in an unknown state, if generator failed, set it to -1 | // Don't leave progress in an unknown state, if generator failed, set it to -1 | ||||
std::lock_guard<std::mutex> lock(self->m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_WorkerMutex); | ||||
self->m_Progress = -1; | m_Progress = -1; | ||||
} | } | ||||
SAFE_DELETE(self->m_ScriptInterface); | SAFE_DELETE(m_ScriptInterface); | ||||
// At this point the random map scripts are done running, so the thread has no further purpose | // At this point the random map scripts are done running, so the thread has no further purpose | ||||
// and can die. The data will be stored in m_MapData already if successful, or m_Progress | // and can die. The data will be stored in m_MapData already if successful, or m_Progress | ||||
// will contain an error value on failure. | // will contain an error value on failure. | ||||
}); | |||||
} | } | ||||
bool CMapGeneratorWorker::Run() | bool CMapGeneratorWorker::Run() | ||||
{ | { | ||||
ScriptRequest rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
// Parse settings | // Parse settings | ||||
JS::RootedValue settingsVal(rq.cx); | JS::RootedValue settingsVal(rq.cx); | ||||
▲ Show 20 Lines • Show All 307 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Need CancelOrWait here