ovr_sdk

view LibOVR/Src/Kernel/OVR_System.h @ 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 PublicHeader: OVR
4 Filename : OVR_System.h
5 Content : General kernel initialization/cleanup, including that
6 of the memory allocator.
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 #ifndef OVR_System_h
30 #define OVR_System_h
32 #include "OVR_Allocator.h"
33 #include "OVR_Log.h"
34 #include "OVR_Atomic.h"
36 namespace OVR {
39 //-----------------------------------------------------------------------------
40 // SystemSingleton
42 // Subsystems are implemented using the Singleton pattern.
43 // To avoid code duplication in all the places where Singletons are defined,
44 // The pattern is defined once here and used everywhere.
46 class SystemSingletonInternal
47 {
48 friend class System;
50 SystemSingletonInternal* NextSingleton;
52 // No copying allowed
53 OVR_NON_COPYABLE(SystemSingletonInternal);
55 protected:
56 SystemSingletonInternal() :
57 NextSingleton(0)
58 {
59 }
61 virtual ~SystemSingletonInternal(){}
63 // Call this to register the destroy events
64 // Destroy callbacks will be called in the reverse order they were registered
65 // Note: As a rule of thumb, call this at the end of the singleton class constructor.
66 void PushDestroyCallbacks();
68 // Required: Invoked when the System object is shutting down
69 // Called after threads are stopped
70 // Called before Log, Allocator, and Timer subsystems are stopped
71 // Listeners are called in the opposite order they were registered
72 virtual void OnSystemDestroy() = 0;
74 // Called just before waiting for threads to die
75 // Listeners are called in the opposite order they were registered
76 // Useful to start terminating threads at the right time
77 // Note: The singleton must not delete itself here.
78 virtual void OnThreadDestroy() {}
79 };
81 // Singletons derive from this class
82 template<class T>
83 class SystemSingletonBase : public SystemSingletonInternal
84 {
85 static AtomicPtr<T> SingletonInstance;
86 static T* SlowGetInstance();
88 protected:
89 ~SystemSingletonBase()
90 {
91 // Make sure the instance gets set to zero on dtor
92 if (SingletonInstance == this)
93 SingletonInstance = 0;
94 }
96 public:
97 static OVR_FORCE_INLINE T* GetInstance()
98 {
99 // Fast version
100 // Note: The singleton instance is stored in an AtomicPtr<> to allow it to be accessed
101 // atomically from multiple threads without locks.
102 T* instance = SingletonInstance;
103 return instance ? instance : SlowGetInstance();
104 }
105 };
107 // For reference, see N3337 14.5.1.3 (Static data members of class templates):
108 template<class T> OVR::AtomicPtr<T> OVR::SystemSingletonBase<T>::SingletonInstance;
110 // Place this in the singleton class in the header file
111 #define OVR_DECLARE_SINGLETON(T) \
112 friend class OVR::SystemSingletonBase<T>; \
113 private: \
114 T(); \
115 virtual ~T(); \
116 virtual void OnSystemDestroy();
118 // Place this in the singleton class source file
119 #define OVR_DEFINE_SINGLETON(T) \
120 namespace OVR { \
121 template<> T* SystemSingletonBase<T>::SlowGetInstance() \
122 { \
123 static OVR::Lock lock; \
124 OVR::Lock::Locker locker(&lock); \
125 if (!SingletonInstance) SingletonInstance = new T; \
126 return SingletonInstance; \
127 } \
128 }
131 // ***** System Core Initialization class
133 // System initialization must take place before any other OVR_Kernel objects are used;
134 // this is done my calling System::Init(). Among other things, this is necessary to
135 // initialize the memory allocator. Similarly, System::Destroy must be
136 // called before program exist for proper cleanup. Both of these tasks can be achieved by
137 // simply creating System object first, allowing its constructor/destructor do the work.
139 // TBD: Require additional System class for Oculus Rift API?
141 class System
142 {
143 public:
144 // System constructor expects allocator to be specified, if it is being substituted.
145 System(Log* log = Log::ConfigureDefaultLog(LogMask_Debug),
146 Allocator* palloc = DefaultAllocator::InitSystemSingleton())
147 {
148 Init(log, palloc);
149 }
150 ~System()
151 {
152 Destroy();
153 }
155 static void OVR_CDECL DirectDisplayInitialize();
156 static bool OVR_CDECL DirectDisplayEnabled();
158 // Returns 'true' if system was properly initialized.
159 static bool OVR_CDECL IsInitialized();
161 // Initializes System core. Users can override memory implementation by passing
162 // a different Allocator here.
163 static void OVR_CDECL Init(Log* log = Log::ConfigureDefaultLog(LogMask_Debug),
164 Allocator *palloc = DefaultAllocator::InitSystemSingleton());
166 // De-initializes System more, finalizing the threading system and destroying
167 // the global memory allocator.
168 static void OVR_CDECL Destroy();
169 };
172 } // namespace OVR
174 #endif