1
0
mirror of synced 2024-12-24 05:24:52 +01:00
ImHex/lib/libimhex/source/api/task_manager.cpp

487 lines
15 KiB
C++
Raw Normal View History

#include <hex/api/task_manager.hpp>
#include <hex/api/localization_manager.hpp>
#include <hex/helpers/logger.hpp>
2022-07-29 18:49:43 +02:00
#include <algorithm>
#include <ranges>
2022-07-29 18:49:43 +02:00
#include <jthread.hpp>
2023-06-21 09:29:57 +02:00
#if defined(OS_WINDOWS)
#include <windows.h>
#include <processthreadsapi.h>
2024-01-30 16:32:48 +01:00
#include <hex/helpers/utils.hpp>
2023-06-21 09:29:57 +02:00
#else
#include <pthread.h>
#endif
namespace {
struct SourceLocationWrapper {
std::source_location location;
bool operator==(const SourceLocationWrapper &other) const {
return location.file_name() == other.location.file_name() &&
location.function_name() == other.location.function_name() &&
location.column() == other.location.column() &&
location.line() == other.location.line();
}
};
}
template<>
struct std::hash<SourceLocationWrapper> {
std::size_t operator()(const SourceLocationWrapper& s) const noexcept {
auto h1 = std::hash<std::string>{}(s.location.file_name());
auto h2 = std::hash<std::string>{}(s.location.function_name());
auto h3 = std::hash<u32>{}(s.location.column());
auto h4 = std::hash<u32>{}(s.location.line());
return (h1 << 0) ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
namespace hex {
namespace {
std::recursive_mutex s_deferredCallsMutex, s_tasksFinishedMutex;
std::list<std::shared_ptr<Task>> s_tasks, s_taskQueue;
std::list<std::function<void()>> s_deferredCalls;
std::unordered_map<SourceLocationWrapper, std::function<void()>> s_onceDeferredCalls;
std::list<std::function<void()>> s_tasksFinishedCallbacks;
std::mutex s_queueMutex;
std::condition_variable s_jobCondVar;
std::vector<std::jthread> s_workers;
thread_local std::array<char, 256> s_currentThreadName;
thread_local Task* s_currentTask = nullptr;
}
Task::Task(const UnlocalizedString &unlocalizedName, u64 maxValue, bool background, std::function<void(Task &)> function)
: m_unlocalizedName(std::move(unlocalizedName)), m_maxValue(maxValue), m_function(std::move(function)), m_background(background) { }
Task::Task(hex::Task &&other) noexcept {
{
2023-12-19 13:10:25 +01:00
std::scoped_lock thisLock(m_mutex);
std::scoped_lock otherLock(other.m_mutex);
2023-12-19 13:10:25 +01:00
m_function = std::move(other.m_function);
m_unlocalizedName = std::move(other.m_unlocalizedName);
}
2023-12-19 13:10:25 +01:00
m_maxValue = u64(other.m_maxValue);
m_currValue = u64(other.m_currValue);
2023-12-19 13:10:25 +01:00
m_finished = bool(other.m_finished);
m_hadException = bool(other.m_hadException);
m_interrupted = bool(other.m_interrupted);
m_shouldInterrupt = bool(other.m_shouldInterrupt);
}
Task::~Task() {
if (!this->isFinished())
this->interrupt();
}
void Task::update(u64 value) {
2023-12-06 11:04:35 +01:00
// Update the current progress value of the task
2023-12-19 13:10:25 +01:00
m_currValue.store(value, std::memory_order_relaxed);
2022-07-29 18:49:43 +02:00
2023-12-06 11:04:35 +01:00
// Check if the task has been interrupted by the main thread and if yes,
// throw an exception that is generally not caught by the task
2023-12-19 13:10:25 +01:00
if (m_shouldInterrupt.load(std::memory_order_relaxed)) [[unlikely]]
throw TaskInterruptor();
}
void Task::update() const {
if (m_shouldInterrupt.load(std::memory_order_relaxed)) [[unlikely]]
throw TaskInterruptor();
}
void Task::increment() {
m_currValue.fetch_add(1, std::memory_order_relaxed);
if (m_shouldInterrupt.load(std::memory_order_relaxed)) [[unlikely]]
throw TaskInterruptor();
}
void Task::setMaxValue(u64 value) {
2023-12-19 13:10:25 +01:00
m_maxValue = value;
}
void Task::interrupt() {
2023-12-19 13:10:25 +01:00
m_shouldInterrupt = true;
2023-12-06 11:04:35 +01:00
// Call the interrupt callback on the current thread if one is set
2023-12-19 13:10:25 +01:00
if (m_interruptCallback)
m_interruptCallback();
}
void Task::setInterruptCallback(std::function<void()> callback) {
2023-12-19 13:10:25 +01:00
m_interruptCallback = std::move(callback);
}
bool Task::isBackgroundTask() const {
2023-12-19 13:10:25 +01:00
return m_background;
}
bool Task::isFinished() const {
2023-12-19 13:10:25 +01:00
return m_finished;
}
bool Task::hadException() const {
2023-12-19 13:10:25 +01:00
return m_hadException;
}
bool Task::shouldInterrupt() const {
2023-12-19 13:10:25 +01:00
return m_shouldInterrupt;
}
bool Task::wasInterrupted() const {
2023-12-19 13:10:25 +01:00
return m_interrupted;
}
void Task::clearException() {
2023-12-19 13:10:25 +01:00
m_hadException = false;
}
std::string Task::getExceptionMessage() const {
2023-12-19 13:10:25 +01:00
std::scoped_lock lock(m_mutex);
2023-12-19 13:10:25 +01:00
return m_exceptionMessage;
}
const UnlocalizedString &Task::getUnlocalizedName() {
return m_unlocalizedName;
}
u64 Task::getValue() const {
2023-12-19 13:10:25 +01:00
return m_currValue;
}
u64 Task::getMaxValue() const {
2023-12-19 13:10:25 +01:00
return m_maxValue;
2022-07-29 18:49:43 +02:00
}
void Task::finish() {
2023-12-19 13:10:25 +01:00
m_finished = true;
}
void Task::interruption() {
2023-12-19 13:10:25 +01:00
m_interrupted = true;
}
void Task::exception(const char *message) {
2023-12-19 13:10:25 +01:00
std::scoped_lock lock(m_mutex);
2023-12-06 11:04:35 +01:00
// Store information about the caught exception
2023-12-19 13:10:25 +01:00
m_exceptionMessage = message;
m_hadException = true;
}
bool TaskHolder::isRunning() const {
const auto &task = m_task.lock();
2023-12-06 11:04:35 +01:00
if (!task)
return false;
return !task->isFinished();
}
bool TaskHolder::hadException() const {
const auto &task = m_task.lock();
2023-12-06 11:04:35 +01:00
if (!task)
return false;
return !task->hadException();
}
bool TaskHolder::shouldInterrupt() const {
const auto &task = m_task.lock();
2023-12-06 11:04:35 +01:00
if (!task)
return false;
return !task->shouldInterrupt();
}
bool TaskHolder::wasInterrupted() const {
const auto &task = m_task.lock();
2023-12-06 11:04:35 +01:00
if (!task)
return false;
return !task->wasInterrupted();
}
2023-11-10 20:47:08 +01:00
void TaskHolder::interrupt() const {
const auto &task = m_task.lock();
2023-12-06 11:04:35 +01:00
if (!task)
return;
task->interrupt();
}
u32 TaskHolder::getProgress() const {
const auto &task = m_task.lock();
2023-12-06 11:04:35 +01:00
if (!task)
return 0;
2023-12-06 11:04:35 +01:00
// If the max value is 0, the task has no progress
if (task->getMaxValue() == 0)
return 0;
2023-12-06 11:04:35 +01:00
// Calculate the progress of the task from 0 to 100
2023-11-10 20:47:08 +01:00
return u32((task->getValue() * 100) / task->getMaxValue());
}
void TaskManager::init() {
2023-06-06 11:29:58 +02:00
const auto threadCount = std::thread::hardware_concurrency();
log::debug("Initializing task manager thread pool with {} workers.", threadCount);
2023-12-06 11:04:35 +01:00
// Create worker threads
for (u32 i = 0; i < threadCount; i++) {
s_workers.emplace_back([](const std::stop_token &stopToken) {
while (true) {
std::shared_ptr<Task> task;
// Set the thread name to "Idle Task" while waiting for a task
2024-01-09 10:39:06 +01:00
TaskManager::setCurrentThreadName("Idle Task");
{
// Wait for a task to be added to the queue
std::unique_lock lock(s_queueMutex);
s_jobCondVar.wait(lock, [&] {
return !s_taskQueue.empty() || stopToken.stop_requested();
});
// Check if the thread should exit
if (stopToken.stop_requested())
break;
// Grab the next task from the queue
task = std::move(s_taskQueue.front());
s_taskQueue.pop_front();
s_currentTask = task.get();
}
try {
// Set the thread name to the name of the task
TaskManager::setCurrentThreadName(Lang(task->m_unlocalizedName));
// Execute the task
task->m_function(*task);
log::debug("Task '{}' finished", task->m_unlocalizedName.get());
} catch (const Task::TaskInterruptor &) {
// Handle the task being interrupted by user request
task->interruption();
} catch (const std::exception &e) {
log::error("Exception in task '{}': {}", task->m_unlocalizedName.get(), e.what());
// Handle the task throwing an uncaught exception
task->exception(e.what());
} catch (...) {
log::error("Exception in task '{}'", task->m_unlocalizedName.get());
// Handle the task throwing an uncaught exception of unknown type
task->exception("Unknown Exception");
}
s_currentTask = nullptr;
task->finish();
}
});
}
}
void TaskManager::exit() {
2023-12-06 11:04:35 +01:00
// Interrupt all tasks
for (const auto &task : s_tasks) {
task->interrupt();
2023-12-06 11:04:35 +01:00
}
2023-12-06 11:04:35 +01:00
// Ask worker threads to exit after finishing their task
for (auto &thread : s_workers)
thread.request_stop();
2023-12-06 11:04:35 +01:00
// Wake up all the idle worker threads so they can exit
s_jobCondVar.notify_all();
2023-12-06 11:04:35 +01:00
// Wait for all worker threads to exit
s_workers.clear();
2023-12-06 11:04:35 +01:00
s_tasks.clear();
s_taskQueue.clear();
s_deferredCalls.clear();
s_onceDeferredCalls.clear();
s_tasksFinishedCallbacks.clear();
}
TaskHolder TaskManager::createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, bool background, std::function<void(Task&)> function) {
2023-12-06 11:04:35 +01:00
std::scoped_lock lock(s_queueMutex);
// Construct new task
auto task = std::make_shared<Task>(std::move(unlocalizedName), maxValue, background, std::move(function));
2023-12-06 11:04:35 +01:00
s_tasks.emplace_back(task);
2023-12-06 11:04:35 +01:00
// Add task to the queue for the worker to pick up
s_taskQueue.emplace_back(std::move(task));
s_jobCondVar.notify_one();
return TaskHolder(s_tasks.back());
}
TaskHolder TaskManager::createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, std::function<void(Task &)> function) {
log::debug("Creating task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), maxValue, false, std::move(function));
2023-12-06 11:04:35 +01:00
}
TaskHolder TaskManager::createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void(Task &)> function) {
log::debug("Creating background task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), 0, true, std::move(function));
}
void TaskManager::collectGarbage() {
{
2023-12-06 11:04:35 +01:00
std::scoped_lock lock(s_queueMutex);
std::erase_if(s_tasks, [](const auto &task) {
return task->isFinished() && !task->hadException();
});
}
if (s_tasks.empty()) {
2023-12-06 11:04:35 +01:00
std::scoped_lock lock(s_deferredCallsMutex);
for (auto &call : s_tasksFinishedCallbacks)
call();
s_tasksFinishedCallbacks.clear();
}
2023-08-26 12:54:52 +02:00
}
Task& TaskManager::getCurrentTask() {
return *s_currentTask;
}
const std::list<std::shared_ptr<Task>>& TaskManager::getRunningTasks() {
return s_tasks;
}
size_t TaskManager::getRunningTaskCount() {
2023-12-06 11:04:35 +01:00
std::scoped_lock lock(s_queueMutex);
return std::ranges::count_if(s_tasks, [](const auto &task){
return !task->isBackgroundTask();
});
}
size_t TaskManager::getRunningBackgroundTaskCount() {
2023-12-06 11:04:35 +01:00
std::scoped_lock lock(s_queueMutex);
return std::ranges::count_if(s_tasks, [](const auto &task){
return task->isBackgroundTask();
});
}
void TaskManager::doLater(const std::function<void()> &function) {
std::scoped_lock lock(s_deferredCallsMutex);
s_deferredCalls.push_back(function);
}
void TaskManager::doLaterOnce(const std::function<void()> &function, std::source_location location) {
std::scoped_lock lock(s_deferredCallsMutex);
s_onceDeferredCalls[SourceLocationWrapper{ location }] = function;
}
void TaskManager::runDeferredCalls() {
std::scoped_lock lock(s_deferredCallsMutex);
while (!s_deferredCalls.empty()) {
auto callback = s_deferredCalls.front();
s_deferredCalls.pop_front();
callback();
}
while (!s_onceDeferredCalls.empty()) {
auto node = s_onceDeferredCalls.extract(s_onceDeferredCalls.begin());
node.mapped()();
}
}
void TaskManager::runWhenTasksFinished(const std::function<void()> &function) {
std::scoped_lock lock(s_tasksFinishedMutex);
for (const auto &task : s_tasks) {
task->interrupt();
}
s_tasksFinishedCallbacks.push_back(function);
}
2024-01-09 10:39:06 +01:00
void TaskManager::setCurrentThreadName(const std::string &name) {
std::ranges::fill(s_currentThreadName, '\0');
std::ranges::copy(name | std::views::take(255), s_currentThreadName.begin());
2024-01-09 10:39:06 +01:00
#if defined(OS_WINDOWS)
using SetThreadDescriptionFunc = HRESULT(WINAPI*)(HANDLE hThread, PCWSTR lpThreadDescription);
static auto setThreadDescription = reinterpret_cast<SetThreadDescriptionFunc>(
reinterpret_cast<uintptr_t>(
::GetProcAddress(
::GetModuleHandleW(L"Kernel32.dll"),
"SetThreadDescription"
)
)
);
if (setThreadDescription != nullptr) {
const auto longName = hex::utf8ToUtf16(name);
setThreadDescription(::GetCurrentThread(), longName.c_str());
} else {
struct THREADNAME_INFO {
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
};
THREADNAME_INFO info = { };
info.dwType = 0x1000;
info.szName = name.c_str();
info.dwThreadID = ::GetCurrentThreadId();
info.dwFlags = 0;
constexpr static DWORD MS_VC_EXCEPTION = 0x406D1388;
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
}
2024-01-09 10:39:06 +01:00
#elif defined(OS_LINUX)
pthread_setname_np(pthread_self(), name.c_str());
#elif defined(OS_WEB)
hex::unused(name);
#elif defined(OS_MACOS)
pthread_setname_np(name.c_str());
#endif
}
std::string TaskManager::getCurrentThreadName() {
return s_currentThreadName.data();
2024-01-09 10:39:06 +01:00
}
}