Bug 1426061. P2 - offload MediaCacheStream::Close() to another thread. r=bechen,gerald
authorJW Wang <jwwang@mozilla.com>
Fri, 15 Dec 2017 10:29:29 +0800
changeset 448738 f58dca05f26f69116bf5a736d384e97963beec61
parent 448737 26dcfbdd90803904986d82eebb70ec85d09d0ff9
child 448739 15d8b89eb6658f627fe5538ca925fb2627be7556
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbechen, gerald
bugs1426061
milestone59.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 1426061. P2 - offload MediaCacheStream::Close() to another thread. r=bechen,gerald So we won't take the cache lock on the main thread. MozReview-Commit-ID: KYSB0vonOZ2
dom/media/MediaCache.cpp
dom/media/MediaCache.h
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -2372,36 +2372,45 @@ MediaCacheStream::AreAllStreamsForResour
 
 void
 MediaCacheStream::Close()
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (!mMediaCache) {
     return;
   }
+  OwnerThread()->Dispatch(NS_NewRunnableFunction(
+    "MediaCacheStream::Close",
+    [ this, client = RefPtr<ChannelMediaResource>(mClient) ]() {
+      AutoLock lock(mMediaCache->Monitor());
+      CloseInternal(lock);
+    }));
+}
 
-  AutoLock lock(mMediaCache->Monitor());
+void
+MediaCacheStream::CloseInternal(AutoLock& aLock)
+{
+  MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
+
   if (mClosed) {
     return;
   }
 
   // Closing a stream will change the return value of
   // MediaCacheStream::AreAllStreamsForResourceSuspended as well as
   // ChannelMediaResource::IsSuspendedByCache. Let's notify it.
-  mMediaCache->QueueSuspendedStatusUpdate(lock, mResourceID);
+  mMediaCache->QueueSuspendedStatusUpdate(aLock, mResourceID);
 
   mClosed = true;
-  mMediaCache->ReleaseStreamBlocks(lock, this);
+  mMediaCache->ReleaseStreamBlocks(aLock, this);
   // Wake up any blocked readers
-  lock.NotifyAll();
+  aLock.NotifyAll();
 
-  // Queue an Update since we may have created more free space. Don't do
-  // it from CloseInternal since that gets called by Update() itself
-  // sometimes, and we try to not to queue updates from Update().
-  mMediaCache->QueueUpdate(lock);
+  // Queue an Update since we may have created more free space.
+  mMediaCache->QueueUpdate(aLock);
 }
 
 void
 MediaCacheStream::Pin()
 {
   // TODO: Assert non-main thread.
   AutoLock lock(mMediaCache->Monitor());
   ++mPinCount;
--- a/dom/media/MediaCache.h
+++ b/dom/media/MediaCache.h
@@ -460,16 +460,18 @@ private:
                                  int64_t aLength);
 
   void NotifyDataEndedInternal(uint32_t aLoadID,
                                nsresult aStatus,
                                bool aReopenOnError);
 
   void UpdateDownloadStatistics(AutoLock&);
 
+  void CloseInternal(AutoLock&);
+
   // Instance of MediaCache to use with this MediaCacheStream.
   RefPtr<MediaCache> mMediaCache;
 
   ChannelMediaResource* const mClient;
 
   // The following fields must be written holding the cache's monitor and
   // only on the main thread, thus can be read either on the main thread
   // or while holding the cache's monitor.