AsyncLoader.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (C) 2009-2021, 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/Util/Logger.h>
  7. #include <AnKi/Util/Tracer.h>
  8. namespace anki {
  9. AsyncLoader::AsyncLoader()
  10. : m_thread("anki_asyload")
  11. {
  12. }
  13. AsyncLoader::~AsyncLoader()
  14. {
  15. stop();
  16. if(!m_taskQueue.isEmpty())
  17. {
  18. ANKI_RESOURCE_LOGW("Stoping loading thread while there is work to do");
  19. while(!m_taskQueue.isEmpty())
  20. {
  21. AsyncLoaderTask* task = &m_taskQueue.getFront();
  22. m_taskQueue.popFront();
  23. m_alloc.deleteInstance(task);
  24. }
  25. }
  26. }
  27. void AsyncLoader::init(const HeapAllocator<U8>& alloc)
  28. {
  29. m_alloc = alloc;
  30. m_thread.start(this, threadCallback);
  31. }
  32. void AsyncLoader::stop()
  33. {
  34. {
  35. LockGuard<Mutex> lock(m_mtx);
  36. m_quit = true;
  37. m_condVar.notifyOne();
  38. }
  39. Error err = m_thread.join();
  40. (void)err;
  41. }
  42. void AsyncLoader::pause()
  43. {
  44. {
  45. LockGuard<Mutex> lock(m_mtx);
  46. m_paused = true;
  47. m_sync = true;
  48. m_condVar.notifyOne();
  49. }
  50. m_barrier.wait();
  51. }
  52. void AsyncLoader::resume()
  53. {
  54. LockGuard<Mutex> lock(m_mtx);
  55. m_paused = false;
  56. m_condVar.notifyOne();
  57. }
  58. Error AsyncLoader::threadCallback(ThreadCallbackInfo& info)
  59. {
  60. AsyncLoader& self = *reinterpret_cast<AsyncLoader*>(info.m_userData);
  61. return self.threadWorker();
  62. }
  63. Error AsyncLoader::threadWorker()
  64. {
  65. Error err = Error::NONE;
  66. while(!err)
  67. {
  68. AsyncLoaderTask* task = nullptr;
  69. Bool quit = false;
  70. Bool sync = false;
  71. {
  72. // Wait for something
  73. LockGuard<Mutex> lock(m_mtx);
  74. while((m_taskQueue.isEmpty() || m_paused) && !m_quit && !m_sync)
  75. {
  76. m_condVar.wait(m_mtx);
  77. }
  78. // Do some work
  79. if(m_quit)
  80. {
  81. quit = true;
  82. }
  83. else if(m_sync)
  84. {
  85. sync = true;
  86. m_sync = false;
  87. }
  88. else
  89. {
  90. task = &m_taskQueue.getFront();
  91. m_taskQueue.popFront();
  92. }
  93. }
  94. if(quit)
  95. {
  96. break;
  97. }
  98. else if(sync)
  99. {
  100. m_barrier.wait();
  101. }
  102. else
  103. {
  104. // Exec the task
  105. ANKI_ASSERT(task);
  106. AsyncLoaderTaskContext ctx;
  107. {
  108. ANKI_TRACE_SCOPED_EVENT(RSRC_ASYNC_TASK);
  109. err = (*task)(ctx);
  110. }
  111. if(!err)
  112. {
  113. m_completedTaskCount.fetchAdd(1);
  114. }
  115. else
  116. {
  117. ANKI_RESOURCE_LOGE("Async loader task failed");
  118. }
  119. // Do other stuff
  120. if(ctx.m_resubmitTask)
  121. {
  122. LockGuard<Mutex> lock(m_mtx);
  123. m_taskQueue.pushBack(task);
  124. }
  125. else
  126. {
  127. // Delete the task
  128. m_alloc.deleteInstance(task);
  129. }
  130. if(ctx.m_pause)
  131. {
  132. LockGuard<Mutex> lock(m_mtx);
  133. m_paused = true;
  134. }
  135. }
  136. }
  137. return err;
  138. }
  139. void AsyncLoader::submitTask(AsyncLoaderTask* task)
  140. {
  141. ANKI_ASSERT(task);
  142. // Append task to the list
  143. LockGuard<Mutex> lock(m_mtx);
  144. m_taskQueue.pushBack(task);
  145. if(!m_paused)
  146. {
  147. // Wake up the thread if it's not paused
  148. m_condVar.notifyOne();
  149. }
  150. }
  151. } // end namespace anki