#pragma once #include #include #include #include #include #include #include #include #include namespace hex { class TaskHolder; class TaskManager; class Task { public: Task() = default; Task(std::string unlocalizedName, u64 maxValue, bool background, std::function function); Task(const Task&) = delete; Task(Task &&other) noexcept; ~Task(); void update(u64 value = 0); void setMaxValue(u64 value); [[nodiscard]] bool isBackgroundTask() const; [[nodiscard]] bool isFinished() const; [[nodiscard]] bool hadException() const; [[nodiscard]] bool wasInterrupted() const; void clearException(); [[nodiscard]] std::string getExceptionMessage() const; [[nodiscard]] const std::string &getUnlocalizedName(); [[nodiscard]] u64 getValue() const; [[nodiscard]] u64 getMaxValue() const; void interrupt(); void setInterruptCallback(std::function callback); private: void finish(); void interruption(); void exception(const char *message); private: mutable std::mutex m_mutex; std::string m_unlocalizedName; u64 m_currValue = 0, m_maxValue = 0; std::thread m_thread; std::function m_interruptCallback; bool m_shouldInterrupt = false; bool m_background = true; bool m_interrupted = false; bool m_finished = false; bool m_hadException = false; std::string m_exceptionMessage; struct TaskInterruptor { virtual ~TaskInterruptor() = default; }; friend class TaskHolder; friend class TaskManager; }; class TaskHolder { public: TaskHolder() = default; explicit TaskHolder(std::weak_ptr task) : m_task(std::move(task)) { } [[nodiscard]] bool isRunning() const; [[nodiscard]] bool hadException() const; [[nodiscard]] bool wasInterrupted() const; void interrupt(); private: std::weak_ptr m_task; }; class TaskManager { public: TaskManager() = delete; constexpr static auto NoProgress = 0; static TaskHolder createTask(std::string name, u64 maxValue, std::function function); static TaskHolder createBackgroundTask(std::string name, std::function function); static void collectGarbage(); static size_t getRunningTaskCount(); static std::list> &getRunningTasks(); static void doLater(const std::function &function); static void runDeferredCalls(); private: static std::mutex s_deferredCallsMutex; static std::list> s_tasks; static std::list> s_deferredCalls; }; }