threadSafeDequeTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include "testing/unitTesting.h"
  23. #include "platform/threads/threadSafeDeque.h"
  24. #include "platform/threads/thread.h"
  25. #include "core/util/tVector.h"
  26. #include "console/console.h"
  27. FIXTURE(ThreadSafeDeque)
  28. {
  29. public:
  30. // Used by the concurrent test.
  31. struct Value : public ThreadSafeRefCount<Value>
  32. {
  33. U32 mIndex;
  34. U32 mTick;
  35. Value() {}
  36. Value(U32 index, U32 tick)
  37. : mIndex(index), mTick(tick) {}
  38. };
  39. typedef ThreadSafeRef<Value> ValueRef;
  40. struct Deque : public ThreadSafeDeque<ValueRef>
  41. {
  42. typedef ThreadSafeDeque<ValueRef> Parent;
  43. U32 mPushIndex;
  44. U32 mPopIndex;
  45. Deque()
  46. : mPushIndex(0), mPopIndex(0) {}
  47. void pushBack(const ValueRef& value)
  48. {
  49. EXPECT_EQ(value->mIndex, mPushIndex) << "index out of line";
  50. mPushIndex++;
  51. Parent::pushBack(value);
  52. }
  53. bool tryPopFront(ValueRef& outValue)
  54. {
  55. if(Parent::tryPopFront(outValue))
  56. {
  57. EXPECT_EQ(outValue->mIndex, mPopIndex) << "index out of line";
  58. mPopIndex++;
  59. return true;
  60. }
  61. else
  62. return false;
  63. }
  64. };
  65. struct ProducerThread : public Thread
  66. {
  67. Vector<U32>& mValues;
  68. Deque& mDeque;
  69. ProducerThread(Vector<U32>& values, Deque& deque)
  70. : mValues(values), mDeque(deque) {}
  71. virtual void run(void*)
  72. {
  73. for(U32 i = 0; i < mValues.size(); i++)
  74. {
  75. U32 tick = Platform::getRealMilliseconds();
  76. mValues[i] = tick;
  77. ValueRef val = new Value(i, tick);
  78. mDeque.pushBack(val);
  79. }
  80. }
  81. };
  82. struct ConsumerThread : public Thread
  83. {
  84. Vector<U32>& mValues;
  85. Deque& mDeque;
  86. ConsumerThread(Vector<U32>& values, Deque& deque)
  87. : mValues(values), mDeque(deque) {}
  88. virtual void run(void*)
  89. {
  90. S32 timeOut = mValues.size() * 32;
  91. U32 endTime = Platform::getRealMilliseconds() + timeOut;
  92. for (U32 i = 0; i < mValues.size(); i++)
  93. {
  94. ValueRef value;
  95. bool timedOut = false;
  96. while (!mDeque.tryPopFront(value))
  97. {
  98. if (timeOut && Platform::getRealMilliseconds() >= endTime)
  99. {
  100. timedOut = true;
  101. break;
  102. }
  103. };
  104. ASSERT_FALSE(timedOut)
  105. << "consumer thread timed out!";
  106. if (timedOut) return;
  107. EXPECT_EQ(i, value->mIndex);
  108. EXPECT_EQ(value->mTick, mValues[i]);
  109. }
  110. }
  111. };
  112. };
  113. // Test deque without concurrency.
  114. TEST_FIX(ThreadSafeDeque, PopFront)
  115. {
  116. ThreadSafeDeque<char> deque;
  117. String str = "teststring";
  118. for(U32 i = 0; i < str.length(); i++)
  119. deque.pushBack(str[i]);
  120. EXPECT_FALSE(deque.isEmpty());
  121. char ch;
  122. for(U32 i = 0; i < str.length(); i++)
  123. {
  124. EXPECT_TRUE(deque.tryPopFront(ch));
  125. EXPECT_EQ(str[i], ch);
  126. }
  127. ASSERT_TRUE(deque.isEmpty());
  128. }
  129. TEST_FIX(ThreadSafeDeque, PopBack)
  130. {
  131. ThreadSafeDeque<char> deque;
  132. String str = "teststring";
  133. const char* p1 = str.c_str() + 4;
  134. const char* p2 = p1 + 1;
  135. while(*p2)
  136. {
  137. deque.pushFront(*p1);
  138. deque.pushBack(*p2);
  139. --p1;
  140. ++p2;
  141. }
  142. char ch;
  143. for(S32 i = str.length()-1; i >= 0; i--)
  144. {
  145. EXPECT_TRUE(deque.tryPopBack(ch));
  146. EXPECT_EQ(str[i], ch);
  147. }
  148. ASSERT_TRUE(deque.isEmpty());
  149. }
  150. // Test many items in a row
  151. TEST_FIX(ThreadSafeDeque, DISABLED_Concurrent)
  152. {
  153. const U32 NumValues = 50;
  154. Deque mDeque;
  155. Vector<U32> mValues;
  156. mValues.setSize(NumValues);
  157. ProducerThread pThread(mValues, mDeque);
  158. ConsumerThread cThread(mValues, mDeque);
  159. pThread.start();
  160. cThread.start();
  161. pThread.join();
  162. cThread.join();
  163. mValues.clear();
  164. };