#pragma once #include #include #include #include #include #include #include #include #include class LuaRunner { public: using CompletionCallback = std::function; struct RunResult { bool started = false; bool singleInstance = false; }; struct TaskStatus { bool singleInstance = false; bool running = false; }; RunResult run(const std::string& taskId, const std::filesystem::path& scriptPath, int timeoutSeconds, CompletionCallback onComplete); TaskStatus status(const std::string& taskId, const std::filesystem::path& scriptPath) const; void cancel(const std::string& taskId) const; void shutdown() const; ~LuaRunner(); private: struct LuaTask { std::shared_ptr cancel; std::shared_ptr executionFinished; std::shared_ptr callbackFinished; bool singleInstance = false; std::jthread worker; LuaTask(std::shared_ptr cancelFlag, std::shared_ptr executionFinishedFlag, std::shared_ptr callbackFinishedFlag, bool isSingleInstance) : cancel(std::move(cancelFlag)), executionFinished(std::move(executionFinishedFlag)), callbackFinished(std::move(callbackFinishedFlag)), singleInstance(isSingleInstance) {} }; struct State { std::mutex tasksMutex; std::unordered_map> tasks; }; std::shared_ptr m_state = std::make_shared(); };