AsyncLoader.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <Tests/Framework/Framework.h>
  6. #include <AnKi/Resource/AsyncLoader.h>
  7. #include <AnKi/Util/HighRezTimer.h>
  8. #include <AnKi/Util/Atomic.h>
  9. #include <AnKi/Util/Functions.h>
  10. using namespace anki;
  11. namespace {
  12. #if 0
  13. class Task : public AsyncLoaderTask
  14. {
  15. public:
  16. F32 m_sleepTime = 0.0;
  17. Barrier* m_barrier = nullptr;
  18. Atomic<U32>* m_count = nullptr;
  19. I32 m_id = -1;
  20. Bool m_pause;
  21. Bool m_resubmit;
  22. Task(F32 time, Barrier* barrier, Atomic<U32>* count, I32 id = -1, Bool pause = false, Bool resubmit = false)
  23. : m_sleepTime(time)
  24. , m_barrier(barrier)
  25. , m_count(count)
  26. , m_id(id)
  27. , m_pause(pause)
  28. , m_resubmit(resubmit)
  29. {
  30. }
  31. Error operator()(AsyncLoaderTaskContext& ctx)
  32. {
  33. if(m_count)
  34. {
  35. auto x = m_count->fetchAdd(1);
  36. if(m_id >= 0)
  37. {
  38. if(m_id != static_cast<I32>(x))
  39. {
  40. ANKI_LOGE("Wrong excecution order");
  41. return Error::kFunctionFailed;
  42. }
  43. }
  44. }
  45. if(m_sleepTime != 0.0)
  46. {
  47. HighRezTimer::sleep(m_sleepTime);
  48. }
  49. if(m_barrier)
  50. {
  51. m_barrier->wait();
  52. }
  53. ctx.m_pause = m_pause;
  54. ctx.m_resubmitTask = m_resubmit;
  55. m_resubmit = false;
  56. return Error::kNone;
  57. }
  58. };
  59. class MemTask : public AsyncLoaderTask
  60. {
  61. public:
  62. HeapMemoryPool* m_pool;
  63. Barrier* m_barrier = nullptr;
  64. MemTask(HeapMemoryPool* pool, Barrier* barrier)
  65. : m_pool(pool)
  66. , m_barrier(barrier)
  67. {
  68. }
  69. Error operator()([[maybe_unused]] AsyncLoaderTaskContext& ctx)
  70. {
  71. void* mem = m_pool->allocate(10, 16);
  72. if(!mem)
  73. return Error::kFunctionFailed;
  74. HighRezTimer::sleep(0.1);
  75. m_pool->free(mem);
  76. if(m_barrier)
  77. {
  78. m_barrier->wait();
  79. }
  80. return Error::kNone;
  81. }
  82. };
  83. } // namespace
  84. ANKI_TEST(Resource, AsyncLoader)
  85. {
  86. HeapMemoryPool pool(allocAligned, nullptr);
  87. // Simple create destroy
  88. {
  89. AsyncLoader a;
  90. }
  91. // Simple task that will finish
  92. {
  93. AsyncLoader a;
  94. Barrier barrier(2);
  95. a.submitNewTask<Task>(0.0f, &barrier, nullptr);
  96. barrier.wait();
  97. }
  98. // Many tasks that will finish
  99. {
  100. AsyncLoader a;
  101. Barrier barrier(2);
  102. Atomic<U32> counter = {0};
  103. const U COUNT = 100;
  104. for(U i = 0; i < COUNT; i++)
  105. {
  106. Barrier* pbarrier = nullptr;
  107. if(i == COUNT - 1)
  108. {
  109. pbarrier = &barrier;
  110. }
  111. a.submitNewTask<Task>(0.01f, pbarrier, &counter);
  112. }
  113. barrier.wait();
  114. ANKI_TEST_EXPECT_EQ(counter.load(), COUNT);
  115. }
  116. // Many tasks that will _not_ finish
  117. {
  118. AsyncLoader a;
  119. for(U i = 0; i < 100; i++)
  120. {
  121. a.submitNewTask<Task>(0.0f, nullptr, nullptr);
  122. }
  123. }
  124. // Tasks that allocate
  125. {
  126. AsyncLoader a;
  127. Barrier barrier(2);
  128. for(U i = 0; i < 10; i++)
  129. {
  130. Barrier* pbarrier = nullptr;
  131. if(i == 9)
  132. {
  133. pbarrier = &barrier;
  134. }
  135. a.submitNewTask<MemTask>(&pool, pbarrier);
  136. }
  137. barrier.wait();
  138. }
  139. // Tasks that allocate and never finished
  140. {
  141. AsyncLoader a;
  142. for(U i = 0; i < 10; i++)
  143. {
  144. a.submitNewTask<MemTask>(&pool, nullptr);
  145. }
  146. }
  147. // Pause/resume
  148. {
  149. AsyncLoader a;
  150. Atomic<U32> counter(0);
  151. Barrier barrier(2);
  152. // Check if the pause will sync
  153. a.submitNewTask<Task>(0.5f, nullptr, &counter, 0);
  154. HighRezTimer::sleep(0.25); // Wait for the thread to pick the task...
  155. a.pause(); /// ...and then sync
  156. ANKI_TEST_EXPECT_EQ(counter.load(), 1);
  157. // Test resume
  158. a.submitNewTask<Task>(0.1f, nullptr, &counter, 1);
  159. HighRezTimer::sleep(1.0);
  160. ANKI_TEST_EXPECT_EQ(counter.load(), 1);
  161. a.resume();
  162. // Sync
  163. a.submitNewTask<Task>(0.1f, &barrier, &counter, 2);
  164. barrier.wait();
  165. ANKI_TEST_EXPECT_EQ(counter.load(), 3);
  166. }
  167. // Pause/resume
  168. {
  169. AsyncLoader a;
  170. Atomic<U32> counter(0);
  171. Barrier barrier(2);
  172. // Check task resubmit
  173. a.submitNewTask<Task>(0.0f, &barrier, &counter, -1, false, true);
  174. barrier.wait();
  175. barrier.wait();
  176. ANKI_TEST_EXPECT_EQ(counter.load(), 2);
  177. // Check task pause
  178. a.submitNewTask<Task>(0.0f, nullptr, &counter, -1, true, false);
  179. a.submitNewTask<Task>(0.0f, nullptr, &counter, -1, false, false);
  180. HighRezTimer::sleep(1.0);
  181. ANKI_TEST_EXPECT_EQ(counter.load(), 3);
  182. a.resume();
  183. HighRezTimer::sleep(1.0);
  184. ANKI_TEST_EXPECT_EQ(counter.load(), 4);
  185. // Check both
  186. counter.setNonAtomically(0);
  187. a.submitNewTask<Task>(0.0f, nullptr, &counter, 0, false, false);
  188. a.submitNewTask<Task>(0.0f, nullptr, &counter, -1, true, true);
  189. a.submitNewTask<Task>(0.0f, nullptr, &counter, 2, false, false);
  190. HighRezTimer::sleep(1.0);
  191. ANKI_TEST_EXPECT_EQ(counter.load(), 2);
  192. a.resume();
  193. HighRezTimer::sleep(1.0);
  194. ANKI_TEST_EXPECT_EQ(counter.load(), 4);
  195. }
  196. // Fuzzy test
  197. {
  198. AsyncLoader a;
  199. Barrier barrier(2);
  200. Atomic<U32> counter = {0};
  201. for(U32 i = 0; i < 10; i++)
  202. {
  203. Barrier* pbarrier = nullptr;
  204. if(i == 9)
  205. {
  206. pbarrier = &barrier;
  207. }
  208. a.submitNewTask<Task>(getRandomRange(0.0f, 0.5f), pbarrier, &counter, i);
  209. }
  210. barrier.wait();
  211. ANKI_TEST_EXPECT_EQ(counter.load(), 10);
  212. }
  213. #endif
  214. } // namespace