From 357e4f860707d3d1e9e3c9841c416bcc25526413 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 25 Dec 2024 11:04:12 +0300 Subject: [PATCH] refactor: extract post-runnables to a separate class --- src/PostRunnables.hpp | 29 +++++++++++++++++++++++++++++ src/engine.cpp | 16 +--------------- src/engine.hpp | 11 +++++------ 3 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 src/PostRunnables.hpp diff --git a/src/PostRunnables.hpp b/src/PostRunnables.hpp new file mode 100644 index 00000000..dc227c88 --- /dev/null +++ b/src/PostRunnables.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include "delegates.hpp" + +class PostRunnables { + std::queue runnables; + std::recursive_mutex mutex; +public: + void postRunnable(runnable task) { + std::lock_guard lock(mutex); + runnables.push(std::move(task)); + } + + void run() { + std::queue tasksToRun; + { + std::lock_guard lock(mutex); + std::swap(tasksToRun, runnables); + } + + while (!tasksToRun.empty()) { + auto& task = tasksToRun.front(); + task(); + tasksToRun.pop(); + } + } +}; diff --git a/src/engine.cpp b/src/engine.cpp index a3c63035..da69e75e 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -184,7 +184,7 @@ void Engine::run() { void Engine::postUpdate() { network->update(); - processPostRunnables(); + postRunnables.run(); } void Engine::updateFrontend() { @@ -213,15 +213,6 @@ void Engine::renderFrame() { gui->draw(ctx, *assets); } -void Engine::processPostRunnables() { - std::lock_guard lock(postRunnablesMutex); - while (!postRunnables.empty()) { - postRunnables.front()(); - postRunnables.pop(); - } - scripting::process_post_runnables(); -} - void Engine::saveSettings() { logger.info() << "saving settings"; files::write_string(paths.getSettingsFile(), toml::stringify(settingsHandler)); @@ -517,11 +508,6 @@ std::shared_ptr Engine::getScreen() { return screen; } -void Engine::postRunnable(const runnable& callback) { - std::lock_guard lock(postRunnablesMutex); - postRunnables.push(callback); -} - SettingsHandler& Engine::getSettingsHandler() { return settingsHandler; } diff --git a/src/engine.hpp b/src/engine.hpp index f10fb3a0..56231928 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -11,15 +11,14 @@ #include "files/engine_paths.hpp" #include "files/settings_io.hpp" #include "util/ObjectsKeeper.hpp" +#include "PostRunnables.hpp" #include "Time.hpp" #include #include -#include #include #include #include -#include class Level; class Screen; @@ -65,13 +64,12 @@ class Engine : public util::ObjectsKeeper { std::vector contentPacks; std::unique_ptr content; std::unique_ptr resPaths; - std::queue postRunnables; - std::recursive_mutex postRunnablesMutex; std::unique_ptr controller; std::unique_ptr interpreter; std::unique_ptr network; std::vector basePacks; std::unique_ptr gui; + PostRunnables postRunnables; Time time; consumer> levelConsumer; bool quitSignal = false; @@ -80,7 +78,6 @@ class Engine : public util::ObjectsKeeper { void loadSettings(); void saveSettings(); void updateHotkeys(); - void processPostRunnables(); void loadAssets(); public: Engine(CoreParameters coreParameters); @@ -157,7 +154,9 @@ public: std::shared_ptr getScreen(); /// @brief Enqueue function call to the end of current frame in draw thread - void postRunnable(const runnable& callback); + void postRunnable(const runnable& callback) { + postRunnables.postRunnable(callback); + } void saveScreenshot();