OVR_System.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /************************************************************************************
  2. Filename : OVR_System.cpp
  3. Content : General kernel initialization/cleanup, including that
  4. of the memory allocator.
  5. Created : September 19, 2012
  6. Notes :
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #include "OVR_System.h"
  21. #include "OVR_Threads.h"
  22. #include "OVR_Timer.h"
  23. #include "OVR_DebugHelp.h"
  24. #include <new>
  25. #if defined(_MSC_VER)
  26. #include <new.h>
  27. #else
  28. #include <new>
  29. #endif
  30. #ifdef OVR_OS_MS
  31. #pragma warning(push, 0)
  32. #include "OVR_Win32_IncludeWindows.h" // GetModuleHandleEx
  33. #pragma warning(pop)
  34. #endif
  35. namespace OVR {
  36. //-----------------------------------------------------------------------------
  37. // Initialization/Shutdown state
  38. // If true, then Destroy() was called and is in the process of executing
  39. // Added to fix race condition if thread is started after the call to System::Destroy()
  40. static bool ShuttingDown = false;
  41. //-----------------------------------------------------------------------------
  42. // Initialization/Shutdown Callbacks
  43. static SystemSingletonInternal *SystemShutdownListenerList = nullptr; // Points to the most recent SystemSingletonInternal added to the list.
  44. static Lock& GetSSILock()
  45. { // Put listLock in a function so that it can be constructed on-demand.
  46. static Lock listLock; // Will construct on the first usage. However, the guarding of this construction is not thread-safe
  47. return listLock; // under all compilers. However, since we are initially calling this on startup before other threads
  48. } // could possibly exist, the first usage of this will be before other threads exist.
  49. void SystemSingletonInternal::RegisterDestroyCallback()
  50. {
  51. GetSSILock().DoLock();
  52. if (ShuttingDown)
  53. {
  54. GetSSILock().Unlock();
  55. OnThreadDestroy();
  56. }
  57. else
  58. {
  59. GetSSILock().Unlock();
  60. // Insert the listener at the front of the list (top of the stack). This is an analogue of a C++ forward_list::push_front or stack::push.
  61. NextShutdownSingleton = SystemShutdownListenerList;
  62. SystemShutdownListenerList = this;
  63. }
  64. }
  65. //-----------------------------------------------------------------------------
  66. // System
  67. static int System_Init_Count = 0;
  68. #if defined(_MSC_VER)
  69. // This allows us to throw OVR::bad_alloc instead of std::bad_alloc, which provides less information.
  70. int OVRNewFailureHandler(size_t /*size*/)
  71. {
  72. throw OVR::bad_alloc();
  73. // Disabled because otherwise a compiler warning is generated regarding unreachable code.
  74. //return 0; // A return value of 0 tells the Standard Library to not retry the allocation.
  75. }
  76. #else
  77. // This allows us to throw OVR::bad_alloc instead of std::bad_alloc, which provides less information.
  78. void OVRNewFailureHandler()
  79. {
  80. throw OVR::bad_alloc();
  81. }
  82. #endif
  83. // Initializes System core, installing allocator.
  84. void System::Init(Log* log)
  85. {
  86. #if defined(_MSC_VER)
  87. // Make it so that failure of the C malloc family of functions results in the same behavior as C++ operator new failure.
  88. // This allows us to throw exceptions for malloc usage the same as for operator new bad_alloc.
  89. _set_new_mode(1);
  90. // Tells the standard library to direct new (and malloc) failures to us. Normally we wouldn't need to do this, as the
  91. // C++ Standard Library already throws std::bad_alloc on operator new failure. The problem is that the Standard Library doesn't
  92. // throw std::bad_alloc upon malloc failure, and we can only intercept malloc failure via this means. _set_new_handler specifies
  93. // a global handler for the current running Standard Library. If the Standard Library is being dynamically linked instead
  94. // of statically linked, then this is a problem because a call to _set_new_handler would override anything the application
  95. // has already set.
  96. _set_new_handler(OVRNewFailureHandler);
  97. #else
  98. // This allows us to throw OVR::bad_alloc instead of std::bad_alloc, which provides less information.
  99. // Question: Does this set the handler for all threads or just the current thread? The C++ Standard doesn't
  100. // explicitly state this, though it may be implied from other parts of the Standard.
  101. std::set_new_handler(OVRNewFailureHandler);
  102. #endif
  103. if (++System_Init_Count == 1)
  104. {
  105. Log::SetGlobalLog(log);
  106. Timer::initializeTimerSystem();
  107. }
  108. else
  109. {
  110. OVR_DEBUG_LOG(("[System] Init recursively called; depth = %d", System_Init_Count));
  111. }
  112. }
  113. void System::Destroy()
  114. {
  115. GetSSILock().DoLock();
  116. ShuttingDown = true;
  117. GetSSILock().Unlock();
  118. if (--System_Init_Count == 0)
  119. {
  120. // Invoke all of the post-finish callbacks (normal case)
  121. for (SystemSingletonInternal *listener = SystemShutdownListenerList; listener; listener = listener->NextShutdownSingleton)
  122. {
  123. listener->OnThreadDestroy();
  124. }
  125. #ifdef OVR_ENABLE_THREADS
  126. // Wait for all threads to finish; this must be done so that memory
  127. // allocator and all destructors finalize correctly.
  128. Thread::FinishAllThreads();
  129. #endif
  130. // Invoke all of the post-finish callbacks (normal case)
  131. for (SystemSingletonInternal* next, *listener = SystemShutdownListenerList; listener; listener = next)
  132. {
  133. next = listener->NextShutdownSingleton;
  134. listener->OnSystemDestroy();
  135. }
  136. SystemShutdownListenerList = nullptr;
  137. Timer::shutdownTimerSystem();
  138. Log::SetGlobalLog(Log::GetDefaultLog());
  139. if (Allocator::IsTrackingLeaks())
  140. {
  141. int ovrLeakCount = Allocator::DumpMemory();
  142. OVR_ASSERT(ovrLeakCount == 0);
  143. if (ovrLeakCount == 0)
  144. {
  145. OVR_DEBUG_LOG(("[System] No OVR object leaks detected."));
  146. }
  147. }
  148. }
  149. else
  150. {
  151. OVR_DEBUG_LOG(("[System] Destroy recursively called; depth = %d", System_Init_Count));
  152. }
  153. GetSSILock().DoLock();
  154. ShuttingDown = false;
  155. GetSSILock().Unlock();
  156. }
  157. // Returns 'true' if system was properly initialized.
  158. bool System::IsInitialized()
  159. {
  160. return System_Init_Count > 0;
  161. }
  162. } // namespace OVR