AsyncLoader.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Resource/AsyncLoader.h>
  6. #include <AnKi/Core/StatsSet.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/Tracer.h>
  9. namespace anki {
  10. ANKI_SVAR(AsyncTasksInFlight, StatCategory::kMisc, "Async loader tasks", StatFlag::kNone)
  11. AsyncLoader::AsyncLoader()
  12. : m_thread("AsyncLoad")
  13. {
  14. m_thread.start(this, threadCallback);
  15. }
  16. AsyncLoader::~AsyncLoader()
  17. {
  18. stop();
  19. if(!m_taskQueue.isEmpty())
  20. {
  21. ANKI_RESOURCE_LOGW("Stoping loading thread while there is work to do");
  22. while(!m_taskQueue.isEmpty())
  23. {
  24. AsyncLoaderTask* task = &m_taskQueue.getFront();
  25. m_taskQueue.popFront();
  26. deleteInstance(ResourceMemoryPool::getSingleton(), task);
  27. }
  28. }
  29. }
  30. void AsyncLoader::stop()
  31. {
  32. {
  33. LockGuard<Mutex> lock(m_mtx);
  34. m_quit = true;
  35. m_condVar.notifyOne();
  36. }
  37. [[maybe_unused]] Error err = m_thread.join();
  38. }
  39. Error AsyncLoader::threadCallback(ThreadCallbackInfo& info)
  40. {
  41. AsyncLoader& self = *reinterpret_cast<AsyncLoader*>(info.m_userData);
  42. return self.threadWorker();
  43. }
  44. Error AsyncLoader::threadWorker()
  45. {
  46. Error err = Error::kNone;
  47. while(!err)
  48. {
  49. AsyncLoaderTask* task = nullptr;
  50. Bool quit = false;
  51. {
  52. // Wait for something
  53. LockGuard<Mutex> lock(m_mtx);
  54. while(m_taskQueue.isEmpty() && !m_quit)
  55. {
  56. m_condVar.wait(m_mtx);
  57. }
  58. // Do some work
  59. if(m_quit)
  60. {
  61. quit = true;
  62. }
  63. else
  64. {
  65. task = &m_taskQueue.getFront();
  66. m_taskQueue.popFront();
  67. }
  68. }
  69. if(quit)
  70. {
  71. break;
  72. }
  73. else
  74. {
  75. // Exec the task
  76. ANKI_ASSERT(task);
  77. AsyncLoaderTaskContext ctx;
  78. {
  79. ANKI_TRACE_SCOPED_EVENT(RsrcAsyncTask);
  80. err = (*task)(ctx);
  81. g_svarAsyncTasksInFlight.decrement(1u);
  82. }
  83. if(!err)
  84. {
  85. m_tasksInFlightCount.fetchSub(1);
  86. }
  87. else
  88. {
  89. ANKI_RESOURCE_LOGE("Async loader task failed");
  90. }
  91. // Do other stuff
  92. if(ctx.m_resubmitTask)
  93. {
  94. LockGuard<Mutex> lock(m_mtx);
  95. m_taskQueue.pushBack(task);
  96. }
  97. else
  98. {
  99. // Delete the task
  100. deleteInstance(ResourceMemoryPool::getSingleton(), task);
  101. }
  102. }
  103. }
  104. return err;
  105. }
  106. void AsyncLoader::submitTask(AsyncLoaderTask* task)
  107. {
  108. ANKI_ASSERT(task);
  109. m_tasksInFlightCount.fetchAdd(1);
  110. g_svarAsyncTasksInFlight.increment(1);
  111. LockGuard<Mutex> lock(m_mtx);
  112. m_taskQueue.pushBack(task);
  113. m_condVar.notifyOne();
  114. }
  115. } // end namespace anki