Bug 1495871 - use C++11 statics in CamerasChild; r=jesup
authorNathan Froyd <froydnj@mozilla.com>
Fri, 05 Oct 2018 13:43:47 -0400
changeset 495610 cdd41eaece0f8c55fe2f32740e1f0b292928c071
parent 495609 2296a2feb192ccf56f118999e1a6ec9fa9f36a34
child 495611 6fc7db6d7326789e53cc0c82189f2196f56e78ca
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs1495871
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1495871 - use C++11 statics in CamerasChild; r=jesup C++11 provides guaranteed thread-safe static initialization, so we can use that instead of ipc's baroque Singleton class.
dom/media/systemservices/CamerasChild.h
--- a/dom/media/systemservices/CamerasChild.h
+++ b/dom/media/systemservices/CamerasChild.h
@@ -9,17 +9,16 @@
 
 #include "mozilla/Move.h"
 #include "mozilla/Pair.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/camera/PCamerasChild.h"
 #include "mozilla/camera/PCamerasParent.h"
 #include "mozilla/media/DeviceChangeCallback.h"
 #include "mozilla/Mutex.h"
-#include "base/singleton.h"
 #include "nsCOMPtr.h"
 
 // conflicts with #include of scoped_ptr.h
 #undef FF
 #include "webrtc/modules/video_capture/video_capture_defines.h"
 
 namespace mozilla {
 
@@ -65,48 +64,51 @@ template <class T> class LockAndDispatch
 // async messaging. We dispatch the messages to another thread to send them
 // async and hold a Monitor to wait for the result to be asynchronously received
 // again. The requirement for async messaging originates on the parent side:
 // it's not reasonable to block all PBackground IPC there while waiting for
 // something like device enumeration to complete.
 
 class CamerasSingleton {
 public:
-  CamerasSingleton();
-  ~CamerasSingleton();
-
   static OffTheBooksMutex& Mutex() {
-    return Singleton<mozilla::camera::CamerasSingleton>::get()->mCamerasMutex;
+    return singleton().mCamerasMutex;
   }
 
   static CamerasChild*& Child() {
     Mutex().AssertCurrentThreadOwns();
-    return Singleton<mozilla::camera::CamerasSingleton>::get()->mCameras;
+    return singleton().mCameras;
   }
 
   static nsCOMPtr<nsIThread>& Thread() {
     Mutex().AssertCurrentThreadOwns();
-    return Singleton<mozilla::camera::CamerasSingleton>::get()->mCamerasChildThread;
+    return singleton().mCamerasChildThread;
   }
 
   static nsCOMPtr<nsIThread>& FakeDeviceChangeEventThread() {
     Mutex().AssertCurrentThreadOwns();
-    return Singleton<mozilla::camera::CamerasSingleton>::get()->mFakeDeviceChangeEventThread;
+    return singleton().mFakeDeviceChangeEventThread;
   }
 
   static bool InShutdown() {
-    return gTheInstance.get()->mInShutdown;
+    return singleton().mInShutdown;
   }
 
   static void StartShutdown() {
-    gTheInstance.get()->mInShutdown = true;
+    singleton().mInShutdown = true;
   }
 
 private:
-  static Singleton<CamerasSingleton> gTheInstance;
+  CamerasSingleton();
+  ~CamerasSingleton();
+
+  static CamerasSingleton& singleton() {
+    static CamerasSingleton camera;
+    return camera;
+  }
 
   // Reinitializing CamerasChild will change the pointers below.
   // We don't want this to happen in the middle of preparing IPC.
   // We will be alive on destruction, so this needs to be off the books.
   mozilla::OffTheBooksMutex mCamerasMutex;
 
   // This is owned by the IPC code, and the same code controls the lifetime.
   // It will set and clear this pointer as appropriate in setup/teardown.