diff options
Diffstat (limited to 'LibOVR/Src/Kernel/OVR_Lockless.h')
-rw-r--r-- | LibOVR/Src/Kernel/OVR_Lockless.h | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/LibOVR/Src/Kernel/OVR_Lockless.h b/LibOVR/Src/Kernel/OVR_Lockless.h index a12f824..4d5b87e 100644 --- a/LibOVR/Src/Kernel/OVR_Lockless.h +++ b/LibOVR/Src/Kernel/OVR_Lockless.h @@ -1,21 +1,21 @@ /************************************************************************************ -PublicHeader: OVR.h +PublicHeader: OVR_Kernel.h Filename : OVR_Lockless.h Content : Lock-less classes for producer/consumer communication Created : November 9, 2013 Authors : John Carmack -Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved. +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. -Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); you may not use the Oculus VR Rift SDK except in compliance with the License, which is provided at the time of installation or download, or which otherwise accompanies this software in either electronic or hard copy form. You may obtain a copy of the License at -http://www.oculusvr.com/licenses/LICENSE-3.1 +http://www.oculusvr.com/licenses/LICENSE-3.2 Unless required by applicable law or agreed to in writing, the Oculus VR SDK distributed under the License is distributed on an "AS IS" BASIS, @@ -42,14 +42,24 @@ namespace OVR { // necessarily getting every one that happens (vsync timing, SensorFusion updates). // // This is multiple consumer safe, but is currently only used with a single consumer. +// +// The SlotType can be the same as T, but should probably be a larger fixed size. +// This allows for forward compatibility when the updater is shared between processes. + +// FIXME: ExchangeAdd_Sync() should be replaced with a portable read-only primitive, +// so that the lockless pose state can be read-only on remote processes and to reduce +// false sharing between processes and improve performance. -template<class T> +template<class T, class SlotType> class LocklessUpdater { public: - LocklessUpdater() : UpdateBegin( 0 ), UpdateEnd( 0 ) {} + LocklessUpdater() : UpdateBegin( 0 ), UpdateEnd( 0 ) + { + OVR_COMPILER_ASSERT(sizeof(T) <= sizeof(SlotType)); + } - T GetState() const + T GetState() const { // Copy the state out, then retry with the alternate slot // if we determine that our copy may have been partially @@ -61,9 +71,9 @@ public: { // We are adding 0, only using these as atomic memory barriers, so it // is ok to cast off the const, allowing GetState() to remain const. - end = UpdateEnd.ExchangeAdd_Sync(0); + end = UpdateEnd.Load_Acquire(); state = Slots[ end & 1 ]; - begin = UpdateBegin.ExchangeAdd_Sync(0); + begin = UpdateBegin.Load_Acquire(); if ( begin == end ) { break; } @@ -71,7 +81,7 @@ public: // The producer is potentially blocked while only having partially // written the update, so copy out the other slot. state = Slots[ (begin & 1) ^ 1 ]; - final = UpdateBegin.ExchangeAdd_NoSync(0); + final = UpdateBegin.Load_Acquire(); if ( final == begin ) { break; } @@ -82,7 +92,7 @@ public: return state; } - void SetState( T state ) + void SetState( const T& state ) { const int slot = UpdateBegin.ExchangeAdd_Sync(1) & 1; // Write to (slot ^ 1) because ExchangeAdd returns 'previous' value before add. @@ -90,9 +100,9 @@ public: UpdateEnd.ExchangeAdd_Sync(1); } - mutable AtomicInt<int> UpdateBegin; - mutable AtomicInt<int> UpdateEnd; - T Slots[2]; + AtomicInt<int> UpdateBegin; + AtomicInt<int> UpdateEnd; + SlotType Slots[2]; }; |