Bug 1314787 - Remove function-static race from DecoderAllocPolicy::Instance(). r=gerald
authorJW Wang <jwwang@mozilla.com>
Thu, 03 Nov 2016 09:56:17 +0800
changeset 347477 e5c6eb67bb0d6d627e783404860a4bc30758a697
parent 347476 6efd141313d472eda740931b77a2167f7eae2b4b
child 347478 c7a9f3fd2ca60ecafc8b628fa3a76e23952b9b1f
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgerald
bugs1314787
milestone52.0a1
Bug 1314787 - Remove function-static race from DecoderAllocPolicy::Instance(). r=gerald MozReview-Commit-ID: 7XfaUZo3oh9
dom/media/MediaFormatReader.cpp
--- a/dom/media/MediaFormatReader.cpp
+++ b/dom/media/MediaFormatReader.cpp
@@ -76,23 +76,28 @@ private:
   using PromisePrivate = Promise::Private;
   DecoderAllocPolicy();
   ~DecoderAllocPolicy();
   // Called by the destructor of TokenImpl to restore the decoder limit.
   void Dealloc();
   // Decrement the decoder limit and resolve a promise if available.
   void ResolvePromise(ReentrantMonitorAutoEnter& aProofOfLock);
 
+  // Protect access to Instance().
+  static StaticMutex sMutex;
+
   ReentrantMonitor mMonitor;
   // The number of decoders available for creation.
   int mDecoderLimit;
   // Requests to acquire tokens.
   std::queue<RefPtr<PromisePrivate>> mPromises;
 };
 
+StaticMutex DecoderAllocPolicy::sMutex;
+
 class DecoderAllocPolicy::AutoDeallocToken : public Token
 {
 private:
   ~AutoDeallocToken() { DecoderAllocPolicy::Instance().Dealloc(); }
 };
 
 DecoderAllocPolicy::DecoderAllocPolicy()
   : mMonitor("DecoderAllocPolicy::mMonitor")
@@ -110,18 +115,17 @@ DecoderAllocPolicy::~DecoderAllocPolicy(
     mPromises.pop();
     p->Reject(true, __func__);
   }
 }
 
 DecoderAllocPolicy&
 DecoderAllocPolicy::Instance()
 {
-  // Note: Function-static initialization is not thread-safe in VS 2013.
-  // Don't uplift this code to 45esr.
+  StaticMutexAutoLock lock(sMutex);
   static auto sPolicy = new DecoderAllocPolicy();
   return *sPolicy;
 }
 
 auto
 DecoderAllocPolicy::Alloc(TrackType aTrack) -> RefPtr<Promise>
 {
   // No limit for audio decoders or a negative number.