Skip to content

Commit 892767c

Browse files
committed
Bug 1593802 - don't drop dispatch flags in TaskQueue's EventTargetWrapper; r=erahm
`TaskQueue` wraps an `nsIEventTarget` to provide "one runnable at a time" execution policies regardless of the underlying implementation of the wrapped `nsIEventTarget` (e.g. a thread pool). `TaskQueue` also provides a `nsISerialEventTarget` wrapper, `EventTargetWrapper`, around itself (!) for consumers who want to continue to provide a more XPCOM-ish API. One would think that dispatching tasks to `EventTargetWrapper` with a given set of flags would pass that set of flags through, unchanged, to the underlying event target of the wrapped `TaskQueue`. This pass-through is not the case. `TaskQueue` supports a "tail dispatch" mode of operation that is somewhat underdocumented. Roughly, tail dispatch to a `TaskQueue` says (with several other conditions) that dispatched tasks are held separately and not passed through to the underlying event target. If a given `TaskQueue` supports tail dispatch and the dispatcher also supports tail dispatch, any tasks dispatched to said `TaskQueue` are silently converted to tail dispatched tasks. Since tail dispatched tasks can't meaningfully have flags associated with them, the current implementation simply drops any passed flags on the floor. These flags, however, might be meaningful, and we should attempt to honor them in the cases we're not doing tail dispatch. (And when we are doing tail dispatch, we can verify that the requested flags are not asking for peculiar things.) Differential Revision: https://phabricator.services.mozilla.com/D51702 --HG-- extra : moz-landing-system : lando
1 parent 0aee355 commit 892767c

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

xpcom/threads/TaskQueue.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TaskQueue::EventTargetWrapper final : public nsISerialEventTarget {
3131
Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) override {
3232
nsCOMPtr<nsIRunnable> runnable = aEvent;
3333
MonitorAutoLock mon(mTaskQueue->mQueueMonitor);
34-
return mTaskQueue->DispatchLocked(/* passed by ref */ runnable,
34+
return mTaskQueue->DispatchLocked(/* passed by ref */ runnable, aFlags,
3535
NormalDispatch);
3636
}
3737

@@ -85,6 +85,7 @@ TaskDispatcher& TaskQueue::TailDispatcher() {
8585
// Note aRunnable is passed by ref to support conditional ownership transfer.
8686
// See Dispatch() in TaskQueue.h for more details.
8787
nsresult TaskQueue::DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
88+
uint32_t aFlags,
8889
DispatchReason aReason) {
8990
mQueueMonitor.AssertCurrentThreadOwns();
9091
if (mIsShutdown) {
@@ -94,6 +95,8 @@ nsresult TaskQueue::DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
9495
AbstractThread* currentThread;
9596
if (aReason != TailDispatch && (currentThread = GetCurrent()) &&
9697
RequiresTailDispatch(currentThread)) {
98+
MOZ_ASSERT(aFlags == NS_DISPATCH_NORMAL,
99+
"Tail dispatch doesn't support flags");
97100
return currentThread->TailDispatcher().AddTask(this, aRunnable.forget());
98101
}
99102

@@ -102,7 +105,7 @@ nsresult TaskQueue::DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
102105
return NS_OK;
103106
}
104107
RefPtr<nsIRunnable> runner(new Runner(this));
105-
nsresult rv = mTarget->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
108+
nsresult rv = mTarget->Dispatch(runner.forget(), aFlags);
106109
if (NS_FAILED(rv)) {
107110
NS_WARNING("Failed to dispatch runnable to run TaskQueue");
108111
return rv;

xpcom/threads/TaskQueue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TaskQueue : public AbstractThread {
6767
nsCOMPtr<nsIRunnable> r = aRunnable;
6868
{
6969
MonitorAutoLock mon(mQueueMonitor);
70-
return DispatchLocked(/* passed by ref */ r, aReason);
70+
return DispatchLocked(/* passed by ref */ r, NS_DISPATCH_NORMAL, aReason);
7171
}
7272
// If the ownership of |r| is not transferred in DispatchLocked() due to
7373
// dispatch failure, it will be deleted here outside the lock. We do so
@@ -111,7 +111,7 @@ class TaskQueue : public AbstractThread {
111111
// mQueueMonitor must be held.
112112
void AwaitIdleLocked();
113113

114-
nsresult DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
114+
nsresult DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable, uint32_t aFlags,
115115
DispatchReason aReason = NormalDispatch);
116116

117117
void MaybeResolveShutdown() {

0 commit comments

Comments
 (0)