oculus1

annotate libovr/Src/Kernel/OVR_Atomic.cpp @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children b069a5c27388
rev   line source
nuclear@1 1 /************************************************************************************
nuclear@1 2
nuclear@1 3 Filename : OVR_Atomic.cpp
nuclear@1 4 Content : Contains atomic operations and inline fastest locking
nuclear@1 5 functionality. Will contain #ifdefs for OS efficiency.
nuclear@1 6 Have non-thread-safe implementation if not available.
nuclear@1 7 Created : September 19, 2012
nuclear@1 8 Notes :
nuclear@1 9
nuclear@1 10 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@1 11
nuclear@1 12 Use of this software is subject to the terms of the Oculus license
nuclear@1 13 agreement provided at the time of installation or download, or which
nuclear@1 14 otherwise accompanies this software in either electronic or hard copy form.
nuclear@1 15
nuclear@1 16 ************************************************************************************/
nuclear@1 17
nuclear@1 18 #include "OVR_Atomic.h"
nuclear@1 19
nuclear@1 20 #ifdef OVR_ENABLE_THREADS
nuclear@1 21
nuclear@1 22 // Include Windows 8-Metro compatible Synchronization API
nuclear@1 23 #if defined(OVR_OS_WIN32) && defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8)
nuclear@1 24 #include <synchapi.h>
nuclear@1 25 #endif
nuclear@1 26
nuclear@1 27
nuclear@1 28 namespace OVR {
nuclear@1 29
nuclear@1 30 // ***** Windows Lock implementation
nuclear@1 31
nuclear@1 32 #if defined(OVR_OS_WIN32)
nuclear@1 33
nuclear@1 34 // ***** Standard Win32 Lock implementation
nuclear@1 35
nuclear@1 36 // Constructors
nuclear@1 37 Lock::Lock(unsigned spinCount)
nuclear@1 38 {
nuclear@1 39 #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8)
nuclear@1 40 // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility
nuclear@1 41 InitializeCriticalSectionEx(&cs, spinCount,
nuclear@1 42 OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO));
nuclear@1 43 #else
nuclear@1 44 // Spin count init critical section function prototype for Window NT
nuclear@1 45 typedef BOOL (WINAPI *Function_InitializeCriticalSectionAndSpinCount)
nuclear@1 46 (LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount);
nuclear@1 47
nuclear@1 48
nuclear@1 49 // Try to load function dynamically so that we don't require NT
nuclear@1 50 // On Windows NT we will use InitializeCriticalSectionAndSpinCount
nuclear@1 51 static bool initTried = 0;
nuclear@1 52 static Function_InitializeCriticalSectionAndSpinCount pInitFn = 0;
nuclear@1 53
nuclear@1 54 if (!initTried)
nuclear@1 55 {
nuclear@1 56 HMODULE hmodule = ::LoadLibrary(OVR_STR("kernel32.dll"));
nuclear@1 57 pInitFn = (Function_InitializeCriticalSectionAndSpinCount)
nuclear@1 58 ::GetProcAddress(hmodule, "InitializeCriticalSectionAndSpinCount");
nuclear@1 59 initTried = true;
nuclear@1 60 }
nuclear@1 61
nuclear@1 62 // Initialize the critical section
nuclear@1 63 if (pInitFn)
nuclear@1 64 pInitFn(&cs, spinCount);
nuclear@1 65 else
nuclear@1 66 ::InitializeCriticalSection(&cs);
nuclear@1 67 #endif
nuclear@1 68
nuclear@1 69 }
nuclear@1 70
nuclear@1 71
nuclear@1 72 Lock::~Lock()
nuclear@1 73 {
nuclear@1 74 DeleteCriticalSection(&cs);
nuclear@1 75 }
nuclear@1 76
nuclear@1 77
nuclear@1 78 #endif
nuclear@1 79
nuclear@1 80 } // OVR
nuclear@1 81
nuclear@1 82 #endif // OVR_ENABLE_THREADS