nuclear@1: /************************************************************************************ nuclear@1: nuclear@1: Filename : OVR_Atomic.cpp nuclear@1: Content : Contains atomic operations and inline fastest locking nuclear@1: functionality. Will contain #ifdefs for OS efficiency. nuclear@1: Have non-thread-safe implementation if not available. nuclear@1: Created : September 19, 2012 nuclear@1: Notes : nuclear@1: nuclear@1: Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. nuclear@1: nuclear@1: Use of this software is subject to the terms of the Oculus license nuclear@1: agreement provided at the time of installation or download, or which nuclear@1: otherwise accompanies this software in either electronic or hard copy form. nuclear@1: nuclear@1: ************************************************************************************/ nuclear@1: nuclear@1: #include "OVR_Atomic.h" nuclear@1: nuclear@1: #ifdef OVR_ENABLE_THREADS nuclear@1: nuclear@1: // Include Windows 8-Metro compatible Synchronization API nuclear@1: #if defined(OVR_OS_WIN32) && defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8) nuclear@1: #include nuclear@1: #endif nuclear@1: nuclear@1: nuclear@1: namespace OVR { nuclear@1: nuclear@1: // ***** Windows Lock implementation nuclear@1: nuclear@1: #if defined(OVR_OS_WIN32) nuclear@1: nuclear@1: // ***** Standard Win32 Lock implementation nuclear@1: nuclear@1: // Constructors nuclear@1: Lock::Lock(unsigned spinCount) nuclear@1: { nuclear@1: #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8) nuclear@1: // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility nuclear@1: InitializeCriticalSectionEx(&cs, spinCount, nuclear@1: OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO)); nuclear@1: #else nuclear@1: // Spin count init critical section function prototype for Window NT nuclear@1: typedef BOOL (WINAPI *Function_InitializeCriticalSectionAndSpinCount) nuclear@1: (LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount); nuclear@1: nuclear@1: nuclear@1: // Try to load function dynamically so that we don't require NT nuclear@1: // On Windows NT we will use InitializeCriticalSectionAndSpinCount nuclear@1: static bool initTried = 0; nuclear@1: static Function_InitializeCriticalSectionAndSpinCount pInitFn = 0; nuclear@1: nuclear@1: if (!initTried) nuclear@1: { nuclear@1: HMODULE hmodule = ::LoadLibrary(OVR_STR("kernel32.dll")); nuclear@1: pInitFn = (Function_InitializeCriticalSectionAndSpinCount) nuclear@1: ::GetProcAddress(hmodule, "InitializeCriticalSectionAndSpinCount"); nuclear@1: initTried = true; nuclear@1: } nuclear@1: nuclear@1: // Initialize the critical section nuclear@1: if (pInitFn) nuclear@1: pInitFn(&cs, spinCount); nuclear@1: else nuclear@1: ::InitializeCriticalSection(&cs); nuclear@1: #endif nuclear@1: nuclear@1: } nuclear@1: nuclear@1: nuclear@1: Lock::~Lock() nuclear@1: { nuclear@1: DeleteCriticalSection(&cs); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: #endif nuclear@1: nuclear@1: } // OVR nuclear@1: nuclear@1: #endif // OVR_ENABLE_THREADS