2021-12-16 23:48:52 +01:00
|
|
|
#include <hex/api/task.hpp>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
2022-09-03 23:56:57 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-07-29 18:49:43 +02:00
|
|
|
#include <algorithm>
|
2022-10-04 23:37:48 +02:00
|
|
|
#include <ranges>
|
2022-07-29 18:49:43 +02:00
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-12-28 23:06:27 +01:00
|
|
|
std::mutex TaskManager::s_deferredCallsMutex, TaskManager::s_tasksFinishedMutex;
|
2022-08-17 16:15:36 +02:00
|
|
|
|
2022-10-05 21:25:52 +02:00
|
|
|
std::list<std::shared_ptr<Task>> TaskManager::s_tasks, TaskManager::s_taskQueue;
|
2022-08-17 16:15:36 +02:00
|
|
|
std::list<std::function<void()>> TaskManager::s_deferredCalls;
|
2022-12-27 11:35:20 +01:00
|
|
|
std::list<std::function<void()>> TaskManager::s_tasksFinishedCallbacks;
|
2022-08-17 16:15:36 +02:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
std::mutex TaskManager::s_queueMutex;
|
|
|
|
std::condition_variable TaskManager::s_jobCondVar;
|
|
|
|
std::vector<std::jthread> TaskManager::s_workers;
|
2022-08-17 16:15:36 +02:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
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_function(std::move(function)), m_background(background) { }
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
Task::Task(hex::Task &&other) noexcept {
|
2022-10-04 23:37:48 +02:00
|
|
|
{
|
|
|
|
std::scoped_lock thisLock(this->m_mutex);
|
|
|
|
std::scoped_lock otherLock(other.m_mutex);
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
this->m_function = std::move(other.m_function);
|
|
|
|
this->m_unlocalizedName = std::move(other.m_unlocalizedName);
|
|
|
|
}
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
this->m_maxValue = u64(other.m_maxValue);
|
|
|
|
this->m_currValue = u64(other.m_currValue);
|
2022-08-17 16:15:36 +02:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
this->m_finished = bool(other.m_finished);
|
|
|
|
this->m_hadException = bool(other.m_hadException);
|
|
|
|
this->m_interrupted = bool(other.m_interrupted);
|
|
|
|
this->m_shouldInterrupt = bool(other.m_shouldInterrupt);
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Task::~Task() {
|
2022-09-14 13:51:48 +02:00
|
|
|
if (!this->isFinished())
|
|
|
|
this->interrupt();
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
void Task::update(u64 value) {
|
|
|
|
this->m_currValue = value;
|
2022-07-29 18:49:43 +02:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
if (this->m_shouldInterrupt)
|
|
|
|
throw TaskInterruptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Task::setMaxValue(u64 value) {
|
|
|
|
this->m_maxValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Task::interrupt() {
|
|
|
|
this->m_shouldInterrupt = true;
|
2022-09-13 16:05:41 +02:00
|
|
|
|
|
|
|
if (this->m_interruptCallback)
|
|
|
|
this->m_interruptCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Task::setInterruptCallback(std::function<void()> callback) {
|
|
|
|
this->m_interruptCallback = std::move(callback);
|
2022-08-17 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
bool Task::isBackgroundTask() const {
|
2022-09-19 16:09:22 +02:00
|
|
|
return this->m_background;
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool Task::isFinished() const {
|
|
|
|
return this->m_finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::hadException() const {
|
|
|
|
return this->m_hadException;
|
|
|
|
}
|
|
|
|
|
2022-10-28 14:32:28 +02:00
|
|
|
bool Task::shouldInterrupt() const {
|
|
|
|
return this->m_shouldInterrupt;
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool Task::wasInterrupted() const {
|
|
|
|
return this->m_interrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Task::clearException() {
|
|
|
|
this->m_hadException = false;
|
|
|
|
}
|
|
|
|
|
2022-09-03 23:56:57 +02:00
|
|
|
std::string Task::getExceptionMessage() const {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
return this->m_exceptionMessage;
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
const std::string &Task::getUnlocalizedName() {
|
|
|
|
return this->m_unlocalizedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 Task::getValue() const {
|
|
|
|
return this->m_currValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 Task::getMaxValue() const {
|
|
|
|
return this->m_maxValue;
|
2022-07-29 18:49:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
void Task::finish() {
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Task::interruption() {
|
|
|
|
this->m_interrupted = true;
|
|
|
|
}
|
|
|
|
|
2022-09-03 23:56:57 +02:00
|
|
|
void Task::exception(const char *message) {
|
2022-08-17 16:15:36 +02:00
|
|
|
std::scoped_lock lock(this->m_mutex);
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-09-03 23:56:57 +02:00
|
|
|
this->m_exceptionMessage = message;
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_hadException = true;
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
bool TaskHolder::isRunning() const {
|
2022-10-05 12:02:40 +02:00
|
|
|
if (this->m_task.expired())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto task = this->m_task.lock();
|
|
|
|
return !task->isFinished();
|
2022-01-09 21:27:59 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool TaskHolder::hadException() const {
|
2022-10-05 12:02:40 +02:00
|
|
|
if (this->m_task.expired())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto task = this->m_task.lock();
|
|
|
|
return !task->hadException();
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-10-28 14:32:28 +02:00
|
|
|
bool TaskHolder::shouldInterrupt() const {
|
|
|
|
if (this->m_task.expired())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto task = this->m_task.lock();
|
|
|
|
return !task->shouldInterrupt();
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool TaskHolder::wasInterrupted() const {
|
2022-10-05 12:02:40 +02:00
|
|
|
if (this->m_task.expired())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto task = this->m_task.lock();
|
|
|
|
return !task->wasInterrupted();
|
2022-08-17 16:15:36 +02:00
|
|
|
}
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
void TaskHolder::interrupt() {
|
2022-10-05 12:02:40 +02:00
|
|
|
if (this->m_task.expired())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto task = this->m_task.lock();
|
|
|
|
task->interrupt();
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
void TaskManager::init() {
|
|
|
|
for (u32 i = 0; i < std::thread::hardware_concurrency(); i++)
|
|
|
|
TaskManager::s_workers.emplace_back(TaskManager::runner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::exit() {
|
|
|
|
for (auto &task : TaskManager::s_tasks)
|
|
|
|
task->interrupt();
|
|
|
|
|
|
|
|
for (auto &thread : TaskManager::s_workers)
|
|
|
|
thread.request_stop();
|
|
|
|
|
|
|
|
s_jobCondVar.notify_all();
|
|
|
|
|
|
|
|
TaskManager::s_workers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::runner(const std::stop_token &stopToken) {
|
|
|
|
std::mutex mutex;
|
|
|
|
while (true) {
|
|
|
|
std::shared_ptr<Task> task;
|
|
|
|
{
|
|
|
|
std::unique_lock lock(s_queueMutex);
|
|
|
|
s_jobCondVar.wait(lock, [&] {
|
2022-10-05 21:25:52 +02:00
|
|
|
return !s_taskQueue.empty() || stopToken.stop_requested();
|
2022-10-04 23:37:48 +02:00
|
|
|
});
|
|
|
|
if (stopToken.stop_requested())
|
|
|
|
break;
|
|
|
|
|
2022-10-05 21:25:52 +02:00
|
|
|
task = std::move(s_taskQueue.front());
|
|
|
|
s_taskQueue.pop_front();
|
2022-10-04 23:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
task->m_function(*task);
|
|
|
|
} catch (const Task::TaskInterruptor &) {
|
|
|
|
task->interruption();
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Exception in task {}: {}", task->m_unlocalizedName, e.what());
|
|
|
|
task->exception(e.what());
|
|
|
|
} catch (...) {
|
|
|
|
log::error("Exception in task {}", task->m_unlocalizedName);
|
|
|
|
task->exception("Unknown Exception");
|
|
|
|
}
|
|
|
|
|
|
|
|
task->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskHolder TaskManager::createTask(std::string name, u64 maxValue, std::function<void(Task &)> function) {
|
2022-10-04 23:37:48 +02:00
|
|
|
std::unique_lock lock(s_queueMutex);
|
2022-10-05 21:25:52 +02:00
|
|
|
auto task = std::make_shared<Task>(std::move(name), maxValue, false, std::move(function));
|
|
|
|
s_tasks.emplace_back(task);
|
|
|
|
s_taskQueue.emplace_back(task);
|
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
s_jobCondVar.notify_one();
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
return TaskHolder(s_tasks.back());
|
2022-01-09 21:27:59 +01:00
|
|
|
}
|
|
|
|
|
2022-09-19 16:09:22 +02:00
|
|
|
TaskHolder TaskManager::createBackgroundTask(std::string name, std::function<void(Task &)> function) {
|
2022-10-04 23:37:48 +02:00
|
|
|
std::unique_lock lock(s_queueMutex);
|
2022-10-05 21:25:52 +02:00
|
|
|
|
|
|
|
auto task = std::make_shared<Task>(std::move(name), 0, true, std::move(function));
|
|
|
|
s_tasks.emplace_back(task);
|
|
|
|
s_taskQueue.emplace_back(task);
|
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
s_jobCondVar.notify_one();
|
2022-09-19 16:09:22 +02:00
|
|
|
|
2022-10-04 23:37:48 +02:00
|
|
|
return TaskHolder(s_tasks.back());
|
2022-09-19 16:09:22 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
void TaskManager::collectGarbage() {
|
2022-12-27 11:35:20 +01:00
|
|
|
std::unique_lock lock1(s_queueMutex);
|
|
|
|
std::unique_lock lock2(s_deferredCallsMutex);
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
std::erase_if(s_tasks, [](const auto &task) { return task->isFinished() && !task->hadException(); });
|
2022-12-27 11:35:20 +01:00
|
|
|
|
|
|
|
if (s_tasks.empty()) {
|
|
|
|
for (auto &call : s_tasksFinishedCallbacks)
|
|
|
|
call();
|
|
|
|
s_tasksFinishedCallbacks.clear();
|
|
|
|
}
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
std::list<std::shared_ptr<Task>> &TaskManager::getRunningTasks() {
|
|
|
|
return s_tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t TaskManager::getRunningTaskCount() {
|
2022-10-04 23:37:48 +02:00
|
|
|
std::unique_lock lock(s_queueMutex);
|
|
|
|
|
|
|
|
return std::count_if(s_tasks.begin(), s_tasks.end(), [](const auto &task){
|
|
|
|
return !task->isBackgroundTask();
|
|
|
|
});
|
2022-08-17 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-27 11:35:20 +01:00
|
|
|
size_t TaskManager::getRunningBackgroundTaskCount() {
|
|
|
|
std::unique_lock lock(s_queueMutex);
|
|
|
|
|
|
|
|
return std::count_if(s_tasks.begin(), s_tasks.end(), [](const auto &task){
|
|
|
|
return task->isBackgroundTask();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
void TaskManager::doLater(const std::function<void()> &function) {
|
|
|
|
std::scoped_lock lock(s_deferredCallsMutex);
|
|
|
|
|
|
|
|
s_deferredCalls.push_back(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::runDeferredCalls() {
|
|
|
|
std::scoped_lock lock(s_deferredCallsMutex);
|
|
|
|
|
|
|
|
for (const auto &call : s_deferredCalls)
|
|
|
|
call();
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
s_deferredCalls.clear();
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2022-12-27 11:35:20 +01:00
|
|
|
void TaskManager::runWhenTasksFinished(const std::function<void()> &function) {
|
2022-12-28 23:06:27 +01:00
|
|
|
std::scoped_lock lock(s_tasksFinishedMutex);
|
2022-12-27 11:35:20 +01:00
|
|
|
|
|
|
|
s_tasksFinishedCallbacks.push_back(function);
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|