build: Restructured entire custom plugin system (#1469)

This commit is contained in:
Nik
2023-12-22 23:39:38 +01:00
committed by GitHub
parent 538e79183c
commit 84bfd10416
27 changed files with 258 additions and 153 deletions

View File

@@ -8,7 +8,6 @@
#include <hex/data_processor/node.hpp>
#include <filesystem>
#include <thread>
#include <jthread.hpp>
#if defined(OS_WEB)
@@ -1000,6 +999,11 @@ namespace hex {
namespace impl {
struct Service {
std::string name;
std::jthread thread;
};
std::vector<Service> &getServices() {
static std::vector<Service> services;
@@ -1007,14 +1011,18 @@ namespace hex {
}
void stopServices() {
for (auto &service : getServices()) {
auto &services = getServices();
for (auto &service : services) {
service.thread.request_stop();
}
for (auto &service : getServices()) {
for (auto &service : services) {
if (service.thread.joinable())
service.thread.join();
}
services.clear();
}
}

View File

@@ -5,6 +5,8 @@
#include <algorithm>
#include <jthread.hpp>
#if defined(OS_WINDOWS)
#include <windows.h>
#include <processthreadsapi.h>
@@ -227,8 +229,57 @@ namespace hex {
log::debug("Initializing task manager thread pool with {} workers.", threadCount);
// Create worker threads
for (u32 i = 0; i < threadCount; i++)
s_workers.emplace_back(TaskManager::runner);
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
setThreadName("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();
}
try {
// Set the thread name to the name of the task
setThreadName(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");
}
task->finish();
}
});
}
}
void TaskManager::exit() {
@@ -251,56 +302,6 @@ namespace hex {
s_taskQueue.clear();
}
void TaskManager::runner(const std::stop_token &stopToken) {
while (true) {
std::shared_ptr<Task> task;
// Set the thread name to "Idle Task" while waiting for a task
setThreadName("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();
}
try {
// Set the thread name to the name of the task
setThreadName(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");
}
task->finish();
}
}
TaskHolder TaskManager::createTask(std::string name, u64 maxValue, bool background, std::function<void(Task&)> function) {
std::scoped_lock lock(s_queueMutex);