OVR_Lockless.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /************************************************************************************
  2. PublicHeader: OVR_Kernel.h
  3. Filename : OVR_Lockless.h
  4. Content : Lock-less classes for producer/consumer communication
  5. Created : November 9, 2013
  6. Authors : John Carmack
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. *************************************************************************************/
  20. #ifndef OVR_Lockless_h
  21. #define OVR_Lockless_h
  22. #include <cstring>
  23. using std::memcpy;
  24. #include "OVR_Atomic.h"
  25. // Define this to compile-in Lockless test logic
  26. //#define OVR_LOCKLESS_TEST
  27. namespace OVR {
  28. // ***** LocklessUpdater
  29. // For single producer cases where you only care about the most recent update, not
  30. // necessarily getting every one that happens (vsync timing, SensorFusion updates).
  31. //
  32. // This is multiple consumer safe, but is currently only used with a single consumer.
  33. //
  34. // The SlotType can be the same as T, but should probably be a larger fixed size.
  35. // This allows for forward compatibility when the updater is shared between processes.
  36. // FIXME: ExchangeAdd_Sync() should be replaced with a portable read-only primitive,
  37. // so that the lockless pose state can be read-only on remote processes and to reduce
  38. // false sharing between processes and improve performance.
  39. template<class T, class SlotType = T>
  40. class LocklessUpdater
  41. {
  42. public:
  43. LocklessUpdater() : UpdateBegin( 0 ), UpdateEnd( 0 )
  44. {
  45. OVR_COMPILER_ASSERT(sizeof(T) <= sizeof(SlotType));
  46. }
  47. T GetState() const
  48. {
  49. // Copy the state out, then retry with the alternate slot
  50. // if we determine that our copy may have been partially
  51. // stepped on by a new update.
  52. T state;
  53. int begin, end, final;
  54. for(;;)
  55. {
  56. // We are adding 0, only using these as atomic memory barriers, so it
  57. // is ok to cast off the const, allowing GetState() to remain const.
  58. end = UpdateEnd.Load_Acquire();
  59. state = Slots[ end & 1 ];
  60. begin = UpdateBegin.Load_Acquire();
  61. if ( begin == end ) {
  62. break;
  63. }
  64. // The producer is potentially blocked while only having partially
  65. // written the update, so copy out the other slot.
  66. state = Slots[ (begin & 1) ^ 1 ];
  67. final = UpdateBegin.Load_Acquire();
  68. if ( final == begin ) {
  69. break;
  70. }
  71. // The producer completed the last update and started a new one before
  72. // we got it copied out, so try fetching the current buffer again.
  73. }
  74. return state;
  75. }
  76. void SetState( const T& state )
  77. {
  78. const int slot = UpdateBegin.ExchangeAdd_Sync(1) & 1;
  79. // Write to (slot ^ 1) because ExchangeAdd returns 'previous' value before add.
  80. Slots[slot ^ 1] = state;
  81. UpdateEnd.ExchangeAdd_Sync(1);
  82. }
  83. AtomicInt<int> UpdateBegin;
  84. AtomicInt<int> UpdateEnd;
  85. SlotType Slots[2];
  86. };
  87. #pragma pack(push, 8)
  88. // Padded out version stored in the updater slots
  89. // Designed to be a larger fixed size to allow the data to grow in the future
  90. // without breaking older compiled code.
  91. OVR_DISABLE_MSVC_WARNING(4351)
  92. template <class Payload, int PaddingSize>
  93. struct LocklessPadding
  94. {
  95. uint8_t buffer[PaddingSize];
  96. LocklessPadding() : buffer() { }
  97. LocklessPadding& operator=(const Payload& rhs)
  98. {
  99. // if this fires off, then increase PaddingSize
  100. // IMPORTANT: this WILL break backwards compatibility
  101. static_assert(sizeof(buffer) >= sizeof(Payload), "PaddingSize is too small");
  102. memcpy(buffer, &rhs, sizeof(Payload));
  103. return *this;
  104. }
  105. operator Payload() const
  106. {
  107. Payload result;
  108. memcpy(&result, buffer, sizeof(Payload));
  109. return result;
  110. }
  111. };
  112. OVR_RESTORE_MSVC_WARNING()
  113. #pragma pack(pop)
  114. } // namespace OVR
  115. #endif // OVR_Lockless_h