gecko-dev/dom/webscheduling/WebTaskSchedulerWorker.cpp

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

109 строки
3.3 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 "WebTaskSchedulerWorker.h"
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/dom/TimeoutManager.h"
namespace mozilla::dom {
WebTaskWorkerRunnable::WebTaskWorkerRunnable(
WorkerPrivate* aWorkerPrivate, WebTaskSchedulerWorker* aSchedulerWorker)
Bug 1769913 - P3 Remove WorkerRunnable::mWorkerPrivate. r=dom-worker-reviewers,asuth WorkerRunnable no longer keeps a raw pointer(mWorkerPrivate) for the associated WorkerPrivate in this patch. Removing the WorkerRunnable::mWorkerPrivate needs to fix the following problems. 1. Thread assertions in WorkerRunnable::Dispatch() To fix this problem, the associated WorkerPrivate is as a parameter and passed to WorkerRunnable::Dispatch() for the dispatching thread assertions. This associated WorkerPrivate is also propagated to PreDispatch() and PostDispatch() for the children classes of WorkerRunnable() 2. Get the associated WorkerPrivate in WorkerRunnable::Run() for environment setup(GlobabObject, JSContext setting for the runnable) - For WorkerThreadRunnable Since WorkerThreadRunnable is supposed to run on the worker thread, it does not need to keep a raw pointer to WorkerPrivate as its class member. GetCurrentThreadWorkerPrivate() should always get the correct WorkerPrivate for WorkerThreadRunnable. - For WorkerParentThreadRunnable WorkerParentRef is introduced to keep a RefPtr<WorkerPrivate> for WorkerParentThreadRunnable instead of using a raw pointer. Checking the associated WorkerPrivate existence by WorkerParentRef at the beginning of WorkerParentThreadRunnable::Run(). If the Worker has already shut down, WorkerParentThreadRunnable cannot do anything with the associated WorkerPrivate, so WorkerParentThreadRunnable::Run() will return NS_OK directly but with a warning. The associated WorkerPrivate is also passed into WorkerRun(), PreRun(), and PostRun(), so the majority of implementations of child classes of WorkerRunnable do not need to be changed. If there are any cases in which the child classes of WorkerThreadRunnable/WorkerParentThreadRunnable want to keep the associated WorkerPrivate, they should use WorkerRefs instead of raw pointers. Depends on D205679 Differential Revision: https://phabricator.services.mozilla.com/D207039
2024-04-19 12:41:58 +03:00
: WorkerSameThreadRunnable("WebTaskWorkerRunnable"),
mSchedulerWorker(aSchedulerWorker) {
MOZ_ASSERT(mSchedulerWorker);
}
RefPtr<WebTaskSchedulerWorker> WebTaskSchedulerWorker::Create(
WorkerPrivate* aWorkerPrivate) {
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
RefPtr<WebTaskSchedulerWorker> scheduler =
MakeRefPtr<WebTaskSchedulerWorker>(aWorkerPrivate);
scheduler->mWorkerRef = StrongWorkerRef::Create(
aWorkerPrivate, "WebTaskSchedulerWorker", [scheduler]() {
// Set mWorkerIsShuttingDown as true here to avoid dispatching tasks
// to worker thread.
scheduler->mWorkerIsShuttingDown = true;
});
if (!scheduler->mWorkerRef) {
NS_WARNING("Create WebTaskScheduler when Worker is shutting down");
scheduler->mWorkerIsShuttingDown = true;
}
return scheduler;
}
WebTaskSchedulerWorker::WebTaskSchedulerWorker(WorkerPrivate* aWorkerPrivate)
: WebTaskScheduler(aWorkerPrivate->GlobalScope()) {}
bool WebTaskWorkerRunnable::WorkerRun(JSContext* aCx,
WorkerPrivate* aWorkerPrivate) {
aWorkerPrivate->AssertIsOnWorkerThread();
if (mSchedulerWorker) {
RefPtr<WebTask> task = mSchedulerWorker->GetNextTask();
if (task) {
task->Run();
}
}
return true;
}
nsresult WebTaskSchedulerWorker::SetTimeoutForDelayedTask(WebTask* aTask,
uint64_t aDelay) {
if (mWorkerIsShuttingDown) {
return NS_ERROR_ABORT;
}
if (!mWorkerRef) {
return NS_ERROR_UNEXPECTED;
}
WorkerPrivate* workerPrivate = mWorkerRef->Private();
MOZ_ASSERT(workerPrivate);
workerPrivate->AssertIsOnWorkerThread();
JSContext* cx = nsContentUtils::GetCurrentJSContext();
if (!cx) {
return NS_ERROR_UNEXPECTED;
}
RefPtr<DelayedWebTaskHandler> handler =
new DelayedWebTaskHandler(cx, this, aTask);
ErrorResult rv;
int32_t delay = aDelay > INT32_MAX ? INT32_MAX : (int32_t)aDelay;
workerPrivate->SetTimeout(cx, handler, delay,
/* aIsInterval */ false,
Timeout::Reason::eDelayedWebTaskTimeout, rv);
return rv.StealNSResult();
}
bool WebTaskSchedulerWorker::DispatchEventLoopRunnable() {
if (mWorkerIsShuttingDown) {
return false;
}
if (!mWorkerRef) {
return false;
}
MOZ_ASSERT(mWorkerRef->Private());
mWorkerRef->Private()->AssertIsOnWorkerThread();
RefPtr<WebTaskWorkerRunnable> runnable =
new WebTaskWorkerRunnable(mWorkerRef->Private(), this);
return runnable->Dispatch(mWorkerRef->Private());
}
void WebTaskSchedulerWorker::Disconnect() {
if (mWorkerRef) {
mWorkerRef = nullptr;
}
WebTaskScheduler::Disconnect();
}
} // namespace mozilla::dom