refactor: extract post-runnables to a separate class

This commit is contained in:
MihailRis 2024-12-25 11:04:12 +03:00
parent e98132f4da
commit 357e4f8607
3 changed files with 35 additions and 21 deletions

29
src/PostRunnables.hpp Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#include <queue>
#include <mutex>
#include "delegates.hpp"
class PostRunnables {
std::queue<runnable> runnables;
std::recursive_mutex mutex;
public:
void postRunnable(runnable task) {
std::lock_guard<std::recursive_mutex> lock(mutex);
runnables.push(std::move(task));
}
void run() {
std::queue<runnable> tasksToRun;
{
std::lock_guard<std::recursive_mutex> lock(mutex);
std::swap(tasksToRun, runnables);
}
while (!tasksToRun.empty()) {
auto& task = tasksToRun.front();
task();
tasksToRun.pop();
}
}
};

View File

@ -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<std::recursive_mutex> 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<Screen> Engine::getScreen() {
return screen;
}
void Engine::postRunnable(const runnable& callback) {
std::lock_guard<std::recursive_mutex> lock(postRunnablesMutex);
postRunnables.push(callback);
}
SettingsHandler& Engine::getSettingsHandler() {
return settingsHandler;
}

View File

@ -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 <filesystem>
#include <memory>
#include <queue>
#include <stdexcept>
#include <string>
#include <vector>
#include <mutex>
class Level;
class Screen;
@ -65,13 +64,12 @@ class Engine : public util::ObjectsKeeper {
std::vector<ContentPack> contentPacks;
std::unique_ptr<Content> content;
std::unique_ptr<ResPaths> resPaths;
std::queue<runnable> postRunnables;
std::recursive_mutex postRunnablesMutex;
std::unique_ptr<EngineController> controller;
std::unique_ptr<cmd::CommandsInterpreter> interpreter;
std::unique_ptr<network::Network> network;
std::vector<std::string> basePacks;
std::unique_ptr<gui::GUI> gui;
PostRunnables postRunnables;
Time time;
consumer<std::unique_ptr<Level>> 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<Screen> 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();