nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: PublicHeader: OVR nuclear@0: Filename : OVR_System.h nuclear@0: Content : General kernel initialization/cleanup, including that nuclear@0: of the memory allocator. 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: #ifndef OVR_System_h nuclear@0: #define OVR_System_h nuclear@0: nuclear@0: #include "OVR_Allocator.h" nuclear@0: #include "OVR_Log.h" nuclear@0: #include "OVR_Atomic.h" nuclear@0: nuclear@0: namespace OVR { nuclear@0: nuclear@0: nuclear@0: //----------------------------------------------------------------------------- nuclear@0: // SystemSingleton nuclear@0: nuclear@0: // Subsystems are implemented using the Singleton pattern. nuclear@0: // To avoid code duplication in all the places where Singletons are defined, nuclear@0: // The pattern is defined once here and used everywhere. nuclear@0: nuclear@0: class SystemSingletonInternal nuclear@0: { nuclear@0: friend class System; nuclear@0: nuclear@0: SystemSingletonInternal* NextSingleton; nuclear@0: nuclear@0: // No copying allowed nuclear@0: OVR_NON_COPYABLE(SystemSingletonInternal); nuclear@0: nuclear@0: protected: nuclear@0: SystemSingletonInternal() : nuclear@0: NextSingleton(0) nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: virtual ~SystemSingletonInternal(){} nuclear@0: nuclear@0: // Call this to register the destroy events nuclear@0: // Destroy callbacks will be called in the reverse order they were registered nuclear@0: // Note: As a rule of thumb, call this at the end of the singleton class constructor. nuclear@0: void PushDestroyCallbacks(); nuclear@0: nuclear@0: // Required: Invoked when the System object is shutting down nuclear@0: // Called after threads are stopped nuclear@0: // Called before Log, Allocator, and Timer subsystems are stopped nuclear@0: // Listeners are called in the opposite order they were registered nuclear@0: virtual void OnSystemDestroy() = 0; nuclear@0: nuclear@0: // Called just before waiting for threads to die nuclear@0: // Listeners are called in the opposite order they were registered nuclear@0: // Useful to start terminating threads at the right time nuclear@0: // Note: The singleton must not delete itself here. nuclear@0: virtual void OnThreadDestroy() {} nuclear@0: }; nuclear@0: nuclear@0: // Singletons derive from this class nuclear@0: template nuclear@0: class SystemSingletonBase : public SystemSingletonInternal nuclear@0: { nuclear@0: static AtomicPtr SingletonInstance; nuclear@0: static T* SlowGetInstance(); nuclear@0: nuclear@0: protected: nuclear@0: ~SystemSingletonBase() nuclear@0: { nuclear@0: // Make sure the instance gets set to zero on dtor nuclear@0: if (SingletonInstance == this) nuclear@0: SingletonInstance = 0; nuclear@0: } nuclear@0: nuclear@0: public: nuclear@0: static OVR_FORCE_INLINE T* GetInstance() nuclear@0: { nuclear@0: // Fast version nuclear@0: // Note: The singleton instance is stored in an AtomicPtr<> to allow it to be accessed nuclear@0: // atomically from multiple threads without locks. nuclear@0: T* instance = SingletonInstance; nuclear@0: return instance ? instance : SlowGetInstance(); nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: // For reference, see N3337 14.5.1.3 (Static data members of class templates): nuclear@0: template OVR::AtomicPtr OVR::SystemSingletonBase::SingletonInstance; nuclear@0: nuclear@0: // Place this in the singleton class in the header file nuclear@0: #define OVR_DECLARE_SINGLETON(T) \ nuclear@0: friend class OVR::SystemSingletonBase; \ nuclear@0: private: \ nuclear@0: T(); \ nuclear@0: virtual ~T(); \ nuclear@0: virtual void OnSystemDestroy(); nuclear@0: nuclear@0: // Place this in the singleton class source file nuclear@0: #define OVR_DEFINE_SINGLETON(T) \ nuclear@0: namespace OVR { \ nuclear@0: template<> T* SystemSingletonBase::SlowGetInstance() \ nuclear@0: { \ nuclear@0: static OVR::Lock lock; \ nuclear@0: OVR::Lock::Locker locker(&lock); \ nuclear@0: if (!SingletonInstance) SingletonInstance = new T; \ nuclear@0: return SingletonInstance; \ nuclear@0: } \ nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // ***** System Core Initialization class nuclear@0: nuclear@0: // System initialization must take place before any other OVR_Kernel objects are used; nuclear@0: // this is done my calling System::Init(). Among other things, this is necessary to nuclear@0: // initialize the memory allocator. Similarly, System::Destroy must be nuclear@0: // called before program exist for proper cleanup. Both of these tasks can be achieved by nuclear@0: // simply creating System object first, allowing its constructor/destructor do the work. nuclear@0: nuclear@0: // TBD: Require additional System class for Oculus Rift API? nuclear@0: nuclear@0: class System nuclear@0: { nuclear@0: public: nuclear@0: // System constructor expects allocator to be specified, if it is being substituted. nuclear@0: System(Log* log = Log::ConfigureDefaultLog(LogMask_Debug), nuclear@0: Allocator* palloc = DefaultAllocator::InitSystemSingleton()) nuclear@0: { nuclear@0: Init(log, palloc); nuclear@0: } nuclear@0: ~System() nuclear@0: { nuclear@0: Destroy(); nuclear@0: } nuclear@0: nuclear@0: static void OVR_CDECL DirectDisplayInitialize(); nuclear@0: static bool OVR_CDECL DirectDisplayEnabled(); nuclear@0: nuclear@0: // Returns 'true' if system was properly initialized. nuclear@0: static bool OVR_CDECL IsInitialized(); nuclear@0: nuclear@0: // Initializes System core. Users can override memory implementation by passing nuclear@0: // a different Allocator here. nuclear@0: static void OVR_CDECL Init(Log* log = Log::ConfigureDefaultLog(LogMask_Debug), nuclear@0: Allocator *palloc = DefaultAllocator::InitSystemSingleton()); nuclear@0: nuclear@0: // De-initializes System more, finalizing the threading system and destroying nuclear@0: // the global memory allocator. nuclear@0: static void OVR_CDECL Destroy(); nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: } // namespace OVR nuclear@0: nuclear@0: #endif