OVR_Lockless.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /************************************************************************************
  2. Filename : OVR_Lockless.cpp
  3. Content : Test logic for lock-less classes
  4. Created : December 27, 2013
  5. Authors : Michael Antonov
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. *************************************************************************************/
  19. #include "OVR_Lockless.h"
  20. #ifdef OVR_LOCKLESS_TEST
  21. #include "OVR_Threads.h"
  22. #include "OVR_Timer.h"
  23. #include "OVR_Log.h"
  24. namespace OVR { namespace LocklessTest {
  25. const int TestIterations = 10000000;
  26. // Use volatile dummies to force compiler to do spinning.
  27. volatile int Dummy1;
  28. int Unused1[32];
  29. volatile int Dummy2;
  30. int Unused2[32];
  31. volatile int Dummy3;
  32. int Unused3[32];
  33. // Data block out of 20 consecutive integers, should be internally consistent.
  34. struct TestData
  35. {
  36. enum { ItemCount = 20 };
  37. int Data[ItemCount];
  38. void Set(int val)
  39. {
  40. for (int i=0; i<ItemCount; i++)
  41. {
  42. Data[i] = val*100 + i;
  43. }
  44. }
  45. int ReadAndCheckConsistency(int prevValue) const
  46. {
  47. int val = Data[0];
  48. for (int i=1; i<ItemCount; i++)
  49. {
  50. if (Data[i] != (val + i))
  51. {
  52. // Only complain once per same-value entry
  53. if (prevValue != val / 100)
  54. {
  55. LogText("LocklessTest Fail - corruption at %d inside block %d\n",
  56. i, val/100);
  57. // OVR_ASSERT(Data[i] == val + i);
  58. }
  59. break;
  60. }
  61. }
  62. return val / 100;
  63. }
  64. };
  65. volatile bool FirstItemWritten = false;
  66. LocklessUpdater<TestData, TestData> TestDataUpdater;
  67. // Use this lock to verify that testing algorithm is otherwise correct...
  68. Lock TestLock;
  69. //-------------------------------------------------------------------------------------
  70. // Consumer thread reads values from TestDataUpdater and
  71. // ensures that each one is internally consistent.
  72. class Consumer : public Thread
  73. {
  74. virtual int Run()
  75. {
  76. LogText("LocklessTest::Consumer::Run started.\n");
  77. while (!FirstItemWritten)
  78. {
  79. // spin until producer wrote first value...
  80. }
  81. TestData d;
  82. int oldValue = 0;
  83. int newValue;
  84. do
  85. {
  86. {
  87. //Lock::Locker scope(&TestLock);
  88. d = TestDataUpdater.GetState();
  89. }
  90. newValue = d.ReadAndCheckConsistency(oldValue);
  91. // Values should increase or stay the same!
  92. if (newValue < oldValue)
  93. {
  94. LogText("LocklessTest Fail - %d after %d; delta = %d\n",
  95. newValue, oldValue, newValue - oldValue);
  96. // OVR_ASSERT(0);
  97. }
  98. if (oldValue != newValue)
  99. {
  100. oldValue = newValue;
  101. if (oldValue % (TestIterations/30) == 0)
  102. {
  103. LogText("LocklessTest::Consumer - %5.2f%% done\n",
  104. 100.0f * (float)oldValue/(float)TestIterations);
  105. }
  106. }
  107. // Spin a while
  108. for (int j = 0; j< 300; j++)
  109. {
  110. Dummy3 = j;
  111. }
  112. } while (oldValue < (TestIterations * 99 / 100));
  113. LogText("LocklessTest::Consumer::Run exiting.\n");
  114. return 0;
  115. }
  116. };
  117. //-------------------------------------------------------------------------------------
  118. class Producer : public Thread
  119. {
  120. virtual int Run()
  121. {
  122. LogText("LocklessTest::Producer::Run started.\n");
  123. for (int testVal = 0; testVal < TestIterations; testVal++)
  124. {
  125. TestData d;
  126. d.Set(testVal);
  127. {
  128. //Lock::Locker scope(&TestLock);
  129. TestDataUpdater.SetState(d);
  130. }
  131. FirstItemWritten = true;
  132. // Spin a bit
  133. for(int j = 0; j < 1000; j++)
  134. {
  135. Dummy2 = j;
  136. }
  137. if (testVal % (TestIterations/30) == 0)
  138. {
  139. LogText("LocklessTest::Producer - %5.2f%% done\n",
  140. 100.0f * (float)testVal/(float)TestIterations);
  141. }
  142. }
  143. LogText("LocklessTest::Producer::Run exiting.\n");
  144. return 0;
  145. }
  146. };
  147. } // namespace LocklessTest
  148. void StartLocklessTest()
  149. {
  150. // These threads will release themselves once done
  151. Ptr<LocklessTest::Producer> producerThread = *new LocklessTest::Producer;
  152. Ptr<LocklessTest::Consumer> consumerThread = *new LocklessTest::Consumer;
  153. producerThread->Start();
  154. consumerThread->Start();
  155. while (!producerThread->IsFinished() && consumerThread->IsFinished())
  156. {
  157. Thread::MSleep(500);
  158. }
  159. }
  160. } // namespace OVR
  161. #endif // OVR_LOCKLESS_TEST