#pragma once #include #include #include namespace UI { class UIBase; } // namespace UI class LvglResourceManager { friend UI::UIBase; public: static LvglResourceManager &GetInstance() { static LvglResourceManager mInstance; return mInstance; } [[nodiscard]] std::scoped_lock scopeLock() { return std::scoped_lock(mLvglMutex); } void AttemptNow(std::function aLvglModifierFunction) { // Attempt to take mutex and preform op if you can't then queue up. if (mLvglMutex.try_lock()) { aLvglModifierFunction(); mLvglMutex.unlock(); } else { QueueForLater(aLvglModifierFunction); } } void QueueForLater(std::function aLvglModifierFunction) { mLvglTasks.push(std::move(aLvglModifierFunction)); } protected: LvglResourceManager(){}; void HandleQueuedTasks() { auto lock = scopeLock(); while (!mLvglTasks.empty()) { mLvglTasks.front()(); mLvglTasks.pop(); } } std::queue> mLvglTasks; std::recursive_mutex mLvglMutex; };