diff --git a/dom/indexedDB/ActorsChild.cpp b/dom/indexedDB/ActorsChild.cpp index 0ae9f99a25c9..9f10fdc6e15f 100644 --- a/dom/indexedDB/ActorsChild.cpp +++ b/dom/indexedDB/ActorsChild.cpp @@ -1098,7 +1098,6 @@ class BackgroundRequestChild::PreprocessHelper final const nsCOMPtr mOwningEventTarget; RefPtr mTaskQueue; - nsCOMPtr mTaskQueueEventTarget; nsCOMPtr mStream; UniquePtr mCloneData; BackgroundRequestChild* mActor; @@ -2677,7 +2676,6 @@ nsresult BackgroundRequestChild::PreprocessHelper::Init( // in the correct order. This is not guaranteed in case we use the I/O thread // directly. mTaskQueue = MakeRefPtr(target.forget()); - mTaskQueueEventTarget = mTaskQueue->WrapAsEventTarget(); ErrorResult errorResult; @@ -2700,7 +2698,7 @@ 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; } @@ -2729,7 +2727,7 @@ nsresult BackgroundRequestChild::PreprocessHelper::Start() { nsCOMPtr 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; } @@ -2742,7 +2740,7 @@ nsresult BackgroundRequestChild::PreprocessHelper::Start() { 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; } diff --git a/dom/media/VideoUtils.h b/dom/media/VideoUtils.h index 85ae976e4ff8..e19a1a54b6c8 100644 --- a/dom/media/VideoUtils.h +++ b/dom/media/VideoUtils.h @@ -11,8 +11,6 @@ #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" @@ -20,12 +18,14 @@ #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" diff --git a/xpcom/threads/AbstractThread.h b/xpcom/threads/AbstractThread.h index 311e5441e2ae..227bd75b76b7 100644 --- a/xpcom/threads/AbstractThread.h +++ b/xpcom/threads/AbstractThread.h @@ -17,7 +17,6 @@ namespace mozilla { -class TaskQueue; class TaskDispatcher; /* diff --git a/xpcom/threads/TaskQueue.cpp b/xpcom/threads/TaskQueue.cpp index 7cc2f1e584fe..9d19612711ea 100644 --- a/xpcom/threads/TaskQueue.cpp +++ b/xpcom/threads/TaskQueue.cpp @@ -6,57 +6,10 @@ #include "mozilla/TaskQueue.h" -#include "nsISerialEventTarget.h" #include "nsThreadUtils.h" namespace mozilla { -class TaskQueue::EventTargetWrapper final : public nsISerialEventTarget { - RefPtr mTaskQueue; - - ~EventTargetWrapper() = default; - - public: - explicit EventTargetWrapper(TaskQueue* aTaskQueue) : mTaskQueue(aTaskQueue) { - MOZ_ASSERT(mTaskQueue); - } - - NS_IMETHOD - DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) override { - nsCOMPtr ref = aEvent; - return Dispatch(ref.forget(), aFlags); - } - - NS_IMETHOD - Dispatch(already_AddRefed aEvent, uint32_t aFlags) override { - nsCOMPtr runnable = aEvent; - MonitorAutoLock mon(mTaskQueue->mQueueMonitor); - return mTaskQueue->DispatchLocked(/* passed by ref */ runnable, aFlags, - NormalDispatch); - } - - NS_IMETHOD - DelayedDispatch(already_AddRefed, 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 aTarget, const char* aName, bool aRequireTailDispatch, bool aRetainFlags) @@ -180,11 +133,6 @@ bool TaskQueue::IsCurrentThreadIn() const { return in; } -already_AddRefed TaskQueue::WrapAsEventTarget() { - nsCOMPtr ref = new EventTargetWrapper(this); - return ref.forget(); -} - nsresult TaskQueue::Runner::Run() { TaskStruct event; { diff --git a/xpcom/threads/TaskQueue.h b/xpcom/threads/TaskQueue.h index 6daa2c74664b..23b1a46c44e4 100644 --- a/xpcom/threads/TaskQueue.h +++ b/xpcom/threads/TaskQueue.h @@ -9,6 +9,7 @@ #include +#include "mozilla/AbstractThread.h" #include "mozilla/Maybe.h" #include "mozilla/Monitor.h" #include "mozilla/MozPromise.h" @@ -18,9 +19,6 @@ #include "nsIDirectTaskDispatcher.h" #include "nsThreadUtils.h" -class nsIEventTarget; -class nsIRunnable; - namespace mozilla { typedef MozPromise ShutdownPromise; @@ -66,6 +64,20 @@ class TaskQueue : public AbstractThread, public nsIDirectTaskDispatcher { TaskDispatcher& TailDispatcher() override; + NS_IMETHOD Dispatch(already_AddRefed aEvent, + uint32_t aFlags) override { + nsCOMPtr 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 aRunnable, DispatchReason aReason = NormalDispatch) override { @@ -80,8 +92,8 @@ class TaskQueue : public AbstractThread, public nsIDirectTaskDispatcher { // 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 @@ -104,10 +116,6 @@ class TaskQueue : public AbstractThread, public nsIDirectTaskDispatcher { // the task queue. bool IsCurrentThreadIn() const override; - // Create a new nsIEventTarget wrapper object that dispatches to this - // TaskQueue. - already_AddRefed WrapAsEventTarget(); - protected: virtual ~TaskQueue(); diff --git a/xpcom/threads/nsThreadManager.cpp b/xpcom/threads/nsThreadManager.cpp index 854480cdfaa9..8ea41ad48935 100644 --- a/xpcom/threads/nsThreadManager.cpp +++ b/xpcom/threads/nsThreadManager.cpp @@ -187,11 +187,9 @@ BackgroundEventTarget::CreateBackgroundTaskQueue(const char* aName) { RefPtr queue = new TaskQueue(do_AddRef(this), aName, /*aSupportsTailDispatch=*/false, /*aRetainFlags=*/true); - nsCOMPtr target(queue->WrapAsEventTarget()); + mTaskQueues.AppendElement(queue); - mTaskQueues.AppendElement(queue.forget()); - - return target.forget(); + return queue.forget(); } extern "C" {