2014-02-05 05:29:26 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
|
2021-04-06 23:15:11 +03:00
|
|
|
#include "mozilla/DelayedRunnable.h"
|
2014-02-05 05:29:26 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
|
2020-06-22 16:55:23 +03:00
|
|
|
const char* aName, bool aRequireTailDispatch)
|
2015-04-14 09:53:07 +03:00
|
|
|
: AbstractThread(aRequireTailDispatch),
|
2016-09-14 06:12:15 +03:00
|
|
|
mTarget(aTarget),
|
2015-07-16 21:13:49 +03:00
|
|
|
mQueueMonitor("TaskQueue::Queue"),
|
2015-04-06 21:24:16 +03:00
|
|
|
mTailDispatcher(nullptr),
|
2014-02-05 05:29:26 +04:00
|
|
|
mIsRunning(false),
|
|
|
|
mIsShutdown(false),
|
2017-04-25 09:57:55 +03:00
|
|
|
mName(aName) {}
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2017-04-25 09:57:55 +03:00
|
|
|
TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
|
2020-06-22 16:55:23 +03:00
|
|
|
bool aSupportsTailDispatch)
|
2018-05-30 22:15:35 +03:00
|
|
|
: TaskQueue(std::move(aTarget), "Unnamed", aSupportsTailDispatch) {}
|
2017-04-25 09:57:55 +03:00
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::~TaskQueue() {
|
2018-06-07 15:46:28 +03:00
|
|
|
// No one is referencing this TaskQueue anymore, meaning no tasks can be
|
|
|
|
// pending as all Runner hold a reference to this TaskQueue.
|
2021-04-06 23:15:11 +03:00
|
|
|
MOZ_ASSERT(mScheduledDelayedRunnables.IsEmpty());
|
2014-02-05 05:29:26 +04:00
|
|
|
}
|
|
|
|
|
2021-04-06 23:15:11 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(TaskQueue, AbstractThread, nsIDirectTaskDispatcher,
|
|
|
|
nsIDelayedRunnableObserver);
|
2020-06-12 08:10:25 +03:00
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskDispatcher& TaskQueue::TailDispatcher() {
|
2015-04-06 21:24:16 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(mTailDispatcher);
|
|
|
|
return *mTailDispatcher;
|
|
|
|
}
|
|
|
|
|
2016-03-18 06:27:15 +03:00
|
|
|
// Note aRunnable is passed by ref to support conditional ownership transfer.
|
|
|
|
// See Dispatch() in TaskQueue.h for more details.
|
|
|
|
nsresult TaskQueue::DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
|
|
|
|
uint32_t aFlags, DispatchReason aReason) {
|
2016-11-10 10:45:45 +03:00
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mIsShutdown) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-04-15 20:54:25 +03:00
|
|
|
AbstractThread* currentThread;
|
2015-05-14 04:47:42 +03:00
|
|
|
if (aReason != TailDispatch && (currentThread = GetCurrent()) &&
|
2020-04-29 00:18:21 +03:00
|
|
|
RequiresTailDispatch(currentThread) &&
|
|
|
|
currentThread->IsTailDispatcherAvailable()) {
|
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
2019-11-05 19:59:30 +03:00
|
|
|
MOZ_ASSERT(aFlags == NS_DISPATCH_NORMAL,
|
|
|
|
"Tail dispatch doesn't support flags");
|
2017-11-15 09:58:02 +03:00
|
|
|
return currentThread->TailDispatcher().AddTask(this, aRunnable.forget());
|
2015-04-15 20:54:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-12 15:48:49 +03:00
|
|
|
LogRunnable::LogDispatch(aRunnable);
|
2020-06-22 16:55:23 +03:00
|
|
|
mTasks.push({std::move(aRunnable), aFlags});
|
2019-12-12 01:26:06 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
if (mIsRunning) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> runner(new Runner(this));
|
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
2019-11-05 19:59:30 +03:00
|
|
|
nsresult rv = mTarget->Dispatch(runner.forget(), aFlags);
|
2014-02-05 05:29:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-07-16 21:13:49 +03:00
|
|
|
NS_WARNING("Failed to dispatch runnable to run TaskQueue");
|
2014-02-05 05:29:26 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mIsRunning = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
void TaskQueue::AwaitIdle() {
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
void TaskQueue::AwaitIdleLocked() {
|
2015-04-29 01:15:49 +03:00
|
|
|
// Make sure there are no tasks for this queue waiting in the caller's tail
|
2015-04-16 19:20:22 +03:00
|
|
|
// dispatcher.
|
|
|
|
MOZ_ASSERT_IF(AbstractThread::GetCurrent(),
|
2016-09-21 12:24:43 +03:00
|
|
|
!AbstractThread::GetCurrent()->HasTailTasksFor(this));
|
2015-04-16 19:20:22 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mIsRunning || mTasks.empty());
|
|
|
|
while (mIsRunning) {
|
|
|
|
mQueueMonitor.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
void TaskQueue::AwaitShutdownAndIdle() {
|
2016-05-12 07:53:19 +03:00
|
|
|
MOZ_ASSERT(!IsCurrentThreadIn());
|
2015-04-29 01:15:49 +03:00
|
|
|
// Make sure there are no tasks for this queue waiting in the caller's tail
|
2015-04-16 19:20:22 +03:00
|
|
|
// dispatcher.
|
|
|
|
MOZ_ASSERT_IF(AbstractThread::GetCurrent(),
|
2016-09-21 12:24:43 +03:00
|
|
|
!AbstractThread::GetCurrent()->HasTailTasksFor(this));
|
2015-04-16 19:20:22 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2014-12-02 08:51:02 +03:00
|
|
|
while (!mIsShutdown) {
|
|
|
|
mQueueMonitor.Wait();
|
|
|
|
}
|
2014-02-05 05:29:26 +04:00
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
2021-04-06 23:15:11 +03:00
|
|
|
void TaskQueue::OnDelayedRunnableCreated(DelayedRunnable* aRunnable) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
MOZ_ASSERT(!mDelayedRunnablesCancelPromise);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::OnDelayedRunnableScheduled(DelayedRunnable* aRunnable) {
|
|
|
|
MOZ_ASSERT(IsOnCurrentThread());
|
|
|
|
mScheduledDelayedRunnables.AppendElement(aRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::OnDelayedRunnableRan(DelayedRunnable* aRunnable) {
|
|
|
|
MOZ_ASSERT(IsOnCurrentThread());
|
|
|
|
MOZ_ALWAYS_TRUE(mScheduledDelayedRunnables.RemoveElement(aRunnable));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto TaskQueue::CancelDelayedRunnables() -> RefPtr<CancelPromise> {
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return CancelDelayedRunnablesLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto TaskQueue::CancelDelayedRunnablesLocked() -> RefPtr<CancelPromise> {
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mDelayedRunnablesCancelPromise) {
|
|
|
|
return mDelayedRunnablesCancelPromise;
|
|
|
|
}
|
|
|
|
mDelayedRunnablesCancelPromise =
|
|
|
|
mDelayedRunnablesCancelHolder.Ensure(__func__);
|
|
|
|
nsCOMPtr<nsIRunnable> cancelRunnable =
|
|
|
|
NewRunnableMethod("TaskQueue::CancelDelayedRunnablesImpl", this,
|
|
|
|
&TaskQueue::CancelDelayedRunnablesImpl);
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(DispatchLocked(/* passed by ref */ cancelRunnable,
|
|
|
|
NS_DISPATCH_NORMAL, TailDispatch));
|
|
|
|
return mDelayedRunnablesCancelPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::CancelDelayedRunnablesImpl() {
|
|
|
|
MOZ_ASSERT(IsOnCurrentThread());
|
|
|
|
for (const auto& runnable : mScheduledDelayedRunnables) {
|
|
|
|
runnable->CancelTimer();
|
|
|
|
}
|
|
|
|
mScheduledDelayedRunnables.Clear();
|
|
|
|
mDelayedRunnablesCancelHolder.Resolve(true, __func__);
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
RefPtr<ShutdownPromise> TaskQueue::BeginShutdown() {
|
2015-06-16 02:19:32 +03:00
|
|
|
// Dispatch any tasks for this queue waiting in the caller's tail dispatcher,
|
|
|
|
// since this is the last opportunity to do so.
|
|
|
|
if (AbstractThread* currentThread = AbstractThread::GetCurrent()) {
|
2016-09-21 12:24:43 +03:00
|
|
|
currentThread->TailDispatchTasksFor(this);
|
2015-06-16 02:19:32 +03:00
|
|
|
}
|
2014-12-02 08:51:02 +03:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2021-04-06 23:15:11 +03:00
|
|
|
Unused << CancelDelayedRunnablesLocked();
|
2014-12-02 08:51:02 +03:00
|
|
|
mIsShutdown = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
|
2015-06-11 06:06:09 +03:00
|
|
|
MaybeResolveShutdown();
|
2014-12-02 08:51:02 +03:00
|
|
|
mon.NotifyAll();
|
2014-12-09 22:43:21 +03:00
|
|
|
return p;
|
2014-12-02 08:51:02 +03:00
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
bool TaskQueue::IsEmpty() {
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return mTasks.empty();
|
|
|
|
}
|
|
|
|
|
2018-11-12 04:09:03 +03:00
|
|
|
bool TaskQueue::IsCurrentThreadIn() const {
|
2019-12-12 03:56:53 +03:00
|
|
|
bool in = mRunningThread == PR_GetCurrentThread();
|
2015-04-07 21:26:35 +03:00
|
|
|
return in;
|
2014-02-18 02:53:52 +04:00
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
nsresult TaskQueue::Runner::Run() {
|
2019-12-12 01:26:06 +03:00
|
|
|
TaskStruct event;
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mQueue->mIsRunning);
|
2017-08-03 13:08:32 +03:00
|
|
|
if (mQueue->mTasks.empty()) {
|
2014-02-05 05:29:26 +04:00
|
|
|
mQueue->mIsRunning = false;
|
2015-06-11 06:06:09 +03:00
|
|
|
mQueue->MaybeResolveShutdown();
|
2014-02-05 05:29:26 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-12-12 01:26:06 +03:00
|
|
|
event = std::move(mQueue->mTasks.front());
|
2014-02-05 05:29:26 +04:00
|
|
|
mQueue->mTasks.pop();
|
|
|
|
}
|
2019-12-12 01:26:06 +03:00
|
|
|
MOZ_ASSERT(event.event);
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2020-06-24 16:51:25 +03:00
|
|
|
// Note that dropping the queue monitor before running the task, and
|
|
|
|
// taking the monitor again after the task has run ensures we have memory
|
|
|
|
// fences enforced. This means that if the object we're calling wasn't
|
|
|
|
// designed to be threadsafe, it will be, provided we're only calling it
|
|
|
|
// in this task queue.
|
2015-04-07 21:26:35 +03:00
|
|
|
{
|
2020-06-24 16:51:25 +03:00
|
|
|
AutoTaskGuard g(mQueue);
|
|
|
|
SerialEventTargetGuard tg(mQueue);
|
2020-05-12 15:48:49 +03:00
|
|
|
{
|
2020-06-24 16:51:25 +03:00
|
|
|
LogRunnable::Run log(event.event);
|
|
|
|
|
2020-05-12 15:48:49 +03:00
|
|
|
event.event->Run();
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2020-06-24 16:51:25 +03:00
|
|
|
// Drop the reference to event. The event will hold a reference to the
|
|
|
|
// object it's calling, and we don't want to keep it alive, it may be
|
|
|
|
// making assumptions what holds references to it. This is especially
|
|
|
|
// the case if the object is waiting for us to shutdown, so that it
|
|
|
|
// can shutdown (like in the MediaDecoderStateMachine's SHUTDOWN case).
|
|
|
|
event.event = nullptr;
|
|
|
|
}
|
2020-05-12 15:48:49 +03:00
|
|
|
}
|
2014-02-18 02:53:52 +04:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
2017-08-03 13:08:32 +03:00
|
|
|
if (mQueue->mTasks.empty()) {
|
2014-02-05 05:29:26 +04:00
|
|
|
// No more events to run. Exit the task runner.
|
|
|
|
mQueue->mIsRunning = false;
|
2015-06-11 06:06:09 +03:00
|
|
|
mQueue->MaybeResolveShutdown();
|
2014-02-05 05:29:26 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's at least one more event that we can run. Dispatch this Runner
|
2016-09-14 06:12:15 +03:00
|
|
|
// to the target again to ensure it runs again. Note that we don't just
|
|
|
|
// run in a loop here so that we don't hog the target. This means we may
|
2014-02-05 05:29:26 +04:00
|
|
|
// run on another thread next time, but we rely on the memory fences from
|
|
|
|
// mQueueMonitor for thread safety of non-threadsafe tasks.
|
2019-12-12 01:26:06 +03:00
|
|
|
nsresult rv;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
rv = mQueue->mTarget->Dispatch(
|
|
|
|
this, mQueue->mTasks.front().flags | NS_DISPATCH_AT_END);
|
|
|
|
}
|
2015-04-10 21:52:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Failed to dispatch, shutdown!
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
2015-04-10 21:52:08 +03:00
|
|
|
mQueue->mIsRunning = false;
|
|
|
|
mQueue->mIsShutdown = true;
|
2015-06-11 06:06:09 +03:00
|
|
|
mQueue->MaybeResolveShutdown();
|
2015-04-10 21:52:08 +03:00
|
|
|
mon.NotifyAll();
|
2014-02-05 05:29:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-12 08:10:25 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIDirectTaskDispatcher
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TaskQueue::DispatchDirectTask(already_AddRefed<nsIRunnable> aEvent) {
|
|
|
|
if (!IsCurrentThreadIn()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mDirectTasks.AddTask(std::move(aEvent));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP TaskQueue::DrainDirectTasks() {
|
|
|
|
if (!IsCurrentThreadIn()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mDirectTasks.DrainTasks();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP TaskQueue::HaveDirectTasks(bool* aValue) {
|
|
|
|
if (!IsCurrentThreadIn()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aValue = mDirectTasks.HaveTasks();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
} // namespace mozilla
|