/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #pragma once #include "Task.hpp" #include "Base/Logger.hpp" #include "Base/Wrapper.hpp" #include namespace OpenVulkano { class ITaskPool { protected: void ExecuteTask(const Ptr& task) { try { task->Execute(); } catch(const std::exception& e) { Logger::MANAGER->error("Failed to execute task! With exception: {}", e.what()); } catch(...) { Logger::MANAGER->error("Failed to execute task! With unknown throwable"); } } public: /*template void EmplaceTask(Args&&... args) { m_tasks.enqueue(std::make_shared((std::forward(args)...))); }*/ virtual void Enqueue(const Ptr& task) = 0; void Enqueue(const std::function& taskFunction) { Enqueue(std::make_shared(taskFunction)); } void operator +=(const std::function& taskFunction) { Enqueue(taskFunction); } }; class MainThreadTaskPool final : public ITaskPool { static MainThreadTaskPool* INSTANCE; moodycamel::ConcurrentQueue> m_tasks; public: MainThreadTaskPool() { if (!INSTANCE) INSTANCE = this; } ~MainThreadTaskPool() { if (INSTANCE == this) INSTANCE = nullptr; } static MainThreadTaskPool& GetInstance() { return *INSTANCE; } void Tick() { Ptr task; while (m_tasks.try_dequeue(task)) { ExecuteTask(task); } } void Enqueue(const Ptr& task) override { m_tasks.enqueue(task); } }; }