gecko-dev/xpcom/threads/TaskQueue.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

284 строки
8.7 KiB
C++
Исходник Обычный вид История

/* -*- 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 "mozilla/DelayedRunnable.h"
#include "nsThreadUtils.h"
namespace mozilla {
TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
const char* aName, bool aRequireTailDispatch)
: AbstractThread(aRequireTailDispatch),
mTarget(aTarget),
mQueueMonitor("TaskQueue::Queue"),
mTailDispatcher(nullptr),
mIsRunning(false),
mIsShutdown(false),
mName(aName) {}
TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
bool aSupportsTailDispatch)
: TaskQueue(std::move(aTarget), "Unnamed", aSupportsTailDispatch) {}
TaskQueue::~TaskQueue() {
// No one is referencing this TaskQueue anymore, meaning no tasks can be
// pending as all Runner hold a reference to this TaskQueue.
MOZ_ASSERT(mScheduledDelayedRunnables.IsEmpty());
}
NS_IMPL_ISUPPORTS_INHERITED(TaskQueue, AbstractThread, nsIDirectTaskDispatcher,
nsIDelayedRunnableObserver);
TaskDispatcher& TaskQueue::TailDispatcher() {
MOZ_ASSERT(IsCurrentThreadIn());
MOZ_ASSERT(mTailDispatcher);
return *mTailDispatcher;
}
// 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) {
mQueueMonitor.AssertCurrentThreadOwns();
if (mIsShutdown) {
return NS_ERROR_FAILURE;
}
AbstractThread* currentThread;
if (aReason != TailDispatch && (currentThread = GetCurrent()) &&
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");
return currentThread->TailDispatcher().AddTask(this, aRunnable.forget());
}
LogRunnable::LogDispatch(aRunnable);
mTasks.push({std::move(aRunnable), aFlags});
if (mIsRunning) {
return NS_OK;
}
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
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);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to dispatch runnable to run TaskQueue");
return rv;
}
mIsRunning = true;
return NS_OK;
}
void TaskQueue::AwaitIdle() {
MonitorAutoLock mon(mQueueMonitor);
AwaitIdleLocked();
}
void TaskQueue::AwaitIdleLocked() {
// Make sure there are no tasks for this queue waiting in the caller's tail
// dispatcher.
MOZ_ASSERT_IF(AbstractThread::GetCurrent(),
!AbstractThread::GetCurrent()->HasTailTasksFor(this));
mQueueMonitor.AssertCurrentThreadOwns();
MOZ_ASSERT(mIsRunning || mTasks.empty());
while (mIsRunning) {
mQueueMonitor.Wait();
}
}
void TaskQueue::AwaitShutdownAndIdle() {
MOZ_ASSERT(!IsCurrentThreadIn());
// Make sure there are no tasks for this queue waiting in the caller's tail
// dispatcher.
MOZ_ASSERT_IF(AbstractThread::GetCurrent(),
!AbstractThread::GetCurrent()->HasTailTasksFor(this));
MonitorAutoLock mon(mQueueMonitor);
while (!mIsShutdown) {
mQueueMonitor.Wait();
}
AwaitIdleLocked();
}
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__);
}
RefPtr<ShutdownPromise> TaskQueue::BeginShutdown() {
// 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()) {
currentThread->TailDispatchTasksFor(this);
}
MonitorAutoLock mon(mQueueMonitor);
Unused << CancelDelayedRunnablesLocked();
mIsShutdown = true;
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
MaybeResolveShutdown();
mon.NotifyAll();
return p;
}
bool TaskQueue::IsEmpty() {
MonitorAutoLock mon(mQueueMonitor);
return mTasks.empty();
}
bool TaskQueue::IsCurrentThreadIn() const {
bool in = mRunningThread == PR_GetCurrentThread();
return in;
}
nsresult TaskQueue::Runner::Run() {
TaskStruct event;
{
MonitorAutoLock mon(mQueue->mQueueMonitor);
MOZ_ASSERT(mQueue->mIsRunning);
if (mQueue->mTasks.empty()) {
mQueue->mIsRunning = false;
mQueue->MaybeResolveShutdown();
mon.NotifyAll();
return NS_OK;
}
event = std::move(mQueue->mTasks.front());
mQueue->mTasks.pop();
}
MOZ_ASSERT(event.event);
// 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.
{
AutoTaskGuard g(mQueue);
SerialEventTargetGuard tg(mQueue);
{
LogRunnable::Run log(event.event);
event.event->Run();
// 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;
}
}
{
MonitorAutoLock mon(mQueue->mQueueMonitor);
if (mQueue->mTasks.empty()) {
// No more events to run. Exit the task runner.
mQueue->mIsRunning = false;
mQueue->MaybeResolveShutdown();
mon.NotifyAll();
return NS_OK;
}
}
// There's at least one more event that we can run. Dispatch this Runner
// 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
// run on another thread next time, but we rely on the memory fences from
// mQueueMonitor for thread safety of non-threadsafe tasks.
nsresult rv;
{
MonitorAutoLock mon(mQueue->mQueueMonitor);
rv = mQueue->mTarget->Dispatch(
this, mQueue->mTasks.front().flags | NS_DISPATCH_AT_END);
}
if (NS_FAILED(rv)) {
// Failed to dispatch, shutdown!
MonitorAutoLock mon(mQueue->mQueueMonitor);
mQueue->mIsRunning = false;
mQueue->mIsShutdown = true;
mQueue->MaybeResolveShutdown();
mon.NotifyAll();
}
return NS_OK;
}
//-----------------------------------------------------------------------------
// 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;
}
} // namespace mozilla