Bug 1646054 - P1. Remove EventTargetWrapper class. r=froydnj
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 22 Jun 2020 13:55:21 +0000
changeset 536559 ad3e3c470aec2cae1576b8be96e3b4ee3b7923be
parent 536558 9432b16790667aaeb603fa66fc34a196e4606655
child 536560 d8c9e055d41015b544834f380300f2fa9601aabf
push id37530
push usernbeleuzu@mozilla.com
push dateMon, 22 Jun 2020 21:47:58 +0000
treeherdermozilla-central@9b38f4b9d883 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1646054, 1602167
milestone79.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 1646054 - P1. Remove EventTargetWrapper class. r=froydnj We also establish the intent of bug 1602167 on the TaskQueue object Differential Revision: https://phabricator.services.mozilla.com/D80351
dom/indexedDB/ActorsChild.cpp
dom/media/VideoUtils.h
xpcom/threads/AbstractThread.h
xpcom/threads/TaskQueue.cpp
xpcom/threads/TaskQueue.h
xpcom/threads/nsThreadManager.cpp
--- a/dom/indexedDB/ActorsChild.cpp
+++ b/dom/indexedDB/ActorsChild.cpp
@@ -1093,17 +1093,16 @@ class BackgroundRequestChild::Preprocess
     Finishing,
 
     // All done.
     Completed
   };
 
   const nsCOMPtr<nsIEventTarget> mOwningEventTarget;
   RefPtr<TaskQueue> mTaskQueue;
-  nsCOMPtr<nsIEventTarget> mTaskQueueEventTarget;
   nsCOMPtr<nsIInputStream> mStream;
   UniquePtr<JSStructuredCloneData> mCloneData;
   BackgroundRequestChild* mActor;
   const uint32_t mCloneDataIndex;
   nsresult mResultCode;
   State mState;
 
  public:
@@ -2672,17 +2671,16 @@ nsresult BackgroundRequestChild::Preproc
   nsCOMPtr<nsIEventTarget> target =
       do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
   MOZ_ASSERT(target);
 
   // We use a TaskQueue here in order to be sure that the events are dispatched
   // in the correct order. This is not guaranteed in case we use the I/O thread
   // directly.
   mTaskQueue = MakeRefPtr<TaskQueue>(target.forget());
-  mTaskQueueEventTarget = mTaskQueue->WrapAsEventTarget();
 
   ErrorResult errorResult;
 
   nsCOMPtr<nsIInputStream> stream;
   // XXX After Bug 1620560, MutableBlob is not needed here anymore.
   aFile.MutableBlob().CreateInputStream(getter_AddRefs(stream), errorResult);
   if (NS_WARN_IF(errorResult.Failed())) {
     return errorResult.StealNSResult();
@@ -2695,17 +2693,17 @@ nsresult BackgroundRequestChild::Preproc
 
   return NS_OK;
 }
 
 nsresult BackgroundRequestChild::PreprocessHelper::Dispatch() {
   AssertIsOnOwningThread();
   MOZ_ASSERT(mState == State::Initial);
 
-  nsresult rv = mTaskQueueEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
+  nsresult rv = mTaskQueue->Dispatch(this, NS_DISPATCH_NORMAL);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
 nsresult BackgroundRequestChild::PreprocessHelper::Start() {
@@ -2724,30 +2722,30 @@ nsresult BackgroundRequestChild::Preproc
 
     return NS_OK;
   }
 
   mState = State::WaitingForStreamReady;
 
   nsCOMPtr<nsIAsyncFileMetadata> asyncFileMetadata = do_QueryInterface(mStream);
   if (asyncFileMetadata) {
-    rv = asyncFileMetadata->AsyncFileMetadataWait(this, mTaskQueueEventTarget);
+    rv = asyncFileMetadata->AsyncFileMetadataWait(this, mTaskQueue);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     return NS_OK;
   }
 
   nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(mStream);
   if (!asyncStream) {
     return NS_ERROR_NO_INTERFACE;
   }
 
-  rv = asyncStream->AsyncWait(this, 0, 0, mTaskQueueEventTarget);
+  rv = asyncStream->AsyncWait(this, 0, 0, mTaskQueue);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
 nsresult BackgroundRequestChild::PreprocessHelper::ProcessStream() {
--- a/dom/media/VideoUtils.h
+++ b/dom/media/VideoUtils.h
@@ -6,31 +6,31 @@
 
 #ifndef VideoUtils_h
 #define VideoUtils_h
 
 #include "AudioSampleFormat.h"
 #include "MediaInfo.h"
 #include "TimeUnits.h"
 #include "VideoLimits.h"
-#include "mozilla/gfx/Point.h"  // for gfx::IntSize
-#include "mozilla/gfx/Types.h"
 #include "mozilla/AbstractThread.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/CheckedInt.h"
 #include "mozilla/MozPromise.h"
 #include "mozilla/ReentrantMonitor.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/SharedThreadPool.h"
+#include "mozilla/TaskQueue.h"
 #include "mozilla/UniquePtr.h"
+#include "mozilla/gfx/Point.h"  // for gfx::IntSize
+#include "mozilla/gfx/Types.h"
 #include "nsCOMPtr.h"
 #include "nsINamed.h"
 #include "nsIThread.h"
 #include "nsITimer.h"
-
 #include "nsThreadUtils.h"
 #include "prtime.h"
 
 using mozilla::CheckedInt32;
 using mozilla::CheckedInt64;
 using mozilla::CheckedUint32;
 using mozilla::CheckedUint64;
 
--- a/xpcom/threads/AbstractThread.h
+++ b/xpcom/threads/AbstractThread.h
@@ -12,17 +12,16 @@
 #  include "nscore.h"
 #  include "nsIRunnable.h"
 #  include "nsISerialEventTarget.h"
 #  include "nsISupportsImpl.h"
 #  include "nsIThread.h"
 
 namespace mozilla {
 
-class TaskQueue;
 class TaskDispatcher;
 
 /*
  * We often want to run tasks on a target that guarantees that events will never
  * run in parallel. There are various target types that achieve this - namely
  * nsIThread and TaskQueue. Note that nsIThreadPool (which implements
  * nsIEventTarget) does not have this property, so we do not want to use
  * nsIEventTarget for this purpose. This class encapsulates the specifics of
--- a/xpcom/threads/TaskQueue.cpp
+++ b/xpcom/threads/TaskQueue.cpp
@@ -1,67 +1,20 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/TaskQueue.h"
 
-#include "nsISerialEventTarget.h"
 #include "nsThreadUtils.h"
 
 namespace mozilla {
 
-class TaskQueue::EventTargetWrapper final : public nsISerialEventTarget {
-  RefPtr<TaskQueue> mTaskQueue;
-
-  ~EventTargetWrapper() = default;
-
- public:
-  explicit EventTargetWrapper(TaskQueue* aTaskQueue) : mTaskQueue(aTaskQueue) {
-    MOZ_ASSERT(mTaskQueue);
-  }
-
-  NS_IMETHOD
-  DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) override {
-    nsCOMPtr<nsIRunnable> ref = aEvent;
-    return Dispatch(ref.forget(), aFlags);
-  }
-
-  NS_IMETHOD
-  Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) override {
-    nsCOMPtr<nsIRunnable> runnable = aEvent;
-    MonitorAutoLock mon(mTaskQueue->mQueueMonitor);
-    return mTaskQueue->DispatchLocked(/* passed by ref */ runnable, aFlags,
-                                      NormalDispatch);
-  }
-
-  NS_IMETHOD
-  DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t aFlags) override {
-    return NS_ERROR_NOT_IMPLEMENTED;
-  }
-
-  NS_IMETHOD
-  IsOnCurrentThread(bool* aResult) override {
-    *aResult = mTaskQueue->IsCurrentThreadIn();
-    return NS_OK;
-  }
-
-  NS_IMETHOD_(bool)
-  IsOnCurrentThreadInfallible() override {
-    return mTaskQueue->mTarget->IsOnCurrentThread();
-  }
-
-  NS_DECL_THREADSAFE_ISUPPORTS
-};
-
-NS_IMPL_ISUPPORTS(TaskQueue::EventTargetWrapper, nsIEventTarget,
-                  nsISerialEventTarget)
-
 TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
                      const char* aName, bool aRequireTailDispatch,
                      bool aRetainFlags)
     : AbstractThread(aRequireTailDispatch),
       mTarget(aTarget),
       mQueueMonitor("TaskQueue::Queue"),
       mTailDispatcher(nullptr),
       mShouldRetainFlags(aRetainFlags),
@@ -175,21 +128,16 @@ bool TaskQueue::IsEmpty() {
   return mTasks.empty();
 }
 
 bool TaskQueue::IsCurrentThreadIn() const {
   bool in = mRunningThread == PR_GetCurrentThread();
   return in;
 }
 
-already_AddRefed<nsISerialEventTarget> TaskQueue::WrapAsEventTarget() {
-  nsCOMPtr<nsISerialEventTarget> ref = new EventTargetWrapper(this);
-  return ref.forget();
-}
-
 nsresult TaskQueue::Runner::Run() {
   TaskStruct event;
   {
     MonitorAutoLock mon(mQueue->mQueueMonitor);
     MOZ_ASSERT(mQueue->mIsRunning);
     if (mQueue->mTasks.empty()) {
       mQueue->mIsRunning = false;
       mQueue->MaybeResolveShutdown();
--- a/xpcom/threads/TaskQueue.h
+++ b/xpcom/threads/TaskQueue.h
@@ -4,28 +4,26 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef TaskQueue_h_
 #define TaskQueue_h_
 
 #include <queue>
 
+#include "mozilla/AbstractThread.h"
 #include "mozilla/Maybe.h"
 #include "mozilla/Monitor.h"
 #include "mozilla/MozPromise.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/TaskDispatcher.h"
 #include "mozilla/Unused.h"
 #include "nsIDirectTaskDispatcher.h"
 #include "nsThreadUtils.h"
 
-class nsIEventTarget;
-class nsIRunnable;
-
 namespace mozilla {
 
 typedef MozPromise<bool, bool, false> ShutdownPromise;
 
 // Abstracts executing runnables in order on an arbitrary event target. The
 // runnables dispatched to the TaskQueue will be executed in the order in which
 // they're received, and are guaranteed to not be executed concurrently.
 // They may be executed on different threads, and a memory barrier is used
@@ -61,32 +59,46 @@ class TaskQueue : public AbstractThread,
   TaskQueue(already_AddRefed<nsIEventTarget> aTarget, const char* aName,
             bool aSupportsTailDispatch = false, bool aRetainFlags = false);
 
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIDIRECTTASKDISPATCHER
 
   TaskDispatcher& TailDispatcher() override;
 
+  NS_IMETHOD Dispatch(already_AddRefed<nsIRunnable> aEvent,
+                      uint32_t aFlags) override {
+    nsCOMPtr<nsIRunnable> runnable = aEvent;
+    {
+      MonitorAutoLock mon(mQueueMonitor);
+      return DispatchLocked(/* passed by ref */ runnable, aFlags,
+                            NormalDispatch);
+    }
+    // If the ownership of |r| is not transferred in DispatchLocked() due to
+    // dispatch failure, it will be deleted here outside the lock. We do so
+    // since the destructor of the runnable might access TaskQueue and result
+    // in deadlocks.
+  }
+
   [[nodiscard]] nsresult Dispatch(
       already_AddRefed<nsIRunnable> aRunnable,
       DispatchReason aReason = NormalDispatch) override {
     nsCOMPtr<nsIRunnable> r = aRunnable;
     {
       MonitorAutoLock mon(mQueueMonitor);
       return DispatchLocked(/* passed by ref */ r, NS_DISPATCH_NORMAL, aReason);
     }
     // If the ownership of |r| is not transferred in DispatchLocked() due to
     // dispatch failure, it will be deleted here outside the lock. We do so
     // since the destructor of the runnable might access TaskQueue and result
     // in deadlocks.
   }
 
-  // Prevent a GCC warning about the other overload of Dispatch being hidden.
-  using AbstractThread::Dispatch;
+  // So we can access nsIEventTarget::Dispatch(nsIRunnable*, uint32_t aFlags)
+  using nsIEventTarget::Dispatch;
 
   // Puts the queue in a shutdown state and returns immediately. The queue will
   // remain alive at least until all the events are drained, because the Runners
   // hold a strong reference to the task queue, and one of them is always held
   // by the target event queue when the task queue is non-empty.
   //
   // The returned promise is resolved when the queue goes empty.
   RefPtr<ShutdownPromise> BeginShutdown();
@@ -99,20 +111,16 @@ class TaskQueue : public AbstractThread,
   void AwaitShutdownAndIdle();
 
   bool IsEmpty();
 
   // Returns true if the current thread is currently running a Runnable in
   // the task queue.
   bool IsCurrentThreadIn() const override;
 
-  // Create a new nsIEventTarget wrapper object that dispatches to this
-  // TaskQueue.
-  already_AddRefed<nsISerialEventTarget> WrapAsEventTarget();
-
  protected:
   virtual ~TaskQueue();
 
   // Blocks until all task finish executing. Called internally by methods
   // that need to wait until the task queue is idle.
   // mQueueMonitor must be held.
   void AwaitIdleLocked();
 
--- a/xpcom/threads/nsThreadManager.cpp
+++ b/xpcom/threads/nsThreadManager.cpp
@@ -182,21 +182,19 @@ void BackgroundEventTarget::FinishShutdo
 
 already_AddRefed<nsISerialEventTarget>
 BackgroundEventTarget::CreateBackgroundTaskQueue(const char* aName) {
   MutexAutoLock lock(mMutex);
 
   RefPtr<TaskQueue> queue = new TaskQueue(do_AddRef(this), aName,
                                           /*aSupportsTailDispatch=*/false,
                                           /*aRetainFlags=*/true);
-  nsCOMPtr<nsISerialEventTarget> target(queue->WrapAsEventTarget());
+  mTaskQueues.AppendElement(queue);
 
-  mTaskQueues.AppendElement(queue.forget());
-
-  return target.forget();
+  return queue.forget();
 }
 
 extern "C" {
 // This uses the C language linkage because it's exposed to Rust
 // via the xpcom/rust/moz_task crate.
 bool NS_IsMainThread() { return sTLSIsMainThread.get(); }
 }