threadSafeDequeTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/threads/threadSafeDeque.h"
  25. #include "platform/threads/thread.h"
  26. #include "core/util/tVector.h"
  27. #include "console/console.h"
  28. FIXTURE(ThreadSafeDeque)
  29. {
  30. public:
  31. // Used by the concurrent test.
  32. struct Value : public ThreadSafeRefCount<Value>
  33. {
  34. U32 mIndex;
  35. U32 mTick;
  36. Value() {}
  37. Value(U32 index, U32 tick)
  38. : mIndex(index), mTick(tick) {}
  39. };
  40. typedef ThreadSafeRef<Value> ValueRef;
  41. struct Deque : public ThreadSafeDeque<ValueRef>
  42. {
  43. typedef ThreadSafeDeque<ValueRef> Parent;
  44. U32 mPushIndex;
  45. U32 mPopIndex;
  46. Deque()
  47. : mPushIndex(0), mPopIndex(0) {}
  48. void pushBack(const ValueRef& value)
  49. {
  50. EXPECT_EQ(value->mIndex, mPushIndex) << "index out of line";
  51. mPushIndex++;
  52. Parent::pushBack(value);
  53. }
  54. bool tryPopFront(ValueRef& outValue)
  55. {
  56. if(Parent::tryPopFront(outValue))
  57. {
  58. EXPECT_EQ(outValue->mIndex, mPopIndex) << "index out of line";
  59. mPopIndex++;
  60. return true;
  61. }
  62. else
  63. return false;
  64. }
  65. };
  66. struct ProducerThread : public Thread
  67. {
  68. Vector<U32>& mValues;
  69. Deque& mDeque;
  70. ProducerThread(Vector<U32>& values, Deque& deque)
  71. : mValues(values), mDeque(deque) {}
  72. virtual void run(void*)
  73. {
  74. for(U32 i = 0; i < mValues.size(); i++)
  75. {
  76. U32 tick = Platform::getRealMilliseconds();
  77. mValues[i] = tick;
  78. ValueRef val = new Value(i, tick);
  79. mDeque.pushBack(val);
  80. }
  81. }
  82. };
  83. struct ConsumerThread : public Thread
  84. {
  85. Vector<U32>& mValues;
  86. Deque& mDeque;
  87. ConsumerThread(Vector<U32>& values, Deque& deque)
  88. : mValues(values), mDeque(deque) {}
  89. virtual void run(void*)
  90. {
  91. S32 timeOut = mValues.size() * 32;
  92. U32 endTime = Platform::getRealMilliseconds() + timeOut;
  93. for (U32 i = 0; i < mValues.size(); i++)
  94. {
  95. ValueRef value;
  96. bool timedOut = false;
  97. while (!mDeque.tryPopFront(value))
  98. {
  99. if (timeOut && Platform::getRealMilliseconds() >= endTime)
  100. {
  101. timedOut = true;
  102. break;
  103. }
  104. };
  105. ASSERT_FALSE(timedOut)
  106. << "consumer thread timed out!";
  107. if (timedOut) return;
  108. EXPECT_EQ(i, value->mIndex);
  109. EXPECT_EQ(value->mTick, mValues[i]);
  110. }
  111. }
  112. };
  113. };
  114. // Test deque without concurrency.
  115. TEST_FIX(ThreadSafeDeque, PopFront)
  116. {
  117. ThreadSafeDeque<char> deque;
  118. String str = "teststring";
  119. for(U32 i = 0; i < str.length(); i++)
  120. deque.pushBack(str[i]);
  121. EXPECT_FALSE(deque.isEmpty());
  122. char ch;
  123. for(U32 i = 0; i < str.length(); i++)
  124. {
  125. EXPECT_TRUE(deque.tryPopFront(ch));
  126. EXPECT_EQ(str[i], ch);
  127. }
  128. ASSERT_TRUE(deque.isEmpty());
  129. }
  130. TEST_FIX(ThreadSafeDeque, PopBack)
  131. {
  132. ThreadSafeDeque<char> deque;
  133. String str = "teststring";
  134. const char* p1 = str.c_str() + 4;
  135. const char* p2 = p1 + 1;
  136. while(*p2)
  137. {
  138. deque.pushFront(*p1);
  139. deque.pushBack(*p2);
  140. --p1;
  141. ++p2;
  142. }
  143. char ch;
  144. for(S32 i = str.length()-1; i >= 0; i--)
  145. {
  146. EXPECT_TRUE(deque.tryPopBack(ch));
  147. EXPECT_EQ(str[i], ch);
  148. }
  149. ASSERT_TRUE(deque.isEmpty());
  150. }
  151. // Test deque in a concurrent setting.
  152. // Test many items in a row
  153. TEST_FIX(ThreadSafeDeque, Concurrent1)
  154. {
  155. const U32 NumValues = 100;
  156. Deque mDeque;
  157. Vector<U32> mValues;
  158. mValues.setSize(NumValues);
  159. ProducerThread pThread(mValues, mDeque);
  160. ConsumerThread cThread(mValues, mDeque);
  161. pThread.start();
  162. cThread.start();
  163. pThread.join();
  164. cThread.join();
  165. mValues.clear();
  166. };
  167. // Test a few items many times to catch any race-condition in start-up
  168. TEST_FIX(ThreadSafeDeque, Concurrent2)
  169. {
  170. for (int i = 0; i < 10000; ++i)
  171. {
  172. Deque mDeque;
  173. Vector<U32> mValues;
  174. mValues.setSize(5);
  175. ProducerThread pThread(mValues, mDeque);
  176. ConsumerThread cThread(mValues, mDeque);
  177. cThread.start();
  178. pThread.start();
  179. pThread.join();
  180. cThread.join();
  181. mValues.clear();
  182. if (::testing::Test::HasFailure()) break;
  183. }
  184. };
  185. #endif