Bug 1277226 - Implement MainThreadWorkerRunnable, r=sicking
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 08 Jun 2016 07:12:05 +0200
changeset 301015 4a56907aff097f0fbbd1ff0479dbb6a547ae7f5a
parent 301014 09e6e28c81971e064c1117099ddf1dda027fa538
child 301016 d71d5a7a70d2c45f430a119c1b0edcfc8f83f664
push id19599
push usercbook@mozilla.com
push dateWed, 08 Jun 2016 10:16:21 +0000
treeherderfx-team@81f4cc3f6f4c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs1277226
milestone50.0a1
Bug 1277226 - Implement MainThreadWorkerRunnable, r=sicking
dom/base/WebSocket.cpp
dom/bindings/BindingUtils.cpp
dom/fetch/Fetch.cpp
dom/notification/Notification.cpp
dom/workers/WorkerRunnable.cpp
dom/workers/WorkerRunnable.h
--- a/dom/base/WebSocket.cpp
+++ b/dom/base/WebSocket.cpp
@@ -2602,21 +2602,21 @@ WebSocketImpl::GetStatus(nsresult* aStat
   AssertIsOnTargetThread();
 
   *aStatus = NS_OK;
   return NS_OK;
 }
 
 namespace {
 
-class CancelRunnable final : public WorkerRunnable
+class CancelRunnable final : public MainThreadWorkerRunnable
 {
 public:
   CancelRunnable(WorkerPrivate* aWorkerPrivate, WebSocketImpl* aImpl)
-    : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
+    : MainThreadWorkerRunnable(aWorkerPrivate)
     , mImpl(aImpl)
   {
   }
 
   bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
   {
     aWorkerPrivate->AssertIsOnWorkerThread();
     aWorkerPrivate->ModifyBusyCountFromWorker(true);
@@ -2624,33 +2624,16 @@ public:
   }
 
   void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
                bool aRunResult) override
   {
     aWorkerPrivate->ModifyBusyCountFromWorker(false);
   }
 
-  bool
-  PreDispatch(WorkerPrivate* aWorkerPrivate) override
-  {
-    // We don't call WorkerRunnable::PreDispatch because it would assert the
-    // wrong thing about which thread we're on.
-    AssertIsOnMainThread();
-    return true;
-  }
-
-  void
-  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
-  {
-    // We don't call WorkerRunnable::PostDispatch because it would assert the
-    // wrong thing about which thread we're on.
-    AssertIsOnMainThread();
-  }
-
 private:
   RefPtr<WebSocketImpl> mImpl;
 };
 
 } // namespace
 
 // Window closed, stop/reload button pressed, user navigated away from page, etc.
 NS_IMETHODIMP
--- a/dom/bindings/BindingUtils.cpp
+++ b/dom/bindings/BindingUtils.cpp
@@ -3353,49 +3353,36 @@ private:
 
     ReleaseWorker();
     return NS_OK;
   }
 
   void
   ReleaseWorker()
   {
-    class ReleaseRunnable final : public WorkerRunnable
+    class ReleaseRunnable final : public MainThreadWorkerRunnable
     {
       RefPtr<DeprecationWarningRunnable> mRunnable;
 
     public:
       ReleaseRunnable(WorkerPrivate* aWorkerPrivate,
                       DeprecationWarningRunnable* aRunnable)
-        : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
+        : MainThreadWorkerRunnable(aWorkerPrivate)
         , mRunnable(aRunnable)
       {}
 
       virtual bool
       WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
       {
         MOZ_ASSERT(aWorkerPrivate);
         aWorkerPrivate->AssertIsOnWorkerThread();
 
         aWorkerPrivate->RemoveFeature(mRunnable);
         return true;
       }
-
-      virtual bool
-      PreDispatch(WorkerPrivate* aWorkerPrivate) override
-      {
-        AssertIsOnMainThread();
-        return true;
-      }
-
-      virtual void
-      PostDispatch(WorkerPrivate* aWorkerPrivate,
-                   bool aDispatchResult) override
-      {
-      }
     };
 
     RefPtr<ReleaseRunnable> runnable =
       new ReleaseRunnable(mWorkerPrivate, this);
     NS_WARN_IF(!runnable->Dispatch());
   }
 };
 
--- a/dom/fetch/Fetch.cpp
+++ b/dom/fetch/Fetch.cpp
@@ -257,26 +257,26 @@ MainThreadFetchResolver::OnResponseAvail
   }
 }
 
 MainThreadFetchResolver::~MainThreadFetchResolver()
 {
   NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver);
 }
 
-class WorkerFetchResponseRunnable final : public WorkerRunnable
+class WorkerFetchResponseRunnable final : public MainThreadWorkerRunnable
 {
   RefPtr<WorkerFetchResolver> mResolver;
   // Passed from main thread to worker thread after being initialized.
   RefPtr<InternalResponse> mInternalResponse;
 public:
   WorkerFetchResponseRunnable(WorkerPrivate* aWorkerPrivate,
                               WorkerFetchResolver* aResolver,
                               InternalResponse* aResponse)
-    : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
+    : MainThreadWorkerRunnable(aWorkerPrivate)
     , mResolver(aResolver)
     , mInternalResponse(aResponse)
   {
   }
 
   bool
   WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
   {
@@ -291,35 +291,16 @@ public:
       promise->MaybeResolve(response);
     } else {
       ErrorResult result;
       result.ThrowTypeError<MSG_FETCH_FAILED>();
       promise->MaybeReject(result);
     }
     return true;
   }
-
-  bool
-  PreDispatch(WorkerPrivate* aWorkerPrivate) override
-  {
-    // Don't call default PreDispatch() since it incorrectly asserts we are
-    // dispatching from the parent worker thread.  We always dispatch from
-    // the main thread.
-    AssertIsOnMainThread();
-    return true;
-  }
-
-  void
-  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
-  {
-    // Don't call default PostDispatch() since it incorrectly asserts we are
-    // dispatching from the parent worker thread.  We always dispatch from
-    // the main thread.
-    AssertIsOnMainThread();
-  }
 };
 
 class WorkerFetchResponseEndBase
 {
   RefPtr<PromiseWorkerProxy> mPromiseProxy;
 public:
   explicit WorkerFetchResponseEndBase(PromiseWorkerProxy* aPromiseProxy)
     : mPromiseProxy(aPromiseProxy)
@@ -331,23 +312,22 @@ public:
   WorkerRunInternal(WorkerPrivate* aWorkerPrivate)
   {
     MOZ_ASSERT(aWorkerPrivate);
     aWorkerPrivate->AssertIsOnWorkerThread();
     mPromiseProxy->CleanUp();
   }
 };
 
-class WorkerFetchResponseEndRunnable final : public WorkerRunnable
+class WorkerFetchResponseEndRunnable final : public MainThreadWorkerRunnable
                                            , public WorkerFetchResponseEndBase
 {
 public:
   explicit WorkerFetchResponseEndRunnable(PromiseWorkerProxy* aPromiseProxy)
-    : WorkerRunnable(aPromiseProxy->GetWorkerPrivate(),
-                     WorkerThreadUnchangedBusyCount)
+    : MainThreadWorkerRunnable(aPromiseProxy->GetWorkerPrivate())
     , WorkerFetchResponseEndBase(aPromiseProxy)
   {
   }
 
   bool
   WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
   {
     WorkerRunInternal(aWorkerPrivate);
@@ -357,35 +337,16 @@ public:
   nsresult
   Cancel() override
   {
     // Execute Run anyway to make sure we cleanup our promise proxy to avoid
     // leaking the worker thread
     Run();
     return WorkerRunnable::Cancel();
   }
-
-  bool
-  PreDispatch(WorkerPrivate* aWorkerPrivate) override
-  {
-    // Don't call default PreDispatch() since it incorrectly asserts we are
-    // dispatching from the parent worker thread.  We always dispatch from
-    // the main thread.
-    AssertIsOnMainThread();
-    return true;
-  }
-
-  void
-  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
-  {
-    // Don't call default PostDispatch() since it incorrectly asserts we are
-    // dispatching from the parent worker thread.  We always dispatch from
-    // the main thread.
-    AssertIsOnMainThread();
-  }
 };
 
 class WorkerFetchResponseEndControlRunnable final : public MainThreadWorkerControlRunnable
                                                   , public WorkerFetchResponseEndBase
 {
 public:
   explicit WorkerFetchResponseEndControlRunnable(PromiseWorkerProxy* aPromiseProxy)
     : MainThreadWorkerControlRunnable(aPromiseProxy->GetWorkerPrivate())
@@ -638,62 +599,43 @@ ExtractByteStreamFromBody(const ArrayBuf
   return NS_ERROR_FAILURE;
 }
 
 namespace {
 /*
  * Called on successfully reading the complete stream.
  */
 template <class Derived>
-class ContinueConsumeBodyRunnable final : public WorkerRunnable
+class ContinueConsumeBodyRunnable final : public MainThreadWorkerRunnable
 {
   // This has been addrefed before this runnable is dispatched,
   // released in WorkerRun().
   FetchBody<Derived>* mFetchBody;
   nsresult mStatus;
   uint32_t mLength;
   uint8_t* mResult;
 
 public:
   ContinueConsumeBodyRunnable(FetchBody<Derived>* aFetchBody, nsresult aStatus,
                               uint32_t aLength, uint8_t* aResult)
-    : WorkerRunnable(aFetchBody->mWorkerPrivate, WorkerThreadUnchangedBusyCount)
+    : MainThreadWorkerRunnable(aFetchBody->mWorkerPrivate)
     , mFetchBody(aFetchBody)
     , mStatus(aStatus)
     , mLength(aLength)
     , mResult(aResult)
   {
     MOZ_ASSERT(NS_IsMainThread());
   }
 
   bool
   WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
   {
     mFetchBody->ContinueConsumeBody(mStatus, mLength, mResult);
     return true;
   }
-
-  bool
-  PreDispatch(WorkerPrivate* aWorkerPrivate) override
-  {
-    // Don't call default PreDispatch() since it incorrectly asserts we are
-    // dispatching from the parent worker thread.  We always dispatch from
-    // the main thread.
-    AssertIsOnMainThread();
-    return true;
-  }
-
-  void
-  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
-  {
-    // Don't call default PostDispatch() since it incorrectly asserts we are
-    // dispatching from the parent worker thread.  We always dispatch from
-    // the main thread.
-    AssertIsOnMainThread();
-  }
 };
 
 // OnStreamComplete always adopts the buffer, utility class to release it in
 // a couple of places.
 class MOZ_STACK_CLASS AutoFreeBuffer final {
   uint8_t* mBuffer;
 
 public:
--- a/dom/notification/Notification.cpp
+++ b/dom/notification/Notification.cpp
@@ -356,39 +356,22 @@ CheckScope(nsIPrincipal* aPrincipal, con
 
   return aPrincipal->CheckMayLoad(scopeURI, /* report = */ true,
                                   /* allowIfInheritsPrincipal = */ false);
 }
 } // anonymous namespace
 
 // Subclass that can be directly dispatched to child workers from the main
 // thread.
-class NotificationWorkerRunnable : public WorkerRunnable
+class NotificationWorkerRunnable : public MainThreadWorkerRunnable
 {
 protected:
   explicit NotificationWorkerRunnable(WorkerPrivate* aWorkerPrivate)
-    : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
-  {
-  }
-
-  bool
-  PreDispatch(WorkerPrivate* aWorkerPrivate) override
+    : MainThreadWorkerRunnable(aWorkerPrivate)
   {
-    // We don't call WorkerRunnable::PreDispatch because it would assert the
-    // wrong thing about which thread we're on.
-    AssertIsOnMainThread();
-    return true;
-  }
-
-  void
-  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
-  {
-    // We don't call WorkerRunnable::PostDispatch because it would assert the
-    // wrong thing about which thread we're on.
-    AssertIsOnMainThread();
   }
 
   bool
   WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
   {
     aWorkerPrivate->AssertIsOnWorkerThread();
     aWorkerPrivate->ModifyBusyCountFromWorker(true);
     WorkerRunInternal(aWorkerPrivate);
--- a/dom/workers/WorkerRunnable.cpp
+++ b/dom/workers/WorkerRunnable.cpp
@@ -598,23 +598,16 @@ WorkerControlRunnable::DispatchInternal(
   }
 
   nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
   MOZ_ASSERT(mainThread);
 
   return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
 }
 
-void
-MainThreadWorkerControlRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
-                                              bool aDispatchResult)
-{
-  AssertIsOnMainThread();
-}
-
 NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable)
 
 WorkerMainThreadRunnable::WorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
                                                    const nsACString& aTelemetryKey)
 : mWorkerPrivate(aWorkerPrivate)
 , mTelemetryKey(aTelemetryKey)
 {
   mWorkerPrivate->AssertIsOnWorkerThread();
--- a/dom/workers/WorkerRunnable.h
+++ b/dom/workers/WorkerRunnable.h
@@ -295,16 +295,45 @@ public:
 private:
   virtual bool
   DispatchInternal() override;
 
   // Should only be called by WorkerPrivate::DoRunLoop.
   using WorkerRunnable::Cancel;
 };
 
+// A convenience class for WorkerRunnables that originate on the main
+// thread.
+class MainThreadWorkerRunnable : public WorkerRunnable
+{
+protected:
+  explicit MainThreadWorkerRunnable(WorkerPrivate* aWorkerPrivate)
+  : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
+  {
+    AssertIsOnMainThread();
+  }
+
+  virtual ~MainThreadWorkerRunnable()
+  {}
+
+  virtual bool
+  PreDispatch(WorkerPrivate* aWorkerPrivate) override
+  {
+    AssertIsOnMainThread();
+    return true;
+  }
+
+  virtual void
+  PostDispatch(WorkerPrivate* aWorkerPrivate,
+               bool aDispatchResult) override
+  {
+    AssertIsOnMainThread();
+  }
+};
+
 // A convenience class for WorkerControlRunnables that originate on the main
 // thread.
 class MainThreadWorkerControlRunnable : public WorkerControlRunnable
 {
 protected:
   explicit MainThreadWorkerControlRunnable(WorkerPrivate* aWorkerPrivate)
   : WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
   { }
@@ -315,17 +344,20 @@ protected:
   virtual bool
   PreDispatch(WorkerPrivate* aWorkerPrivate) override
   {
     AssertIsOnMainThread();
     return true;
   }
 
   virtual void
-  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override;
+  PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
+  {
+    AssertIsOnMainThread();
+  }
 };
 
 // A WorkerRunnable that should be dispatched from the worker to itself for
 // async tasks. This will increment the busy count PostDispatch() (only if
 // dispatch was successful) and decrement it in PostRun().
 //
 // Async tasks will almost always want to use this since
 // a WorkerSameThreadRunnable keeps the Worker from being GCed.