Bug 1267600 - Ask the main thread to shut down the SystemClockDriver if needed. r?jesup draft
authorPaul Adenot <paul@paul.cx>
Tue, 26 Apr 2016 14:12:19 +0200
changeset 356437 b25ad7533bc2d9a6769c77a95cbbe0d1dbea456a
parent 356436 b0a8e23b23e0095d8d13c42e90f90f2039891349
child 519404 01bd66b2b20364d1439d4dd9540c10c0b37e6d57
push id16514
push userpaul@paul.cx
push dateTue, 26 Apr 2016 12:12:43 +0000
reviewersjesup
bugs1267600
milestone48.0a1
Bug 1267600 - Ask the main thread to shut down the SystemClockDriver if needed. r?jesup This also factors in the OfflineAudioDriver case that was correct, but similar, in the ThreadedDriver dtor, and implements the suggestion that was in the comment (use .forget()). MozReview-Commit-ID: 7kdb60mFAnb
dom/media/GraphDriver.cpp
--- a/dom/media/GraphDriver.cpp
+++ b/dom/media/GraphDriver.cpp
@@ -164,20 +164,45 @@ void GraphDriver::SetPreviousDriver(Grap
   GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
   mPreviousDriver = aPreviousDriver;
 }
 
 ThreadedDriver::ThreadedDriver(MediaStreamGraphImpl* aGraphImpl)
   : GraphDriver(aGraphImpl)
 { }
 
+class MediaStreamGraphShutdownThreadRunnable : public nsRunnable {
+public:
+  explicit MediaStreamGraphShutdownThreadRunnable(already_AddRefed<nsIThread> aThread)
+    : mThread(aThread)
+  {
+  }
+  NS_IMETHOD Run()
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+    MOZ_ASSERT(mThread);
+
+    mThread->Shutdown();
+    mThread = nullptr;
+    return NS_OK;
+  }
+private:
+  nsCOMPtr<nsIThread> mThread;
+};
+
 ThreadedDriver::~ThreadedDriver()
 {
   if (mThread) {
-    mThread->Shutdown();
+    if (NS_IsMainThread()) {
+      mThread->Shutdown();
+    } else {
+      nsCOMPtr<nsIRunnable> event =
+        new MediaStreamGraphShutdownThreadRunnable(mThread.forget());
+      NS_DispatchToMainThread(event);
+    }
   }
 }
 class MediaStreamGraphInitThreadRunnable : public nsRunnable {
 public:
   explicit MediaStreamGraphInitThreadRunnable(ThreadedDriver* aDriver)
     : mDriver(aDriver)
   {
   }
@@ -416,44 +441,18 @@ void SystemClockDriver::WakeUp()
 
 OfflineClockDriver::OfflineClockDriver(MediaStreamGraphImpl* aGraphImpl, GraphTime aSlice)
   : ThreadedDriver(aGraphImpl),
     mSlice(aSlice)
 {
 
 }
 
-class MediaStreamGraphShutdownThreadRunnable : public nsRunnable {
-public:
-  explicit MediaStreamGraphShutdownThreadRunnable(nsIThread* aThread)
-    : mThread(aThread)
-  {
-  }
-  NS_IMETHOD Run()
-  {
-    MOZ_ASSERT(NS_IsMainThread());
-    MOZ_ASSERT(mThread);
-
-    mThread->Shutdown();
-    mThread = nullptr;
-    return NS_OK;
-  }
-private:
-  nsCOMPtr<nsIThread> mThread;
-};
-
 OfflineClockDriver::~OfflineClockDriver()
 {
-  // transfer the ownership of mThread to the event
-  // XXX should use .forget()/etc
-  if (mThread) {
-    nsCOMPtr<nsIRunnable> event = new MediaStreamGraphShutdownThreadRunnable(mThread);
-    mThread = nullptr;
-    NS_DispatchToMainThread(event);
-  }
 }
 
 MediaTime
 OfflineClockDriver::GetIntervalForIteration()
 {
   return mGraphImpl->MillisecondsToMediaTime(mSlice);
 }