/* -*- 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/. */
// NS_IMPL_NAMED_* relies on the mName field, which is not present on // release or beta. Instead, fall back to using "Runnable" for all // runnables. # ifndef MOZ_COLLECTING_RUNNABLE_TELEMETRY
NS_IMPL_ISUPPORTS(Runnable, nsIRunnable) # else
NS_IMPL_NAMED_ADDREF(Runnable, mName)
NS_IMPL_NAMED_RELEASE(Runnable, mName)
NS_IMPL_QUERY_INTERFACE(Runnable, nsIRunnable, nsINamed) # endif
NS_IMETHODIMP
Runnable::Run() { // Do nothing return NS_OK;
}
void CancelableRunnable::OnDiscard() { // Tasks that implement Cancel() can be safely cleaned up if it turns out // that the task will not run.
(void)NS_WARN_IF(NS_FAILED(Cancel()));
}
PrioritizableRunnable::PrioritizableRunnable(
already_AddRefed<nsIRunnable>&& aRunnable, uint32_t aPriority) // Real runnable name is managed by overridding the GetName function.
: Runnable("PrioritizableRunnable"),
mRunnable(std::move(aRunnable)),
mPriority(aPriority) { # if DEBUG
nsCOMPtr<nsIRunnablePriority> runnablePrio = do_QueryInterface(mRunnable);
MOZ_ASSERT(!runnablePrio); # endif
}
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
NS_IMETHODIMP
PrioritizableRunnable::GetName(nsACString& aName) { // Try to get a name from the underlying runnable.
nsCOMPtr<nsINamed> named = do_QueryInterface(mRunnable); if (named) {
named->GetName(aName);
} return NS_OK;
} # endif
nsresult NS_DispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent) {
nsresult rv;
nsCOMPtr<nsIRunnable> event(aEvent); // XXX: Consider using GetCurrentSerialEventTarget() to support TaskQueues.
nsISerialEventTarget* thread = NS_GetCurrentThread(); if (!thread) { return NS_ERROR_UNEXPECTED;
} // To keep us from leaking the runnable if dispatch method fails, // we grab the reference on failures and release it.
nsIRunnable* temp = event.get();
rv = thread->Dispatch(event.forget(), NS_DISPATCH_NORMAL); if (NS_WARN_IF(NS_FAILED(rv))) { // Dispatch() leaked the reference to the event, but due to caller's // assumptions, we shouldn't leak here. And given we are on the same // thread as the dispatch target, it's mostly safe to do it here.
NS_RELEASE(temp);
} return rv;
}
// It is common to call NS_DispatchToCurrentThread with a newly // allocated runnable with a refcount of zero. To keep us from leaking // the runnable if the dispatch method fails, we take a death grip.
nsresult NS_DispatchToCurrentThread(nsIRunnable* aEvent) {
nsCOMPtr<nsIRunnable> event(aEvent); return NS_DispatchToCurrentThread(event.forget());
}
nsresult NS_DispatchToMainThread(already_AddRefed<nsIRunnable>&& aEvent,
uint32_t aDispatchFlags) {
LeakRefPtr<nsIRunnable> event(std::move(aEvent));
nsCOMPtr<nsIThread> thread;
nsresult rv = NS_GetMainThread(getter_AddRefs(thread)); if (NS_WARN_IF(NS_FAILED(rv))) {
NS_ASSERTION(false, "Failed NS_DispatchToMainThread() in shutdown; leaking"); // NOTE: if you stop leaking here, adjust Promise::MaybeReportRejected(), // which assumes a leak here, or split into leaks and no-leaks versions return rv;
} return thread->Dispatch(event.take(), aDispatchFlags);
}
// In the case of failure with a newly allocated runnable with a // refcount of zero, we intentionally leak the runnable, because it is // likely that the runnable is being dispatched to the main thread // because it owns main thread only objects, so it is not safe to // release them here.
nsresult NS_DispatchToMainThread(nsIRunnable* aEvent, uint32_t aDispatchFlags) {
nsCOMPtr<nsIRunnable> event(aEvent); return NS_DispatchToMainThread(event.forget(), aDispatchFlags);
}
// XXX: Consider using GetCurrentSerialEventTarget() to support TaskQueues.
nsISerialEventTarget* thread = NS_GetCurrentThread(); if (!thread) { return NS_ERROR_UNEXPECTED;
}
nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
nsIThread* aThread,
EventQueuePriority aQueue) {
nsresult rv;
nsCOMPtr<nsIRunnable> event(aEvent);
NS_ENSURE_TRUE(event, NS_ERROR_INVALID_ARG); if (!aThread) { return NS_ERROR_UNEXPECTED;
} // To keep us from leaking the runnable if dispatch method fails, // we grab the reference on failures and release it.
nsIRunnable* temp = event.get();
rv = aThread->DispatchToQueue(event.forget(), aQueue); if (NS_WARN_IF(NS_FAILED(rv))) { // Dispatch() leaked the reference to the event, but due to caller's // assumptions, we shouldn't leak here. And given we are on the same // thread as the dispatch target, it's mostly safe to do it here.
NS_RELEASE(temp);
}
class IdleRunnableWrapper final : public Runnable, public nsIDiscardableRunnable, public nsIIdleRunnable { public: explicit IdleRunnableWrapper(already_AddRefed<nsIRunnable>&& aEvent)
: Runnable("IdleRunnableWrapper"),
mRunnable(std::move(aEvent)),
mDiscardable(do_QueryInterface(mRunnable)) {}
NS_DECL_ISUPPORTS_INHERITED
NS_IMETHOD Run() override { if (!mRunnable) { return NS_OK;
}
CancelTimer(); // Don't clear mDiscardable because that would cause QueryInterface to // change behavior during the lifetime of an instance.
nsCOMPtr<nsIRunnable> runnable = std::move(mRunnable); return runnable->Run();
}
// nsIDiscardableRunnable void OnDiscard() override { if (!mRunnable) { // Run() was already called from TimedOut(). return;
}
mDiscardable->OnDiscard();
mRunnable = nullptr;
}
if (!idleEvent) {
idleEvent = new IdleRunnableWrapper(event.forget());
event = do_QueryInterface(idleEvent);
MOZ_DIAGNOSTIC_ASSERT(event);
}
idleEvent->SetTimer(aTimeout, aThread);
nsresult rv = NS_DispatchToThreadQueue(event.forget(), aThread, aQueue); if (NS_SUCCEEDED(rv)) { // This is intended to bind with the "DISP" log made from inside // NS_DispatchToThreadQueue for the `event`. There is no possibly to inject // another "DISP" for a different event on this thread.
LOG1(("TIMEOUT %u", aTimeout));
}
nsAutoLowPriorityIO::~nsAutoLowPriorityIO() { #ifdefined(XP_WIN) if (MOZ_LIKELY(lowIOPrioritySet)) { // On Windows the old thread priority is automatically restored
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
} #elifdefined(XP_MACOSX) if (MOZ_LIKELY(lowIOPrioritySet)) {
setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, oldPriority);
} #endif
}
namespace mozilla {
nsISerialEventTarget* GetCurrentSerialEventTarget() { if (nsISerialEventTarget* current =
SerialEventTargetGuard::GetCurrentSerialEventTarget()) { return current;
}
MOZ_DIAGNOSTIC_ASSERT(!nsThreadPool::GetCurrentThreadPool(), "Call to GetCurrentSerialEventTarget() from thread " "pool without an active TaskQueue");
template <typename T>
LogTaskBase<T>::Run::Run(T* aEvent, bool aWillRunAgain)
: mWillRunAgain(aWillRunAgain) { // Logging address of this RAII so that we can use it to identify the DONE log // while not keeping any ref to the event that could be invalid at the dtor // time.
LOG1(("EXEC %p %p", aEvent, this));
} template <typename T>
LogTaskBase<T>::Run::Run(T* aEvent, void* aContext, bool aWillRunAgain)
: mWillRunAgain(aWillRunAgain) {
LOG1(("EXEC %p (%p) %p", aEvent, aContext, this));
}
template <>
LogTaskBase<nsTimerImpl>::Run::Run(nsTimerImpl* aEvent, bool aWillRunAgain)
: mWillRunAgain(aWillRunAgain) { // The name of the timer will be logged when running it on the target thread. // Logging it here (on the `Timer` thread) would be redundant.
LOG1(("EXEC %p %p [nsTimerImpl]", aEvent, this));
}
extern"C" { // These functions use the C language linkage because they're exposed to Rust // via the xpcom/rust/moz_task crate, which wraps them in safe Rust functions // that enable Rust code to get/create threads and dispatch runnables on them.
// NS_NewNamedThread's aStackSize parameter has the default argument // nsIThreadManager::DEFAULT_STACK_SIZE, but we can't omit default arguments // when calling a C++ function from Rust, and we can't access // nsIThreadManager::DEFAULT_STACK_SIZE in Rust to pass it explicitly, // since it is defined in a %{C++ ... %} block within nsIThreadManager.idl. // So we indirect through this function.
nsresult NS_NewNamedThreadWithDefaultStackSize(const nsACString& aName,
nsIThread** aResult,
nsIRunnable* aEvent) { return NS_NewNamedThread(aName, aResult, aEvent);
}
nsresult NS_DispatchAndSpinEventLoopUntilComplete( const nsACString& aVeryGoodReasonToDoThis, nsIEventTarget* aEventTarget,
already_AddRefed<nsIRunnable> aEvent) { // NOTE: Get the current thread specifically, as `SpinEventLoopUntil` can // only spin that event target's loop. The reply will specify // NS_DISPATCH_IGNORE_BLOCK_DISPATCH to ensure the reply is received even if // the caller is a threadpool thread.
nsCOMPtr<nsIThread> current = NS_GetCurrentThread(); if (NS_WARN_IF(!current)) { return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<nsThreadSyncDispatch> wrapper = new nsThreadSyncDispatch(current.forget(), std::move(aEvent));
nsresult rv = aEventTarget->Dispatch(do_AddRef(wrapper)); if (NS_WARN_IF(NS_FAILED(rv))) { // FIXME: Consider avoiding leaking the `nsThreadSyncDispatch` as well by // using a fallible version of `Dispatch` once that is added. return rv;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.