testThreadSafeRefCount.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/threadSafeRefCount.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. CreateUnitTest( TestThreadSafeRefCountSerial, "Platform/ThreadSafeRefCount/Serial" )
  31. {
  32. struct TestObject : public ThreadSafeRefCount< TestObject >
  33. {
  34. static bool smDeleted;
  35. TestObject()
  36. {
  37. smDeleted = false;
  38. }
  39. ~TestObject()
  40. {
  41. smDeleted = true;
  42. }
  43. };
  44. typedef ThreadSafeRef< TestObject > TestObjectRef;
  45. void run()
  46. {
  47. TestObjectRef ref1 = new TestObject;
  48. TEST( !ref1->isShared() );
  49. TEST( ref1 != NULL );
  50. TestObjectRef ref2 = ref1;
  51. TEST( ref1->isShared() );
  52. TEST( ref2->isShared() );
  53. TEST( ref1 == ref2 );
  54. ref1 = NULL;
  55. TEST( !ref2->isShared() );
  56. ref2 = NULL;
  57. TEST( TestObject::smDeleted );
  58. }
  59. };
  60. bool TestThreadSafeRefCountSerial::TestObject::smDeleted;
  61. CreateUnitTest( TestThreadSafeRefCountConcurrent, "Platform/ThreadSafeRefCount/Concurrent" )
  62. {
  63. public:
  64. typedef TestThreadSafeRefCountConcurrent TestType;
  65. enum
  66. {
  67. NUM_ADD_REFS_PER_THREAD = 1000,
  68. NUM_EXTRA_REFS_PER_THREAD = 1000,
  69. NUM_THREADS = 10
  70. };
  71. class TestObject : public ThreadSafeRefCount< TestObject >
  72. {
  73. public:
  74. };
  75. ThreadSafeRef< TestObject > mRef;
  76. class TestThread : public Thread
  77. {
  78. public:
  79. TestType* mTest;
  80. Vector< ThreadSafeRef< TestObject > > mExtraRefs;
  81. TestThread( TestType* test )
  82. : mTest( test ) {}
  83. void run( void* arg )
  84. {
  85. if( !arg )
  86. {
  87. for( U32 i = 0; i < NUM_ADD_REFS_PER_THREAD; ++ i )
  88. mTest->mRef->addRef();
  89. mExtraRefs.setSize( NUM_EXTRA_REFS_PER_THREAD );
  90. for( U32 i = 0; i < NUM_EXTRA_REFS_PER_THREAD; ++ i )
  91. mExtraRefs[ i ] = mTest->mRef;
  92. }
  93. else
  94. {
  95. mExtraRefs.clear();
  96. for( U32 i = 0; i < NUM_ADD_REFS_PER_THREAD; ++ i )
  97. mTest->mRef->release();
  98. }
  99. }
  100. };
  101. void run()
  102. {
  103. mRef = new TestObject;
  104. TEST( mRef->getRefCount() == 2 ); // increments of 2
  105. Vector< TestThread* > threads;
  106. threads.setSize( NUM_THREADS );
  107. // Create threads.
  108. for( U32 i = 0; i < NUM_THREADS; ++ i )
  109. threads[ i ] = new TestThread( this );
  110. // Run phase 1: create references.
  111. for( U32 i = 0; i < NUM_THREADS; ++ i )
  112. threads[ i ]->start( NULL );
  113. // Wait for completion.
  114. for( U32 i = 0; i < NUM_THREADS; ++ i )
  115. threads[ i ]->join();
  116. Con::printf( "REF: %i", mRef->getRefCount() );
  117. TEST( mRef->getRefCount() == 2 + ( ( NUM_ADD_REFS_PER_THREAD + NUM_EXTRA_REFS_PER_THREAD ) * NUM_THREADS * 2 ) );
  118. // Run phase 2: release references.
  119. for( U32 i = 0; i < NUM_THREADS; ++ i )
  120. threads[ i ]->start( ( void* ) 1 );
  121. // Wait for completion.
  122. for( U32 i = 0; i < NUM_THREADS; ++ i )
  123. {
  124. threads[ i ]->join();
  125. delete threads[ i ];
  126. }
  127. TEST( mRef->getRefCount() == 2 ); // increments of two
  128. mRef = NULL;
  129. }
  130. };
  131. CreateUnitTest( TestThreadSafeRefCountTagging, "Platform/ThreadSafeRefCount/Tagging" )
  132. {
  133. struct TestObject : public ThreadSafeRefCount< TestObject > {};
  134. typedef ThreadSafeRef< TestObject > TestObjectRef;
  135. void run()
  136. {
  137. TestObjectRef ref;
  138. TEST( !ref.isTagged() );
  139. TEST( !ref );
  140. TEST( !ref.ptr() );
  141. TEST( ref.trySetFromTo( ref, NULL ) );
  142. TEST( !ref.isTagged() );
  143. TEST( ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_Set ) );
  144. TEST( ref.isTagged() );
  145. TEST( ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_Set ) );
  146. TEST( ref.isTagged() );
  147. TEST( ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_Unset ) );
  148. TEST( !ref.isTagged() );
  149. TEST( ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_Unset ) );
  150. TEST( !ref.isTagged() );
  151. TEST( ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_SetOrFail ) );
  152. TEST( ref.isTagged() );
  153. TEST( !ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_SetOrFail ) );
  154. TEST( ref.isTagged() );
  155. TEST( !ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_FailIfSet ) );
  156. TEST( ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_UnsetOrFail ) );
  157. TEST( !ref.isTagged() );
  158. TEST( !ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_UnsetOrFail ) );
  159. TEST( !ref.isTagged() );
  160. TEST( !ref.trySetFromTo( ref, NULL, TestObjectRef::TAG_FailIfUnset ) );
  161. TestObjectRef objectA = new TestObject;
  162. TestObjectRef objectB = new TestObject;
  163. TEST( !objectA->isShared() );
  164. TEST( !objectB->isShared() );
  165. ref = objectA;
  166. TEST( !ref.isTagged() );
  167. TEST( ref == objectA );
  168. TEST( ref == objectA.ptr() );
  169. TEST( objectA->isShared() );
  170. TEST( ref.trySetFromTo( objectA, objectB, TestObjectRef::TAG_Set ) );
  171. TEST( ref.isTagged() );
  172. TEST( ref == objectB );
  173. TEST( ref == objectB.ptr() );
  174. TEST( objectB->isShared() );
  175. TEST( !objectA->isShared() );
  176. TEST( ref.trySetFromTo( ref, objectA ) );
  177. TEST( ref.isTagged() );
  178. TEST( ref == objectA );
  179. TEST( ref == objectA.ptr() );
  180. }
  181. };
  182. #endif // !TORQUE_SHIPPING