nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: Filename : OVR_Atomic.cpp nuclear@0: Content : Contains atomic operations and inline fastest locking nuclear@0: functionality. Will contain #ifdefs for OS efficiency. nuclear@0: Have non-thread-safe implementation if not available. nuclear@0: Created : September 19, 2012 nuclear@0: Notes : nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #include "OVR_Atomic.h" nuclear@0: #include "OVR_Allocator.h" nuclear@0: nuclear@0: #ifdef OVR_ENABLE_THREADS nuclear@0: nuclear@0: // Include Windows 8-Metro compatible Synchronization API nuclear@0: #if defined(OVR_OS_MS) && defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8) nuclear@0: #include nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: namespace OVR { nuclear@0: nuclear@0: // ***** Windows Lock implementation nuclear@0: nuclear@0: #if defined(OVR_OS_MS) nuclear@0: nuclear@0: // ***** Standard Win32 Lock implementation nuclear@0: nuclear@0: // Constructors nuclear@0: Lock::Lock(unsigned spinCount) nuclear@0: { nuclear@0: #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8) nuclear@0: // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility nuclear@0: InitializeCriticalSectionEx(&cs, (DWORD)spinCount, nuclear@0: OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO)); nuclear@0: #else nuclear@0: ::InitializeCriticalSectionAndSpinCount(&cs, (DWORD)spinCount); // This is available with WindowsXP+. nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: nuclear@0: Lock::~Lock() nuclear@0: { nuclear@0: DeleteCriticalSection(&cs); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: //------------------------------------------------------------------------------------- nuclear@0: // ***** SharedLock nuclear@0: nuclear@0: // This is a general purpose globally shared Lock implementation that should probably be nuclear@0: // moved to Kernel. nuclear@0: // May in theory busy spin-wait if we hit contention on first lock creation, nuclear@0: // but this shouldn't matter in practice since Lock* should be cached. nuclear@0: nuclear@0: nuclear@0: enum { LockInitMarker = 0xFFFFFFFF }; nuclear@0: nuclear@0: Lock* SharedLock::GetLockAddRef() nuclear@0: { nuclear@0: int oldUseCount; nuclear@0: nuclear@0: do { nuclear@0: oldUseCount = UseCount; nuclear@0: if (oldUseCount == (int)LockInitMarker) nuclear@0: continue; nuclear@0: nuclear@0: if (oldUseCount == 0) nuclear@0: { nuclear@0: // Initialize marker nuclear@0: if (AtomicOps::CompareAndSet_Sync(&UseCount, 0, LockInitMarker)) nuclear@0: { nuclear@0: Construct(Buffer); nuclear@0: do { } nuclear@0: while (!AtomicOps::CompareAndSet_Sync(&UseCount, LockInitMarker, 1)); nuclear@0: return toLock(); nuclear@0: } nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: } while (!AtomicOps::CompareAndSet_NoSync(&UseCount, oldUseCount, oldUseCount + 1)); nuclear@0: nuclear@0: return toLock(); nuclear@0: } nuclear@0: nuclear@0: void SharedLock::ReleaseLock(Lock* plock) nuclear@0: { nuclear@0: OVR_UNUSED(plock); nuclear@0: OVR_ASSERT(plock == toLock()); nuclear@0: nuclear@0: int oldUseCount; nuclear@0: nuclear@0: do { nuclear@0: oldUseCount = UseCount; nuclear@0: OVR_ASSERT(oldUseCount != (int)LockInitMarker); nuclear@0: nuclear@0: if (oldUseCount == 1) nuclear@0: { nuclear@0: // Initialize marker nuclear@0: if (AtomicOps::CompareAndSet_Sync(&UseCount, 1, LockInitMarker)) nuclear@0: { nuclear@0: Destruct(toLock()); nuclear@0: nuclear@0: do { } nuclear@0: while (!AtomicOps::CompareAndSet_Sync(&UseCount, LockInitMarker, 0)); nuclear@0: nuclear@0: return; nuclear@0: } nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: } while (!AtomicOps::CompareAndSet_NoSync(&UseCount, oldUseCount, oldUseCount - 1)); nuclear@0: } nuclear@0: nuclear@0: } // OVR nuclear@0: nuclear@0: #endif // OVR_ENABLE_THREADS