testThreadSafePriorityQueue.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "unit/test.h"
  23. #include "platform/threads/threadSafePriorityQueue.h"
  24. #include "platform/threads/thread.h"
  25. #include "core/util/tVector.h"
  26. #include "console/console.h"
  27. #ifndef TORQUE_SHIPPING
  28. using namespace UnitTesting;
  29. #define TEST( x ) test( ( x ), "FAIL: " #x )
  30. #define XTEST( t, x ) t->test( ( x ), "FAIL: " #x )
  31. // Test queue without concurrency.
  32. CreateUnitTest( TestThreadSafePriorityQueueSerial, "Platform/ThreadSafePriorityQueue/Serial" )
  33. {
  34. struct Value
  35. {
  36. F32 mPriority;
  37. U32 mIndex;
  38. Value() {}
  39. Value( F32 priority, U32 index )
  40. : mPriority( priority ), mIndex( index ) {}
  41. };
  42. template< bool SORT_MIN_TO_MAX >
  43. void test1()
  44. {
  45. Vector< Value > values;
  46. values.push_back( Value( 0.2f, 2 ) );
  47. values.push_back( Value( 0.7f, 7 ) );
  48. values.push_back( Value( 0.4f, 4 ) );
  49. values.push_back( Value( 0.6f, 6 ) );
  50. values.push_back( Value( 0.1f, 1 ) );
  51. values.push_back( Value( 0.5f, 5 ) );
  52. values.push_back( Value( 0.3f, 3 ) );
  53. values.push_back( Value( 0.8f, 8 ) );
  54. values.push_back( Value( 0.6f, 6 ) );
  55. values.push_back( Value( 0.9f, 9 ) );
  56. values.push_back( Value( 0.0f, 0 ) );
  57. const S32 min = 0;
  58. const S32 max = 9;
  59. ThreadSafePriorityQueue< U32, F32, SORT_MIN_TO_MAX > queue;
  60. for( U32 i = 0; i < values.size(); ++ i )
  61. queue.insert( values[ i ].mPriority, values[ i ].mIndex );
  62. TEST( !queue.isEmpty() );
  63. S32 index;
  64. if( SORT_MIN_TO_MAX )
  65. index = min - 1;
  66. else
  67. index = max + 1;
  68. for( U32 i = 0; i < values.size(); ++ i )
  69. {
  70. U32 value;
  71. TEST( queue.takeNext( value ) );
  72. if( value != index )
  73. {
  74. if( SORT_MIN_TO_MAX )
  75. index ++;
  76. else
  77. index --;
  78. }
  79. TEST( value == index );
  80. }
  81. }
  82. void run()
  83. {
  84. test1< true >();
  85. test1< false >();
  86. }
  87. };
  88. // Test queue with concurrency.
  89. CreateUnitTest( TestThreadSafePriorityQueueConcurrent, "Platform/ThreadSafePriorityQueue/Concurrent" )
  90. {
  91. public:
  92. typedef TestThreadSafePriorityQueueConcurrent TestType;
  93. enum
  94. {
  95. DEFAULT_NUM_VALUES = 100000,
  96. DEFAULT_NUM_CONSUMERS = 10,
  97. DEFAULT_NUM_PRODUCERS = 10
  98. };
  99. struct Value : public ThreadSafeRefCount< Value >
  100. {
  101. U32 mIndex;
  102. F32 mPriority;
  103. bool mCheck;
  104. Value() : mCheck( false ) {}
  105. Value( U32 index, F32 priority )
  106. : mIndex( index ), mPriority( priority ), mCheck( false ) {}
  107. };
  108. typedef ThreadSafeRef< Value > ValueRef;
  109. U32 mProducerIndex;
  110. U32 mConsumerIndex;
  111. ThreadSafePriorityQueue< ValueRef > mQueue;
  112. Vector< ValueRef > mValues;
  113. struct ProducerThread : public Thread
  114. {
  115. ProducerThread( TestType* test )
  116. : Thread( 0, test ) {}
  117. virtual void run( void* arg )
  118. {
  119. _setName( "ProducerThread" );
  120. Platform::outputDebugString( "Starting ProducerThread" );
  121. TestType* test = ( TestType* ) arg;
  122. while( 1 )
  123. {
  124. U32 index = test->mProducerIndex;
  125. if( index == test->mValues.size() )
  126. break;
  127. if( dCompareAndSwap( test->mProducerIndex, index, index + 1 ) )
  128. {
  129. F32 priority = Platform::getRandom();
  130. ValueRef val = new Value( index, priority );
  131. test->mValues[ index ] = val;
  132. test->mQueue.insert( priority, val );
  133. }
  134. }
  135. Platform::outputDebugString( "Stopping ProducerThread" );
  136. }
  137. };
  138. struct ConsumerThread : public Thread
  139. {
  140. ConsumerThread( TestType* test )
  141. : Thread( 0, test ) {}
  142. virtual void run( void* arg )
  143. {
  144. _setName( "ConsumerThread" );
  145. Platform::outputDebugString( "Starting ConsumerThread" );
  146. TestType* t = ( TestType* ) arg;
  147. while( t->mConsumerIndex < t->mValues.size() )
  148. {
  149. ValueRef value;
  150. if( t->mQueue.takeNext( value ) )
  151. {
  152. dFetchAndAdd( t->mConsumerIndex, 1 );
  153. XTEST( t, t->mValues[ value->mIndex ] == value );
  154. value->mCheck = true;
  155. }
  156. else
  157. Platform::sleep( 5 );
  158. }
  159. Platform::outputDebugString( "Stopping ConsumerThread" );
  160. }
  161. };
  162. void run()
  163. {
  164. U32 numValues = Con::getIntVariable( "$testThreadSafePriorityQueue::numValues", DEFAULT_NUM_VALUES );
  165. U32 numConsumers = Con::getIntVariable( "$testThreadSafePriorityQueue::numConsumers", DEFAULT_NUM_CONSUMERS );
  166. U32 numProducers = Con::getIntVariable( "$testThreadSafePriorityQueue::numProducers", DEFAULT_NUM_PRODUCERS );
  167. mProducerIndex = 0;
  168. mConsumerIndex = 0;
  169. mValues.setSize( numValues );
  170. Vector< ProducerThread* > producers;
  171. Vector< ConsumerThread* > consumers;
  172. producers.setSize( numProducers );
  173. consumers.setSize( numConsumers );
  174. for( U32 i = 0; i < numProducers; ++ i )
  175. {
  176. producers[ i ] = new ProducerThread( this );
  177. producers[ i ]->start();
  178. }
  179. for( U32 i = 0; i < numConsumers; ++ i )
  180. {
  181. consumers[ i ] = new ConsumerThread( this );
  182. consumers[ i ]->start();
  183. }
  184. for( U32 i = 0; i < numProducers; ++ i )
  185. {
  186. producers[ i ]->join();
  187. delete producers[ i ];
  188. }
  189. for( U32 i = 0; i < numConsumers; ++ i )
  190. {
  191. consumers[ i ]->join();
  192. delete consumers[ i ];
  193. }
  194. for( U32 i = 0; i < mValues.size(); ++ i )
  195. {
  196. TEST( mValues[ i ] != NULL );
  197. if( mValues[ i ] != NULL )
  198. TEST( mValues[ i ]->mCheck );
  199. }
  200. mValues.clear();
  201. }
  202. };
  203. #endif // !TORQUE_SHIPPING