| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Resource/Common.h>
- #include <AnKi/Util/Thread.h>
- #include <AnKi/Util/List.h>
- namespace anki {
- // Forward
- class AsyncLoader;
- /// @addtogroup resource
- /// @{
- class AsyncLoaderTaskContext
- {
- public:
- /// Pause the async loader.
- Bool m_pause = false;
- /// Resubmit the same task at the end of the queue.
- Bool m_resubmitTask = false;
- };
- /// Interface for tasks for the AsyncLoader.
- class AsyncLoaderTask : public IntrusiveListEnabled<AsyncLoaderTask>
- {
- public:
- virtual ~AsyncLoaderTask()
- {
- }
- virtual ANKI_USE_RESULT Error operator()(AsyncLoaderTaskContext& ctx) = 0;
- };
- /// Asynchronous resource loader.
- class AsyncLoader
- {
- public:
- AsyncLoader();
- ~AsyncLoader();
- void init(const HeapAllocator<U8>& alloc);
- /// Submit a task.
- void submitTask(AsyncLoaderTask* task);
- /// Create a new asynchronous loading task.
- template<typename TTask, typename... TArgs>
- TTask* newTask(TArgs&&... args)
- {
- return m_alloc.template newInstance<TTask>(std::forward<TArgs>(args)...);
- }
- /// Create and submit a new asynchronous loading task.
- template<typename TTask, typename... TArgs>
- void submitNewTask(TArgs&&... args)
- {
- submitTask(newTask<TTask>(std::forward<TArgs>(args)...));
- }
- /// Pause the loader. This method will block the caller for the current async task to finish. The rest of the
- /// tasks in the queue will not be executed until resume is called.
- void pause();
- /// Resume the async loading.
- void resume();
- HeapAllocator<U8> getAllocator() const
- {
- return m_alloc;
- }
- /// Get the total number of completed tasks.
- U64 getCompletedTaskCount() const
- {
- return m_completedTaskCount.load();
- }
- private:
- HeapAllocator<U8> m_alloc;
- Thread m_thread;
- Barrier m_barrier = {2};
- Mutex m_mtx;
- ConditionVariable m_condVar;
- IntrusiveList<AsyncLoaderTask> m_taskQueue;
- Bool m_quit = false;
- Bool m_paused = false;
- Bool m_sync = false;
- Atomic<U64> m_completedTaskCount = {0};
- /// Thread callback
- static ANKI_USE_RESULT Error threadCallback(ThreadCallbackInfo& info);
- Error threadWorker();
- void stop();
- };
- /// @}
- } // end namespace anki
|