Bug 1631304 - Replace EventTargetWrapper with XPCOMThreadWrapper. r=jya
authorBobby Holley <bobbyholley@gmail.com>
Tue, 28 Apr 2020 21:18:17 +0000
changeset 526566 a33a94d662338261a5fc84215be617bdda1fcb29
parent 526565 7d0595a40d4e4b4d55fe3bc6eda16324929aa3a3
child 526567 bad4d84f41cae3804de1b494172630bd661dde7f
push id37358
push useropoprus@mozilla.com
push dateWed, 29 Apr 2020 03:05:14 +0000
treeherdermozilla-central@6bb8423186c1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1631304
milestone77.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 1631304 - Replace EventTargetWrapper with XPCOMThreadWrapper. r=jya This is how it used to be, before the Quantum DOM stuff. We use nsIThreadInternal because that supports thread observers, which we leverage in the next patch. Differential Revision: https://phabricator.services.mozilla.com/D71709
xpcom/threads/AbstractThread.cpp
--- a/xpcom/threads/AbstractThread.cpp
+++ b/xpcom/threads/AbstractThread.cpp
@@ -10,97 +10,99 @@
 #include "mozilla/Maybe.h"
 #include "mozilla/MozPromise.h"  // We initialize the MozPromise logging in this file.
 #include "mozilla/StateWatching.h"  // We initialize the StateWatching logging in this file.
 #include "mozilla/StaticPtr.h"
 #include "mozilla/TaskDispatcher.h"
 #include "mozilla/TaskQueue.h"
 #include "mozilla/Unused.h"
 #include "nsContentUtils.h"
+#include "nsIThreadInternal.h"
 #include "nsServiceManagerUtils.h"
+#include "nsThreadManager.h"
 #include "nsThreadUtils.h"
 
 namespace mozilla {
 
 LazyLogModule gMozPromiseLog("MozPromise");
 LazyLogModule gStateWatchingLog("StateWatching");
 
 StaticRefPtr<AbstractThread> sMainThread;
 MOZ_THREAD_LOCAL(AbstractThread*) AbstractThread::sCurrentThreadTLS;
 
-class EventTargetWrapper : public AbstractThread {
+class XPCOMThreadWrapper : public AbstractThread {
  public:
-  explicit EventTargetWrapper(nsIEventTarget* aTarget,
+  explicit XPCOMThreadWrapper(nsIThreadInternal* aThread,
                               bool aRequireTailDispatch)
-      : AbstractThread(aRequireTailDispatch), mTarget(aTarget) {
+      : AbstractThread(aRequireTailDispatch), mThread(aThread) {
     // Our current mechanism of implementing tail dispatch is appshell-specific.
     // This is because a very similar mechanism already exists on the main
     // thread, and we want to avoid making event dispatch on the main thread
     // more complicated than it already is.
     //
     // If you need to use tail dispatch on other XPCOM threads, you'll need to
     // implement an nsIThreadObserver to fire the tail dispatcher at the
     // appropriate times. You will also need to modify this assertion.
     MOZ_ASSERT_IF(aRequireTailDispatch,
-                  NS_IsMainThread() && aTarget->IsOnCurrentThread());
+                  NS_IsMainThread() && aThread->IsOnCurrentThread());
   }
 
   nsresult Dispatch(already_AddRefed<nsIRunnable> aRunnable,
                     DispatchReason aReason = NormalDispatch) override {
     AbstractThread* currentThread;
     if (aReason != TailDispatch && (currentThread = GetCurrent()) &&
         RequiresTailDispatch(currentThread)) {
       return currentThread->TailDispatcher().AddTask(this,
                                                      std::move(aRunnable));
     }
 
     RefPtr<nsIRunnable> runner = new Runner(this, std::move(aRunnable));
-    return mTarget->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
+    return mThread->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
   }
 
   // Prevent a GCC warning about the other overload of Dispatch being hidden.
   using AbstractThread::Dispatch;
 
   bool IsCurrentThreadIn() const override {
-    return mTarget->IsOnCurrentThread();
+    return mThread->IsOnCurrentThread();
   }
 
   void FireTailDispatcher() {
     MOZ_DIAGNOSTIC_ASSERT(mTailDispatcher.isSome());
     mTailDispatcher.ref().DrainDirectTasks();
     mTailDispatcher.reset();
   }
 
   TaskDispatcher& TailDispatcher() override {
     MOZ_ASSERT(IsCurrentThreadIn());
     if (!mTailDispatcher.isSome()) {
       mTailDispatcher.emplace(/* aIsTailDispatcher = */ true);
 
       nsCOMPtr<nsIRunnable> event =
-          NewRunnableMethod("EventTargetWrapper::FireTailDispatcher", this,
-                            &EventTargetWrapper::FireTailDispatcher);
+          NewRunnableMethod("XPCOMThreadWrapper::FireTailDispatcher", this,
+                            &XPCOMThreadWrapper::FireTailDispatcher);
       nsContentUtils::RunInStableState(event.forget());
     }
 
     return mTailDispatcher.ref();
   }
 
   bool MightHaveTailTasks() override { return mTailDispatcher.isSome(); }
 
-  nsIEventTarget* AsEventTarget() override { return mTarget; }
+  nsIEventTarget* AsEventTarget() override { return mThread; }
 
  private:
-  const RefPtr<nsIEventTarget> mTarget;
+  const RefPtr<nsIThreadInternal> mThread;
   Maybe<AutoTaskDispatcher> mTailDispatcher;
 
   class Runner : public Runnable {
    public:
-    explicit Runner(EventTargetWrapper* aThread,
+    explicit Runner(XPCOMThreadWrapper* aThread,
                     already_AddRefed<nsIRunnable> aRunnable)
-        : Runnable("EventTargetWrapper::Runner"),
+        : Runnable("XPCOMThreadWrapper::Runner"),
           mThread(aThread),
           mRunnable(aRunnable) {}
 
     NS_IMETHOD Run() override {
       MOZ_ASSERT(mThread == AbstractThread::GetCurrent());
       MOZ_ASSERT(mThread->IsCurrentThreadIn());
       return mRunnable->Run();
     }
@@ -116,17 +118,17 @@ class EventTargetWrapper : public Abstra
           aName.Append(name);
         }
       }
       return NS_OK;
     }
 #endif
 
    private:
-    const RefPtr<EventTargetWrapper> mThread;
+    const RefPtr<XPCOMThreadWrapper> mThread;
     const RefPtr<nsIRunnable> mRunnable;
   };
 };
 
 NS_IMPL_ISUPPORTS(AbstractThread, nsIEventTarget, nsISerialEventTarget)
 
 NS_IMETHODIMP_(bool)
 AbstractThread::IsOnCurrentThreadInfallible() { return IsCurrentThreadIn(); }
@@ -191,20 +193,20 @@ void AbstractThread::InitTLS() {
   if (!sCurrentThreadTLS.init()) {
     MOZ_CRASH();
   }
 }
 
 void AbstractThread::InitMainThread() {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(!sMainThread);
-  nsCOMPtr<nsIThread> mainThread;
-  NS_GetMainThread(getter_AddRefs(mainThread));
+  nsCOMPtr<nsIThreadInternal> mainThread =
+      do_QueryInterface(nsThreadManager::get().GetMainThreadWeak());
   MOZ_DIAGNOSTIC_ASSERT(mainThread);
-  sMainThread = new EventTargetWrapper(mainThread.get(),
+  sMainThread = new XPCOMThreadWrapper(mainThread.get(),
                                        /* aRequireTailDispatch = */ true);
   ClearOnShutdown(&sMainThread);
 
   if (!sCurrentThreadTLS.init()) {
     MOZ_CRASH();
   }
   // Set the default current main thread so that GetCurrent() never returns
   // nullptr.
@@ -220,36 +222,38 @@ void AbstractThread::DispatchStateChange
 void AbstractThread::DispatchDirectTask(
     already_AddRefed<nsIRunnable> aRunnable) {
   GetCurrent()->TailDispatcher().AddDirectTask(std::move(aRunnable));
 }
 
 /* static */
 already_AddRefed<AbstractThread> AbstractThread::CreateXPCOMThreadWrapper(
     nsIThread* aThread, bool aRequireTailDispatch) {
-  RefPtr<EventTargetWrapper> wrapper =
-      new EventTargetWrapper(aThread, aRequireTailDispatch);
+  nsCOMPtr<nsIThreadInternal> internalThread = do_QueryInterface(aThread);
+  MOZ_ASSERT(internalThread, "Need an nsThread for AbstractThread");
+  RefPtr<XPCOMThreadWrapper> wrapper =
+      new XPCOMThreadWrapper(internalThread, aRequireTailDispatch);
 
   bool onCurrentThread = false;
   Unused << aThread->IsOnCurrentThread(&onCurrentThread);
 
   if (onCurrentThread) {
     MOZ_ASSERT(
         !sCurrentThreadTLS.get(),
-        "There can only be a single EventTargetWrapper available on a thread");
+        "There can only be a single XPCOMThreadWrapper available on a thread");
     sCurrentThreadTLS.set(wrapper);
     return wrapper.forget();
   }
 
   // Set the thread-local sCurrentThreadTLS to point to the wrapper on the
   // target thread. This ensures that sCurrentThreadTLS is as expected by
   // AbstractThread::GetCurrent() on the target thread.
   nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
       "AbstractThread::CreateXPCOMThreadWrapper", [wrapper]() {
         MOZ_ASSERT(!sCurrentThreadTLS.get(),
-                   "There can only be a single EventTargetWrapper available on "
+                   "There can only be a single XPCOMThreadWrapper available on "
                    "a thread");
         sCurrentThreadTLS.set(wrapper);
       });
   aThread->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
   return wrapper.forget();
 }
 }  // namespace mozilla