threadSafeDequeTest.cpp 4.5 KB

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