ux: Reduce file loading time to basically zero

This commit is contained in:
WerWolv
2022-09-19 16:09:22 +02:00
parent b11dbe4fe1
commit 7b61268f22
7 changed files with 143 additions and 106 deletions
+17 -4
View File
@@ -9,11 +9,11 @@ namespace hex {
std::mutex TaskManager::s_deferredCallsMutex;
std::list<std::shared_ptr<Task>> TaskManager::s_tasks;
std::list<std::shared_ptr<Task>> TaskManager::s_tasks, s_backgroundTasks;
std::list<std::function<void()>> TaskManager::s_deferredCalls;
Task::Task(std::string unlocalizedName, u64 maxValue, std::function<void(Task &)> function)
: m_unlocalizedName(std::move(unlocalizedName)), m_currValue(0), m_maxValue(maxValue) {
Task::Task(std::string unlocalizedName, u64 maxValue, bool background, std::function<void(Task &)> function)
: m_unlocalizedName(std::move(unlocalizedName)), m_currValue(0), m_maxValue(maxValue), m_background(background) {
this->m_thread = std::thread([this, func = std::move(function)] {
try {
func(*this);
@@ -83,6 +83,12 @@ namespace hex {
this->m_interruptCallback = std::move(callback);
}
bool Task::isBackgroundTask() const {
std::scoped_lock lock(this->m_mutex);
return this->m_background;
}
bool Task::isFinished() const {
std::scoped_lock lock(this->m_mutex);
@@ -164,13 +170,20 @@ namespace hex {
TaskHolder TaskManager::createTask(std::string name, u64 maxValue, std::function<void(Task &)> function) {
s_tasks.emplace_back(std::make_shared<Task>(std::move(name), maxValue, std::move(function)));
s_tasks.emplace_back(std::make_shared<Task>(std::move(name), maxValue, false, std::move(function)));
return TaskHolder(s_tasks.back());
}
TaskHolder TaskManager::createBackgroundTask(std::string name, std::function<void(Task &)> function) {
s_backgroundTasks.emplace_back(std::make_shared<Task>(std::move(name), 0, true, std::move(function)));
return TaskHolder(s_backgroundTasks.back());
}
void TaskManager::collectGarbage() {
std::erase_if(s_tasks, [](const auto &task) { return task->isFinished() && !task->hadException(); });
std::erase_if(s_backgroundTasks, [](const auto &task) { return task->isFinished(); });
}
std::list<std::shared_ptr<Task>> &TaskManager::getRunningTasks() {