2
0

ThreadHive.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2009-2016, 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/util/ThreadHive.h>
  7. #include <anki/util/HighRezTimer.h>
  8. namespace anki
  9. {
  10. class ThreadHiveTestContext
  11. {
  12. public:
  13. ThreadHiveTestContext()
  14. {
  15. }
  16. ~ThreadHiveTestContext()
  17. {
  18. }
  19. union
  20. {
  21. Atomic<I32> m_countAtomic;
  22. I32 m_count;
  23. };
  24. };
  25. static void decNumber(void* arg, U32, ThreadHive& hive)
  26. {
  27. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  28. ctx->m_countAtomic.fetchSub(2);
  29. }
  30. static void incNumber(void* arg, U32, ThreadHive& hive)
  31. {
  32. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  33. ctx->m_countAtomic.fetchAdd(4);
  34. hive.submitTask(decNumber, arg);
  35. }
  36. static void taskToWaitOn(void* arg, U32, ThreadHive& hive)
  37. {
  38. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  39. HighRezTimer::sleep(1.0);
  40. ctx->m_count = 10;
  41. HighRezTimer::sleep(0.1);
  42. }
  43. static void taskToWait(void* arg, U32 threadId, ThreadHive& hive)
  44. {
  45. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  46. U prev = ctx->m_countAtomic.fetchAdd(1);
  47. ANKI_TEST_EXPECT_GEQ(prev, 10);
  48. }
  49. ANKI_TEST(Util, ThreadHive)
  50. {
  51. const U32 threadCount = 4;
  52. HeapAllocator<U8> alloc(allocAligned, nullptr);
  53. ThreadHive hive(threadCount, alloc);
  54. // Simple test
  55. if(1)
  56. {
  57. ThreadHiveTestContext ctx;
  58. ctx.m_countAtomic.set(0);
  59. const U INITIAL_TASK_COUNT = 100;
  60. for(U i = 0; i < INITIAL_TASK_COUNT; ++i)
  61. {
  62. hive.submitTask(incNumber, &ctx);
  63. }
  64. hive.waitAllTasks();
  65. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.get(), INITIAL_TASK_COUNT * 2);
  66. }
  67. // Depedency tests
  68. if(1)
  69. {
  70. ThreadHiveTestContext ctx;
  71. ctx.m_count = 0;
  72. ThreadHiveTask task;
  73. task.m_callback = taskToWaitOn;
  74. task.m_argument = &ctx;
  75. hive.submitTasks(&task, 1);
  76. const U DEP_TASKS = 10;
  77. ThreadHiveTask dtasks[DEP_TASKS];
  78. for(U i = 0; i < DEP_TASKS; ++i)
  79. {
  80. dtasks[i].m_callback = taskToWait;
  81. dtasks[i].m_argument = &ctx;
  82. dtasks[i].m_inDependencies = WeakArray<ThreadHiveDependencyHandle>(&task.m_outDependency, 1);
  83. }
  84. hive.submitTasks(&dtasks[0], DEP_TASKS);
  85. // Again
  86. ThreadHiveTask dtasks2[DEP_TASKS];
  87. for(U i = 0; i < DEP_TASKS; ++i)
  88. {
  89. dtasks2[i].m_callback = taskToWait;
  90. dtasks2[i].m_argument = &ctx;
  91. dtasks2[i].m_inDependencies = WeakArray<ThreadHiveDependencyHandle>(&dtasks[i].m_outDependency, 1);
  92. }
  93. hive.submitTasks(&dtasks2[0], DEP_TASKS);
  94. hive.waitAllTasks();
  95. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.get(), DEP_TASKS * 2 + 10);
  96. }
  97. // Fuzzy test
  98. if(1)
  99. {
  100. ThreadHiveTestContext ctx;
  101. ctx.m_count = 0;
  102. I number = 0;
  103. ThreadHiveDependencyHandle dep = 0;
  104. const U SUBMISSION_COUNT = 100;
  105. const U TASK_COUNT = 1000;
  106. for(U i = 0; i < SUBMISSION_COUNT; ++i)
  107. {
  108. for(U j = 0; j < TASK_COUNT; ++j)
  109. {
  110. Bool cb = rand() % 2;
  111. number = (cb) ? number + 2 : number - 2;
  112. ThreadHiveTask task;
  113. task.m_callback = (cb) ? incNumber : decNumber;
  114. task.m_argument = &ctx;
  115. if((rand() % 3) == 0 && j > 0)
  116. {
  117. task.m_inDependencies = WeakArray<ThreadHiveDependencyHandle>(&dep, 1);
  118. }
  119. hive.submitTasks(&task, 1);
  120. if((rand() % 7) == 0)
  121. {
  122. dep = task.m_outDependency;
  123. }
  124. }
  125. dep = 0;
  126. hive.waitAllTasks();
  127. }
  128. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.get(), number);
  129. }
  130. }
  131. } // end namespace anki