1
0
mirror of synced 2025-01-25 15:53:43 +01:00

impr: Make sure init tasks always get executed

This commit is contained in:
WerWolv 2025-01-04 16:11:05 +01:00
parent 48b202c56b
commit 71f4f87288
4 changed files with 20 additions and 16 deletions

View File

@ -21,6 +21,7 @@ namespace hex::init {
std::string name; std::string name;
std::function<bool()> callback; std::function<bool()> callback;
bool async; bool async;
bool running;
}; };
enum FrameResult{ Success, Failure, Running }; enum FrameResult{ Success, Failure, Running };

View File

@ -27,7 +27,7 @@ namespace hex::init {
// Add initialization tasks to run // Add initialization tasks to run
TaskManager::init(); TaskManager::init();
for (const auto &[name, task, async] : init::getInitTasks()) for (const auto &[name, task, async, running] : init::getInitTasks())
splashWindow->addStartupTask(name, task, async); splashWindow->addStartupTask(name, task, async);
splashWindow->startStartupTasks(); splashWindow->startStartupTasks();

View File

@ -60,7 +60,7 @@ namespace hex::init {
} }
RequestAddInitTask::subscribe([this](const std::string& name, bool async, const TaskFunction &function){ RequestAddInitTask::subscribe([this](const std::string& name, bool async, const TaskFunction &function){
m_tasks.push_back(Task{ name, function, async }); m_tasks.push_back(Task{ name, function, async, false });
}); });
} }
@ -220,14 +220,17 @@ namespace hex::init {
auto startTime = std::chrono::high_resolution_clock::now(); auto startTime = std::chrono::high_resolution_clock::now();
// Loop over all registered init tasks
for (auto it = m_tasks.begin(); it != m_tasks.end(); ++it) {
// Construct a new task callback
this->createTask(*it);
}
// Check every 100ms if all tasks have run // Check every 100ms if all tasks have run
while (true) { while (true) {
// Loop over all registered init tasks
for (auto it = m_tasks.begin(); it != m_tasks.end(); ++it) {
// Construct a new task callback
if (!it->running) {
this->createTask(*it);
it->running = true;
}
}
{ {
std::scoped_lock lock(m_tasksMutex); std::scoped_lock lock(m_tasksMutex);
if (m_completedTaskCount >= m_totalTaskCount) if (m_completedTaskCount >= m_totalTaskCount)

View File

@ -251,7 +251,7 @@ namespace hex::init {
// Run all exit tasks, and print to console // Run all exit tasks, and print to console
void runExitTasks() { void runExitTasks() {
for (const auto &[name, task, async] : init::getExitTasks()) { for (const auto &[name, task, async, running] : init::getExitTasks()) {
const bool result = task(); const bool result = task();
log::info("Exit task '{0}' finished {1}", name, result ? "successfully" : "unsuccessfully"); log::info("Exit task '{0}' finished {1}", name, result ? "successfully" : "unsuccessfully");
} }
@ -259,18 +259,18 @@ namespace hex::init {
std::vector<Task> getInitTasks() { std::vector<Task> getInitTasks() {
return { return {
{ "Setting up environment", setupEnvironment, false }, { "Setting up environment", setupEnvironment, false, false },
{ "Creating directories", createDirectories, false }, { "Creating directories", createDirectories, false, false },
{ "Loading settings", loadSettings, false }, { "Loading settings", loadSettings, false, false },
{ "Loading plugins", loadPlugins, false }, { "Loading plugins", loadPlugins, false, false },
}; };
} }
std::vector<Task> getExitTasks() { std::vector<Task> getExitTasks() {
return { return {
{ "Prepare exit", prepareExit, false }, { "Prepare exit", prepareExit, false, false },
{ "Unloading plugins", unloadPlugins, false }, { "Unloading plugins", unloadPlugins, false, false },
{ "Deleting old files", deleteOldFiles, false }, { "Deleting old files", deleteOldFiles, false, false },
}; };
} }