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>
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
std::mutex TaskManager::s_deferredCallsMutex;
|
|
|
|
|
|
|
|
std::list<std::shared_ptr<Task>> TaskManager::s_tasks;
|
|
|
|
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) {
|
|
|
|
this->m_thread = std::thread([this, func = std::move(function)] {
|
|
|
|
try {
|
|
|
|
func(*this);
|
|
|
|
} catch (const TaskInterruptor &) {
|
|
|
|
this->interruption();
|
2022-09-03 23:56:57 +02:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Exception in task {}: {}", this->m_unlocalizedName, e.what());
|
2022-09-04 00:04:27 +02:00
|
|
|
this->exception(e.what());
|
2022-08-17 16:15:36 +02:00
|
|
|
} catch (...) {
|
2022-09-03 23:56:57 +02:00
|
|
|
log::error("Exception in task {}", this->m_unlocalizedName);
|
2022-09-04 00:04:27 +02:00
|
|
|
this->exception("Unknown Exception");
|
2022-08-17 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Task::Task(hex::Task &&other) noexcept {
|
|
|
|
std::scoped_lock thisLock(this->m_mutex);
|
|
|
|
std::scoped_lock otherLock(other.m_mutex);
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_thread = std::move(other.m_thread);
|
|
|
|
this->m_unlocalizedName = std::move(other.m_unlocalizedName);
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_maxValue = other.m_maxValue;
|
|
|
|
this->m_currValue = other.m_currValue;
|
|
|
|
|
|
|
|
this->m_finished = other.m_finished;
|
|
|
|
this->m_hadException = other.m_hadException;
|
|
|
|
this->m_interrupted = other.m_interrupted;
|
|
|
|
this->m_shouldInterrupt = 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();
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
this->m_thread.join();
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
void Task::update(u64 value) {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
2022-07-29 18:49:43 +02:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
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) {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
this->m_maxValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Task::interrupt() {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::isFinished() const {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
return this->m_finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::hadException() const {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
return this->m_hadException;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::wasInterrupted() const {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
return this->m_interrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Task::clearException() {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
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
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
this->m_finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Task::interruption() {
|
|
|
|
std::scoped_lock lock(this->m_mutex);
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return !m_task.expired() && !m_task.lock()->isFinished();
|
2022-01-09 21:27:59 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool TaskHolder::hadException() const {
|
|
|
|
return m_task.expired() || m_task.lock()->hadException();
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool TaskHolder::wasInterrupted() const {
|
|
|
|
return m_task.expired() || m_task.lock()->wasInterrupted();
|
|
|
|
}
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
void TaskHolder::interrupt() {
|
|
|
|
if (!this->m_task.expired())
|
|
|
|
this->m_task.lock()->interrupt();
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
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)));
|
|
|
|
|
|
|
|
return TaskHolder(s_tasks.back());
|
2022-01-09 21:27:59 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
void TaskManager::collectGarbage() {
|
|
|
|
std::erase_if(s_tasks, [](const auto &task) { return task->isFinished() && !task->hadException(); });
|
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() {
|
|
|
|
return s_tasks.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|