2021-12-16 23:48:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <thread>
|
|
|
|
#include <functional>
|
|
|
|
#include <cstdint>
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <mutex>
|
2022-08-17 16:15:36 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <list>
|
2021-12-16 23:48:52 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
class TaskHolder;
|
|
|
|
class TaskManager;
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
class Task {
|
|
|
|
public:
|
2022-07-29 18:49:43 +02:00
|
|
|
Task() = default;
|
2022-08-17 16:15:36 +02:00
|
|
|
Task(std::string unlocalizedName, u64 maxValue, std::function<void(Task &)> function);
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
Task(const Task&) = delete;
|
2022-07-29 18:49:43 +02:00
|
|
|
Task(Task &&other) noexcept;
|
2022-08-17 16:15:36 +02:00
|
|
|
~Task();
|
|
|
|
|
|
|
|
void update(u64 value = 0);
|
|
|
|
void setMaxValue(u64 value);
|
|
|
|
|
|
|
|
[[nodiscard]] bool isFinished() const;
|
|
|
|
[[nodiscard]] bool hadException() const;
|
|
|
|
[[nodiscard]] bool wasInterrupted() const;
|
|
|
|
void clearException();
|
2022-09-03 23:56:57 +02:00
|
|
|
[[nodiscard]] std::string getExceptionMessage() const;
|
2022-07-29 18:49:43 +02:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
[[nodiscard]] const std::string &getUnlocalizedName();
|
|
|
|
[[nodiscard]] u64 getValue() const;
|
|
|
|
[[nodiscard]] u64 getMaxValue() const;
|
|
|
|
|
|
|
|
void interrupt();
|
|
|
|
|
2022-09-13 16:05:41 +02:00
|
|
|
void setInterruptCallback(std::function<void()> callback);
|
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
private:
|
2021-12-16 23:48:52 +01:00
|
|
|
void finish();
|
2022-08-17 16:15:36 +02:00
|
|
|
void interruption();
|
2022-09-03 23:56:57 +02:00
|
|
|
void exception(const char *message);
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
|
|
|
|
std::string m_unlocalizedName;
|
|
|
|
u64 m_currValue, m_maxValue;
|
|
|
|
std::thread m_thread;
|
2022-09-13 16:05:41 +02:00
|
|
|
std::function<void()> m_interruptCallback;
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
bool m_shouldInterrupt = false;
|
|
|
|
|
|
|
|
bool m_interrupted = false;
|
|
|
|
bool m_finished = false;
|
|
|
|
bool m_hadException = false;
|
2022-09-03 23:56:57 +02:00
|
|
|
std::string m_exceptionMessage;
|
2022-08-17 16:15:36 +02:00
|
|
|
|
|
|
|
struct TaskInterruptor { virtual ~TaskInterruptor() = default; };
|
|
|
|
|
|
|
|
friend class TaskHolder;
|
|
|
|
friend class TaskManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TaskHolder {
|
|
|
|
public:
|
|
|
|
TaskHolder() = default;
|
|
|
|
explicit TaskHolder(std::weak_ptr<Task> task) : m_task(std::move(task)) { }
|
|
|
|
|
|
|
|
[[nodiscard]] bool isRunning() const;
|
|
|
|
[[nodiscard]] bool hadException() const;
|
|
|
|
[[nodiscard]] bool wasInterrupted() const;
|
|
|
|
|
|
|
|
void interrupt();
|
|
|
|
private:
|
|
|
|
std::weak_ptr<Task> m_task;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TaskManager {
|
|
|
|
public:
|
|
|
|
TaskManager() = delete;
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
constexpr static auto NoProgress = 0;
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
static TaskHolder createTask(std::string name, u64 maxValue, std::function<void(Task &)> function);
|
|
|
|
static void collectGarbage();
|
2022-01-09 21:27:59 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
static size_t getRunningTaskCount();
|
2022-08-17 16:15:36 +02:00
|
|
|
static std::list<std::shared_ptr<Task>> &getRunningTasks();
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
static void doLater(const std::function<void()> &function);
|
|
|
|
static void runDeferredCalls();
|
2021-12-16 23:48:52 +01:00
|
|
|
private:
|
2022-08-17 16:15:36 +02:00
|
|
|
static std::mutex s_deferredCallsMutex;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2022-08-17 16:15:36 +02:00
|
|
|
static std::list<std::shared_ptr<Task>> s_tasks;
|
|
|
|
static std::list<std::function<void()>> s_deferredCalls;
|
2021-12-16 23:48:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|