LockFreeStacks.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "UserTypes.h"
  9. #include <AzCore/std/parallel/containers/lock_free_stack.h>
  10. #include <AzCore/std/parallel/containers/lock_free_stamped_stack.h>
  11. #include <AzCore/std/parallel/containers/lock_free_intrusive_stack.h>
  12. #include <AzCore/std/parallel/containers/lock_free_intrusive_stamped_stack.h>
  13. #include <AzCore/std/parallel/thread.h>
  14. #include <AzCore/std/functional.h>
  15. namespace UnitTest
  16. {
  17. using namespace AZStd;
  18. using namespace UnitTestInternal;
  19. class LockFreeStack
  20. : public LeakDetectionFixture
  21. {
  22. public:
  23. #ifdef _DEBUG
  24. static const int NUM_ITERATIONS = 5000;
  25. #else
  26. static const int NUM_ITERATIONS = 50000;
  27. #endif
  28. template <class S>
  29. void Push(S* stack)
  30. {
  31. for (int i = 1; i <= NUM_ITERATIONS; ++i)
  32. {
  33. stack->push(i);
  34. m_total.fetch_add(i);
  35. }
  36. }
  37. template <class S>
  38. void Pop(S* stack)
  39. {
  40. int numPopped = 0;
  41. while (numPopped != NUM_ITERATIONS)
  42. {
  43. int value = 0;
  44. if (stack->pop(&value))
  45. {
  46. m_total.fetch_sub(value);
  47. ++numPopped;
  48. }
  49. else
  50. {
  51. // for some schedulers we need the yield otherwise we will deadlock
  52. AZStd::this_thread::yield();
  53. }
  54. }
  55. }
  56. atomic<int> m_total;
  57. };
  58. TEST_F(LockFreeStack, LockFreeStack)
  59. {
  60. lock_free_stack<int, MyLockFreeAllocator> stack;
  61. int result = 0;
  62. AZ_TEST_ASSERT(stack.empty());
  63. AZ_TEST_ASSERT(!stack.pop(&result));
  64. stack.push(20);
  65. AZ_TEST_ASSERT(!stack.empty());
  66. AZ_TEST_ASSERT(stack.pop(&result));
  67. AZ_TEST_ASSERT(result == 20);
  68. AZ_TEST_ASSERT(stack.empty());
  69. AZ_TEST_ASSERT(!stack.pop(&result));
  70. stack.push(20);
  71. stack.push(30);
  72. AZ_TEST_ASSERT(!stack.empty());
  73. AZ_TEST_ASSERT(stack.pop(&result));
  74. AZ_TEST_ASSERT(result == 30);
  75. AZ_TEST_ASSERT(!stack.empty());
  76. AZ_TEST_ASSERT(stack.pop(&result));
  77. AZ_TEST_ASSERT(result == 20);
  78. AZ_TEST_ASSERT(stack.empty());
  79. AZ_TEST_ASSERT(!stack.pop(&result));
  80. {
  81. m_total = 0;
  82. AZStd::thread thread0(AZStd::bind(&LockFreeStack::Push<decltype(stack)>, this, &stack));
  83. AZStd::thread thread1(AZStd::bind(&LockFreeStack::Pop<decltype(stack)>, this, &stack));
  84. AZStd::thread thread2(AZStd::bind(&LockFreeStack::Push<decltype(stack)>, this, &stack));
  85. AZStd::thread thread3(AZStd::bind(&LockFreeStack::Pop<decltype(stack)>, this, &stack));
  86. thread0.join();
  87. thread1.join();
  88. thread2.join();
  89. thread3.join();
  90. AZ_TEST_ASSERT(m_total == 0);
  91. AZ_TEST_ASSERT(stack.empty());
  92. }
  93. }
  94. TEST_F(LockFreeStack, LockFreeStampedStack)
  95. {
  96. lock_free_stamped_stack<int, MyLockFreeAllocator> stack;
  97. int result = 0;
  98. AZ_TEST_ASSERT(stack.empty());
  99. AZ_TEST_ASSERT(!stack.pop(&result));
  100. stack.push(20);
  101. AZ_TEST_ASSERT(!stack.empty());
  102. AZ_TEST_ASSERT(stack.pop(&result));
  103. AZ_TEST_ASSERT(result == 20);
  104. AZ_TEST_ASSERT(stack.empty());
  105. AZ_TEST_ASSERT(!stack.pop(&result));
  106. stack.push(20);
  107. stack.push(30);
  108. AZ_TEST_ASSERT(!stack.empty());
  109. AZ_TEST_ASSERT(stack.pop(&result));
  110. AZ_TEST_ASSERT(result == 30);
  111. AZ_TEST_ASSERT(!stack.empty());
  112. AZ_TEST_ASSERT(stack.pop(&result));
  113. AZ_TEST_ASSERT(result == 20);
  114. AZ_TEST_ASSERT(stack.empty());
  115. AZ_TEST_ASSERT(!stack.pop(&result));
  116. {
  117. m_total = 0;
  118. AZStd::thread thread0(AZStd::bind(&LockFreeStack::Push<decltype(stack)>, this, &stack));
  119. AZStd::thread thread1(AZStd::bind(&LockFreeStack::Pop<decltype(stack)>, this, &stack));
  120. AZStd::thread thread2(AZStd::bind(&LockFreeStack::Push<decltype(stack)>, this, &stack));
  121. AZStd::thread thread3(AZStd::bind(&LockFreeStack::Pop<decltype(stack)>, this, &stack));
  122. thread0.join();
  123. thread1.join();
  124. thread2.join();
  125. thread3.join();
  126. AZ_TEST_ASSERT(m_total == 0);
  127. AZ_TEST_ASSERT(stack.empty());
  128. }
  129. }
  130. struct MyStackItem
  131. : public lock_free_intrusive_stack_node<MyStackItem>
  132. {
  133. public:
  134. MyStackItem() {}
  135. MyStackItem(int data)
  136. : m_data(data) {}
  137. lock_free_intrusive_stack_node<MyStackItem> m_listHook; // Public member hook.
  138. int m_data; // This is left public only for testing purpose.
  139. };
  140. class LockFreeIntrusiveStack
  141. : public LeakDetectionFixture
  142. {
  143. public:
  144. #ifdef _DEBUG
  145. static const int NUM_ITERATIONS = 5000;
  146. #else
  147. static const int NUM_ITERATIONS = 50000;
  148. #endif
  149. template <class S>
  150. void Push(S* stack, vector<MyStackItem>* items)
  151. {
  152. for (int i = 0; i < NUM_ITERATIONS; ++i)
  153. {
  154. stack->push((*items)[i]);
  155. m_total.fetch_add((*items)[i].m_data);
  156. }
  157. }
  158. template <class S>
  159. void Pop(S* stack)
  160. {
  161. int numPopped = 0;
  162. while (numPopped != NUM_ITERATIONS)
  163. {
  164. MyStackItem* item = stack->pop();
  165. if (item)
  166. {
  167. m_total.fetch_sub(item->m_data);
  168. ++numPopped;
  169. }
  170. }
  171. }
  172. atomic<int> m_total;
  173. };
  174. typedef lock_free_intrusive_stack<MyStackItem,
  175. lock_free_intrusive_stack_base_hook<MyStackItem> > MyIntrusiveStackBase;
  176. typedef lock_free_intrusive_stack<MyStackItem,
  177. lock_free_intrusive_stack_member_hook<MyStackItem, & MyStackItem::m_listHook> > MyIntrusiveStackMember;
  178. typedef lock_free_intrusive_stamped_stack<MyStackItem,
  179. lock_free_intrusive_stack_base_hook<MyStackItem> > MyIntrusiveStampedStackBase;
  180. typedef lock_free_intrusive_stamped_stack<MyStackItem,
  181. lock_free_intrusive_stack_member_hook<MyStackItem, & MyStackItem::m_listHook> > MyIntrusiveStampedStackMember;
  182. TEST_F(LockFreeIntrusiveStack, MyIntrusiveStackBase)
  183. {
  184. MyIntrusiveStackBase stack;
  185. MyStackItem item1(100);
  186. MyStackItem item2(200);
  187. AZ_TEST_ASSERT(stack.empty());
  188. AZ_TEST_ASSERT(!stack.pop());
  189. stack.push(item1);
  190. AZ_TEST_ASSERT(!stack.empty());
  191. AZ_TEST_ASSERT(stack.pop() == &item1);
  192. AZ_TEST_ASSERT(stack.empty());
  193. AZ_TEST_ASSERT(!stack.pop());
  194. stack.push(item1);
  195. stack.push(item2);
  196. AZ_TEST_ASSERT(!stack.empty());
  197. AZ_TEST_ASSERT(stack.pop() == &item2);
  198. AZ_TEST_ASSERT(!stack.empty());
  199. AZ_TEST_ASSERT(stack.pop() == &item1);
  200. AZ_TEST_ASSERT(stack.empty());
  201. AZ_TEST_ASSERT(!stack.pop());
  202. vector<MyStackItem> items;
  203. items.reserve(NUM_ITERATIONS);
  204. for (int i = 1; i <= NUM_ITERATIONS; ++i)
  205. {
  206. items.push_back(MyStackItem(i));
  207. }
  208. {
  209. m_total = 0;
  210. AZStd::thread thread0(AZStd::bind(&LockFreeIntrusiveStack::Push<decltype(stack)>, this, &stack, &items));
  211. AZStd::thread thread1(AZStd::bind(&LockFreeIntrusiveStack::Pop<decltype(stack)>, this, &stack));
  212. thread0.join();
  213. thread1.join();
  214. AZ_TEST_ASSERT(m_total == 0);
  215. AZ_TEST_ASSERT(stack.empty());
  216. }
  217. }
  218. TEST_F(LockFreeIntrusiveStack, MyIntrusiveStackMember)
  219. {
  220. MyIntrusiveStackMember stack;
  221. MyStackItem item1(100);
  222. MyStackItem item2(200);
  223. AZ_TEST_ASSERT(stack.empty());
  224. AZ_TEST_ASSERT(!stack.pop());
  225. stack.push(item1);
  226. AZ_TEST_ASSERT(!stack.empty());
  227. AZ_TEST_ASSERT(stack.pop() == &item1);
  228. AZ_TEST_ASSERT(stack.empty());
  229. AZ_TEST_ASSERT(!stack.pop());
  230. stack.push(item1);
  231. stack.push(item2);
  232. AZ_TEST_ASSERT(!stack.empty());
  233. AZ_TEST_ASSERT(stack.pop() == &item2);
  234. AZ_TEST_ASSERT(!stack.empty());
  235. AZ_TEST_ASSERT(stack.pop() == &item1);
  236. AZ_TEST_ASSERT(stack.empty());
  237. AZ_TEST_ASSERT(!stack.pop());
  238. vector<MyStackItem> items;
  239. items.reserve(NUM_ITERATIONS);
  240. for (int i = 1; i <= NUM_ITERATIONS; ++i)
  241. {
  242. items.push_back(MyStackItem(i));
  243. }
  244. {
  245. m_total = 0;
  246. AZStd::thread thread0(AZStd::bind(&LockFreeIntrusiveStack::Push<decltype(stack)>, this, &stack, &items));
  247. AZStd::thread thread1(AZStd::bind(&LockFreeIntrusiveStack::Pop<decltype(stack)>, this, &stack));
  248. thread0.join();
  249. thread1.join();
  250. AZ_TEST_ASSERT(m_total == 0);
  251. AZ_TEST_ASSERT(stack.empty());
  252. }
  253. }
  254. TEST_F(LockFreeIntrusiveStack, MyIntrusiveStampedStackBase)
  255. {
  256. MyIntrusiveStampedStackBase stack;
  257. MyStackItem item1(100);
  258. MyStackItem item2(200);
  259. AZ_TEST_ASSERT(stack.empty());
  260. AZ_TEST_ASSERT(!stack.pop());
  261. stack.push(item1);
  262. AZ_TEST_ASSERT(!stack.empty());
  263. AZ_TEST_ASSERT(stack.pop() == &item1);
  264. AZ_TEST_ASSERT(stack.empty());
  265. AZ_TEST_ASSERT(!stack.pop());
  266. stack.push(item1);
  267. stack.push(item2);
  268. AZ_TEST_ASSERT(!stack.empty());
  269. AZ_TEST_ASSERT(stack.pop() == &item2);
  270. AZ_TEST_ASSERT(!stack.empty());
  271. AZ_TEST_ASSERT(stack.pop() == &item1);
  272. AZ_TEST_ASSERT(stack.empty());
  273. AZ_TEST_ASSERT(!stack.pop());
  274. vector<MyStackItem> items;
  275. items.reserve(NUM_ITERATIONS);
  276. for (int i = 1; i <= NUM_ITERATIONS; ++i)
  277. {
  278. items.push_back(MyStackItem(i));
  279. }
  280. {
  281. m_total = 0;
  282. AZStd::thread thread0(AZStd::bind(&LockFreeIntrusiveStack::Push<decltype(stack)>, this, &stack, &items));
  283. AZStd::thread thread1(AZStd::bind(&LockFreeIntrusiveStack::Pop<decltype(stack)>, this, &stack));
  284. thread0.join();
  285. thread1.join();
  286. AZ_TEST_ASSERT(m_total == 0);
  287. AZ_TEST_ASSERT(stack.empty());
  288. }
  289. }
  290. TEST_F(LockFreeIntrusiveStack, MyIntrusiveStampedStackMember)
  291. {
  292. MyIntrusiveStampedStackMember stack;
  293. MyStackItem item1(100);
  294. MyStackItem item2(200);
  295. AZ_TEST_ASSERT(stack.empty());
  296. AZ_TEST_ASSERT(!stack.pop());
  297. stack.push(item1);
  298. AZ_TEST_ASSERT(!stack.empty());
  299. AZ_TEST_ASSERT(stack.pop() == &item1);
  300. AZ_TEST_ASSERT(stack.empty());
  301. AZ_TEST_ASSERT(!stack.pop());
  302. stack.push(item1);
  303. stack.push(item2);
  304. AZ_TEST_ASSERT(!stack.empty());
  305. AZ_TEST_ASSERT(stack.pop() == &item2);
  306. AZ_TEST_ASSERT(!stack.empty());
  307. AZ_TEST_ASSERT(stack.pop() == &item1);
  308. AZ_TEST_ASSERT(stack.empty());
  309. AZ_TEST_ASSERT(!stack.pop());
  310. vector<MyStackItem> items;
  311. items.reserve(NUM_ITERATIONS);
  312. for (int i = 1; i <= NUM_ITERATIONS; ++i)
  313. {
  314. items.push_back(MyStackItem(i));
  315. }
  316. {
  317. m_total = 0;
  318. AZStd::thread thread0(AZStd::bind(&LockFreeIntrusiveStack::Push<decltype(stack)>, this, &stack, &items));
  319. AZStd::thread thread1(AZStd::bind(&LockFreeIntrusiveStack::Pop<decltype(stack)>, this, &stack));
  320. thread0.join();
  321. thread1.join();
  322. AZ_TEST_ASSERT(m_total == 0);
  323. AZ_TEST_ASSERT(stack.empty());
  324. }
  325. }
  326. }