Bug 1418917. P1 - run some functions off the main thread. draft
authorJW Wang <jwwang@mozilla.com>
Thu, 16 Nov 2017 15:41:36 +0800
changeset 703047 65f032e8dda5875f65c6ff3b5efecdd2e746f5af
parent 703000 2d7dc6215167a5bdeb6f3dfde5b2df513b990921
child 703048 016e630c64d7c2d71c1db6954d4703cbac6214db
push id90681
push userjwwang@mozilla.com
push dateFri, 24 Nov 2017 08:20:36 +0000
bugs1418917
milestone59.0a1
Bug 1418917. P1 - run some functions off the main thread. MozReview-Commit-ID: Fn9OveV69hX
dom/media/MediaCache.cpp
dom/media/MediaCache.h
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -1932,27 +1932,33 @@ MediaCache::NoteSeek(MediaCacheStream* a
     }
   }
 }
 
 void
 MediaCacheStream::NotifyLoadID(uint32_t aLoadID)
 {
   MOZ_ASSERT(aLoadID > 0);
-  ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
-  mLoadID = aLoadID;
+
+  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
+    "MediaCacheStream::NotifyLoadID",
+    [ client = RefPtr<ChannelMediaResource>(mClient), this, aLoadID ]() {
+      ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
+      mLoadID = aLoadID;
+    });
+  OwnerThread()->Dispatch(r.forget());
 }
 
 void
-MediaCacheStream::NotifyDataStarted(uint32_t aLoadID,
-                                    int64_t aOffset,
-                                    bool aSeekable,
-                                    int64_t aLength)
+MediaCacheStream::NotifyDataStartedInternal(uint32_t aLoadID,
+                                            int64_t aOffset,
+                                            bool aSeekable,
+                                            int64_t aLength)
 {
-  NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
+  MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
   MOZ_ASSERT(aLoadID > 0);
   LOG("Stream %p DataStarted: %" PRId64 " aLoadID=%u aLength=%" PRId64,
       this,
       aOffset,
       aLoadID,
       aLength);
 
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
@@ -1995,16 +2001,33 @@ MediaCacheStream::UpdatePrincipal(nsIPri
     if (nsContentUtils::CombineResourcePrincipals(&stream->mPrincipal,
                                                   aPrincipal)) {
       stream->mClient->CacheClientNotifyPrincipalChanged();
     }
   }
 }
 
 void
+MediaCacheStream::NotifyDataStarted(uint32_t aLoadID,
+                                    int64_t aOffset,
+                                    bool aSeekable,
+                                    int64_t aLength)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(aLoadID > 0);
+
+  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
+    "MediaCacheStream::NotifyDataStarted",
+    [ =, client = RefPtr<ChannelMediaResource>(mClient) ]() {
+      NotifyDataStartedInternal(aLoadID, aOffset, aSeekable, aLength);
+    });
+  OwnerThread()->Dispatch(r.forget());
+}
+
+void
 MediaCacheStream::NotifyDataReceived(uint32_t aLoadID,
                                      uint32_t aCount,
                                      const uint8_t* aData)
 {
   MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
   MOZ_ASSERT(aLoadID > 0);
 
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
@@ -2455,22 +2478,28 @@ MediaCacheStream::SetReadMode(ReadMode a
     return;
   mCurrentMode = aMode;
   mMediaCache->QueueUpdate();
 }
 
 void
 MediaCacheStream::SetPlaybackRate(uint32_t aBytesPerSecond)
 {
-  NS_ASSERTION(aBytesPerSecond > 0, "Zero playback rate not allowed");
-  ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
-  if (aBytesPerSecond == mPlaybackBytesPerSecond)
-    return;
-  mPlaybackBytesPerSecond = aBytesPerSecond;
-  mMediaCache->QueueUpdate();
+  MOZ_ASSERT(aBytesPerSecond > 0, "Zero playback rate not allowed");
+
+  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
+    "MediaCacheStream::SetPlaybackRate",
+    [ =, client = RefPtr<ChannelMediaResource>(mClient) ]() {
+      ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
+      if (!mClosed && mPlaybackBytesPerSecond != aBytesPerSecond) {
+        mPlaybackBytesPerSecond = aBytesPerSecond;
+        mMediaCache->QueueUpdate();
+      }
+    });
+  OwnerThread()->Dispatch(r.forget());
 }
 
 nsresult
 MediaCacheStream::Seek(int64_t aOffset)
 {
   MOZ_ASSERT(!NS_IsMainThread());
   mMediaCache->GetReentrantMonitor().AssertCurrentThreadIn();
 
@@ -2488,22 +2517,28 @@ MediaCacheStream::Seek(int64_t aOffset)
   mMediaCache->QueueUpdate();
   return NS_OK;
 }
 
 void
 MediaCacheStream::ThrottleReadahead(bool bThrottle)
 {
   MOZ_ASSERT(NS_IsMainThread());
-  ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
-  if (mThrottleReadahead != bThrottle) {
-    LOGI("Stream %p ThrottleReadahead %d", this, bThrottle);
-    mThrottleReadahead = bThrottle;
-    mMediaCache->QueueUpdate();
-  }
+
+  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
+    "MediaCacheStream::ThrottleReadahead",
+    [ client = RefPtr<ChannelMediaResource>(mClient), this, bThrottle ]() {
+      ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
+      if (!mClosed && mThrottleReadahead != bThrottle) {
+        LOGI("Stream %p ThrottleReadahead %d", this, bThrottle);
+        mThrottleReadahead = bThrottle;
+        mMediaCache->QueueUpdate();
+      }
+    });
+  OwnerThread()->Dispatch(r.forget());
 }
 
 uint32_t
 MediaCacheStream::ReadPartialBlock(int64_t aOffset, Span<char> aBuffer)
 {
   mMediaCache->GetReentrantMonitor().AssertCurrentThreadIn();
   MOZ_ASSERT(IsOffsetAllowed(aOffset));
 
--- a/dom/media/MediaCache.h
+++ b/dom/media/MediaCache.h
@@ -449,16 +449,21 @@ private:
   // any thread.
   int64_t GetNextCachedDataInternal(int64_t aOffset);
   // Writes |mPartialBlock| to disk.
   // Used by |NotifyDataEnded| and |FlushPartialBlock|.
   // If |aNotifyAll| is true, this function will wake up readers who may be
   // waiting on the media cache monitor. Called on the main thread only.
   void FlushPartialBlockInternal(bool aNotify, ReentrantMonitorAutoEnter& aReentrantMonitor);
 
+  void NotifyDataStartedInternal(uint32_t aLoadID,
+                                 int64_t aOffset,
+                                 bool aSeekable,
+                                 int64_t aLength);
+
   void NotifyDataEndedInternal(uint32_t aLoadID,
                                nsresult aStatus,
                                bool aReopenOnError);
 
   // Instance of MediaCache to use with this MediaCacheStream.
   RefPtr<MediaCache> mMediaCache;
 
   ChannelMediaResource* const mClient;