From ccdb305b54b3a4e7fbcf6e3a261dc8decfc3a695 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 10 Mar 2024 13:00:34 -0400 Subject: [PATCH] Added basic job system for async --- Editor/src/Windows/EditorInterface.cpp | 18 +++++++-- Nuake/src/Scripting/ScriptingEngineNet.cpp | 1 + Nuake/src/Threading/Job.cpp | 25 +++++++++++++ Nuake/src/Threading/Job.h | 25 +++++++++++++ Nuake/src/Threading/JobSystem.h | 43 ++++++++++++++++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 Nuake/src/Threading/Job.cpp create mode 100644 Nuake/src/Threading/Job.h create mode 100644 Nuake/src/Threading/JobSystem.h diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 83e19b36..46d4e009 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -53,6 +53,7 @@ #include #include +#include namespace Nuake { @@ -127,10 +128,14 @@ namespace Nuake { { if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30)) || (Input::IsKeyPressed(GLFW_KEY_F5))) { - SceneSnapshot = Engine::GetCurrentScene()->Copy(); + this->SceneSnapshot = Engine::GetCurrentScene()->Copy(); - ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); - Engine::EnterPlayMode(); + auto job = [this]() + { + ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); + }; + + JobSystem::Get().Dispatch(job, []() { Engine::EnterPlayMode(); }); } if (ImGui::BeginItemTooltip()) @@ -240,7 +245,12 @@ namespace Nuake { if (ImGui::Button(ICON_FA_HAMMER, ImVec2(30, 30))) { - Nuake::ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); + JobSystem::Get().Dispatch([]() + { + Nuake::ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); + }, + []() {} + ); } if (ImGui::BeginItemTooltip()) diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index 13bd272b..3544a1b0 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -3,6 +3,7 @@ #include "src/Core/Logger.h" #include "src/Core/FileSystem.h" #include "src/Core/OS.h" +#include "src/Threading/JobSystem.h" #include "src/Resource/Project.h" #include "src/Scene/Components/NetScriptComponent.h" diff --git a/Nuake/src/Threading/Job.cpp b/Nuake/src/Threading/Job.cpp new file mode 100644 index 00000000..36b18160 --- /dev/null +++ b/Nuake/src/Threading/Job.cpp @@ -0,0 +1,25 @@ +#include "Job.h" + +namespace Nuake { + + Job::Job(std::function job, std::function end) + : m_Job(job) + , m_End(end) + { + m_End = end; + + m_Thread = std::thread([this, job]() + { + job(); + m_IsDone = true; + }); + } + + void Job::End() + { + if (m_End) + { + m_End(); + } + } +} \ No newline at end of file diff --git a/Nuake/src/Threading/Job.h b/Nuake/src/Threading/Job.h new file mode 100644 index 00000000..c4f3bd0e --- /dev/null +++ b/Nuake/src/Threading/Job.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +namespace Nuake { + + class Job + { + public: + Job(std::function job, std::function end); + Job(const Job&) = delete; + Job& operator=(const Job&) = delete; + ~Job() { m_Thread.join(); } + bool IsDone() { return m_IsDone; } + + void End(); + private: + std::thread m_Thread; + std::atomic m_IsDone; + std::function m_Job; + std::function m_End; + }; +} \ No newline at end of file diff --git a/Nuake/src/Threading/JobSystem.h b/Nuake/src/Threading/JobSystem.h new file mode 100644 index 00000000..a1b39a29 --- /dev/null +++ b/Nuake/src/Threading/JobSystem.h @@ -0,0 +1,43 @@ +#pragma once +#include "Job.h" + +namespace Nuake { + + class JobSystem + { + private: + std::vector> m_Jobs; + + public: + + JobSystem() = default; + ~JobSystem() = default; + + static JobSystem& Get() + { + static JobSystem instance; + return instance; + } + + void Dispatch(std::function job, std::function end) + { + m_Jobs.push_back(std::make_unique(job, end)); + } + + void Update() + { + for (auto it = m_Jobs.begin(); it != m_Jobs.end();) + { + if (it->get()->IsDone()) + { + it->get()->End(); + it = m_Jobs.erase(it); + } + else + { + ++it; + } + } + } + }; +} \ No newline at end of file