sys: Improved startup time by running startup tasks in parallel

This commit is contained in:
WerWolv
2022-09-19 16:54:19 +02:00
parent 7b61268f22
commit 4c01a749de
8 changed files with 54 additions and 25 deletions
+18 -3
View File
@@ -43,15 +43,27 @@ namespace hex::init {
return std::async(std::launch::async, [this] {
bool status = true;
for (const auto &[name, task] : this->m_tasks) {
{
u32 tasksCompleted = 0;
for (const auto &[name, task, async] : this->m_tasks) {
if (!async) {
std::lock_guard guard(this->m_progressMutex);
this->m_currTaskName = name;
}
try {
auto runTask = [&, task = task] {
if (!task())
status = false;
tasksCompleted++;
};
try {
if (async) {
std::thread(runTask).detach();
} else {
runTask();
}
} catch (std::exception &e) {
log::error("Init task '{}' threw an exception: {}", name, e.what());
status = false;
@@ -63,6 +75,9 @@ namespace hex::init {
}
}
while (tasksCompleted < this->m_tasks.size())
std::this_thread::sleep_for(100ms);
// Small extra delay so the last progress step is visible
std::this_thread::sleep_for(200ms);