OVR_Atomic.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /************************************************************************************
  2. Filename : OVR_Atomic.cpp
  3. Content : Contains atomic operations and inline fastest locking
  4. functionality. Will contain #ifdefs for OS efficiency.
  5. Have non-thread-safe implementation if not available.
  6. Created : September 19, 2012
  7. Notes :
  8. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  9. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  10. you may not use the Oculus VR Rift SDK except in compliance with the License,
  11. which is provided at the time of installation or download, or which
  12. otherwise accompanies this software in either electronic or hard copy form.
  13. You may obtain a copy of the License at
  14. http://www.oculusvr.com/licenses/LICENSE-3.2
  15. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. ************************************************************************************/
  21. #include "OVR_Atomic.h"
  22. #include "OVR_Allocator.h"
  23. #ifdef OVR_ENABLE_THREADS
  24. // Include Windows 8-Metro compatible Synchronization API
  25. #if defined(OVR_OS_MS) && defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8)
  26. #include <synchapi.h>
  27. #endif
  28. namespace OVR {
  29. // ***** Windows Lock implementation
  30. #if defined(OVR_OS_MS)
  31. // ***** Standard Win32 Lock implementation
  32. // Constructors
  33. Lock::Lock(unsigned spinCount)
  34. {
  35. #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8)
  36. // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility
  37. InitializeCriticalSectionEx(&cs, (DWORD)spinCount,
  38. OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO));
  39. #else
  40. ::InitializeCriticalSectionAndSpinCount(&cs, (DWORD)spinCount); // This is available with WindowsXP+.
  41. #endif
  42. }
  43. Lock::~Lock()
  44. {
  45. DeleteCriticalSection(&cs);
  46. }
  47. #endif
  48. //-------------------------------------------------------------------------------------
  49. // ***** SharedLock
  50. // This is a general purpose globally shared Lock implementation that should probably be
  51. // moved to Kernel.
  52. // May in theory busy spin-wait if we hit contention on first lock creation,
  53. // but this shouldn't matter in practice since Lock* should be cached.
  54. enum { LockInitMarker = 0xFFFFFFFF };
  55. Lock* SharedLock::GetLockAddRef()
  56. {
  57. int oldUseCount;
  58. do {
  59. oldUseCount = UseCount;
  60. if (oldUseCount == (int)LockInitMarker)
  61. continue;
  62. if (oldUseCount == 0)
  63. {
  64. // Initialize marker
  65. if (UseCount.CompareAndSet_Sync(0, LockInitMarker))
  66. {
  67. Construct<Lock>(Buffer);
  68. do { }
  69. while (UseCount.CompareAndSet_Sync(LockInitMarker, 1));
  70. return toLock();
  71. }
  72. continue;
  73. }
  74. } while (!UseCount.CompareAndSet_NoSync(oldUseCount, oldUseCount + 1));
  75. return toLock();
  76. }
  77. void SharedLock::ReleaseLock(Lock* plock)
  78. {
  79. OVR_UNUSED(plock);
  80. OVR_ASSERT(plock == toLock());
  81. int oldUseCount;
  82. do {
  83. oldUseCount = UseCount;
  84. OVR_ASSERT(oldUseCount != (int)LockInitMarker);
  85. if (oldUseCount == 1)
  86. {
  87. // Initialize marker
  88. if (UseCount.CompareAndSet_Sync(1, LockInitMarker))
  89. {
  90. Destruct<Lock>(toLock());
  91. do { }
  92. while (!UseCount.CompareAndSet_Sync(LockInitMarker, 0));
  93. return;
  94. }
  95. continue;
  96. }
  97. } while (!UseCount.CompareAndSet_NoSync(oldUseCount, oldUseCount - 1));
  98. }
  99. } // OVR
  100. #endif // OVR_ENABLE_THREADS