ovr_sdk

view LibOVR/Src/Kernel/OVR_Atomic.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line source
1 /************************************************************************************
3 Filename : OVR_Atomic.cpp
4 Content : Contains atomic operations and inline fastest locking
5 functionality. Will contain #ifdefs for OS efficiency.
6 Have non-thread-safe implementation if not available.
7 Created : September 19, 2012
8 Notes :
10 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
12 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
13 you may not use the Oculus VR Rift SDK except in compliance with the License,
14 which is provided at the time of installation or download, or which
15 otherwise accompanies this software in either electronic or hard copy form.
17 You may obtain a copy of the License at
19 http://www.oculusvr.com/licenses/LICENSE-3.2
21 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
22 distributed under the License is distributed on an "AS IS" BASIS,
23 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 See the License for the specific language governing permissions and
25 limitations under the License.
27 ************************************************************************************/
29 #include "OVR_Atomic.h"
30 #include "OVR_Allocator.h"
32 #ifdef OVR_ENABLE_THREADS
34 // Include Windows 8-Metro compatible Synchronization API
35 #if defined(OVR_OS_MS) && defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8)
36 #include <synchapi.h>
37 #endif
40 namespace OVR {
42 // ***** Windows Lock implementation
44 #if defined(OVR_OS_MS)
46 // ***** Standard Win32 Lock implementation
48 // Constructors
49 Lock::Lock(unsigned spinCount)
50 {
51 #if defined(NTDDI_WIN8) && (NTDDI_VERSION >= NTDDI_WIN8)
52 // On Windows 8 we use InitializeCriticalSectionEx due to Metro-Compatibility
53 InitializeCriticalSectionEx(&cs, (DWORD)spinCount,
54 OVR_DEBUG_SELECT(NULL, CRITICAL_SECTION_NO_DEBUG_INFO));
55 #else
56 ::InitializeCriticalSectionAndSpinCount(&cs, (DWORD)spinCount); // This is available with WindowsXP+.
57 #endif
58 }
61 Lock::~Lock()
62 {
63 DeleteCriticalSection(&cs);
64 }
67 #endif
70 //-------------------------------------------------------------------------------------
71 // ***** SharedLock
73 // This is a general purpose globally shared Lock implementation that should probably be
74 // moved to Kernel.
75 // May in theory busy spin-wait if we hit contention on first lock creation,
76 // but this shouldn't matter in practice since Lock* should be cached.
79 enum { LockInitMarker = 0xFFFFFFFF };
81 Lock* SharedLock::GetLockAddRef()
82 {
83 int oldUseCount;
85 do {
86 oldUseCount = UseCount;
87 if (oldUseCount == (int)LockInitMarker)
88 continue;
90 if (oldUseCount == 0)
91 {
92 // Initialize marker
93 if (AtomicOps<int>::CompareAndSet_Sync(&UseCount, 0, LockInitMarker))
94 {
95 Construct<Lock>(Buffer);
96 do { }
97 while (!AtomicOps<int>::CompareAndSet_Sync(&UseCount, LockInitMarker, 1));
98 return toLock();
99 }
100 continue;
101 }
103 } while (!AtomicOps<int>::CompareAndSet_NoSync(&UseCount, oldUseCount, oldUseCount + 1));
105 return toLock();
106 }
108 void SharedLock::ReleaseLock(Lock* plock)
109 {
110 OVR_UNUSED(plock);
111 OVR_ASSERT(plock == toLock());
113 int oldUseCount;
115 do {
116 oldUseCount = UseCount;
117 OVR_ASSERT(oldUseCount != (int)LockInitMarker);
119 if (oldUseCount == 1)
120 {
121 // Initialize marker
122 if (AtomicOps<int>::CompareAndSet_Sync(&UseCount, 1, LockInitMarker))
123 {
124 Destruct<Lock>(toLock());
126 do { }
127 while (!AtomicOps<int>::CompareAndSet_Sync(&UseCount, LockInitMarker, 0));
129 return;
130 }
131 continue;
132 }
134 } while (!AtomicOps<int>::CompareAndSet_NoSync(&UseCount, oldUseCount, oldUseCount - 1));
135 }
137 } // OVR
139 #endif // OVR_ENABLE_THREADS