script: Move TaskManager to GlobalScope (#34827)

This is a simplification of the internal `TaskQueue` API that moves the
`TaskManager` to the `GlobalScope` itself. In addition, the handling of
cancellers is moved to the `TaskManager` as well. This means that no
arguments other than the `task` are necessary for queueing tasks, which
makes the API a lot easier to use and cleaner.

`TaskSource` now also keeps a copy of the canceller with it, so that
they always know the proper way to cancel any tasks queued on them.

There is one complication here. The event loop `sender` for dedicated
workers is constantly changing as it is set to `None` when not handling
messages. This is because this sender keeps a handle to the main
thread's `Worker` object, preventing garbage collection while any
messages are still in flight or being handled. This change allows
setting the `sender` on the `TaskManager` to `None` to allow proper
garbabge collection.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-04 09:41:50 +01:00 committed by GitHub
parent 75a22cfe2e
commit b2eda71952
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
54 changed files with 1060 additions and 1516 deletions

View file

@ -152,7 +152,6 @@ use crate::script_runtime::{
CanGc, CommonScriptMsg, JSContext, Runtime, ScriptChan, ScriptThreadEventCategory,
ThreadSafeJSContext,
};
use crate::task_manager::TaskManager;
use crate::task_queue::TaskQueue;
use crate::task_source::{TaskSource, TaskSourceName};
use crate::{devtools, webdriver_handlers};
@ -693,7 +692,7 @@ impl ScriptThread {
global
.task_manager()
.dom_manipulation_task_source()
.queue(task, global.upcast())
.queue(task)
.expect("Enqueing navigate js task on the DOM manipulation task source failed");
} else {
if let Some(ref sender) = script_thread.senders.devtools_server_sender {
@ -907,6 +906,7 @@ impl ScriptThread {
sender: self_sender.as_boxed(),
pipeline_id: state.id,
name: TaskSourceName::Networking,
canceller: Default::default(),
}));
let cx = runtime.cx();
@ -1033,8 +1033,10 @@ impl ScriptThread {
fn prepare_for_shutdown_inner(&self) {
let docs = self.documents.borrow();
for (_, document) in docs.iter() {
let window = document.window();
window.ignore_all_tasks();
document
.window()
.task_manager()
.cancel_all_tasks_and_ignore_future_tasks();
}
}
@ -1407,6 +1409,7 @@ impl ScriptThread {
//
// This task is empty because any new IPC messages in the ScriptThread trigger a
// rendering update when animations are not running.
let _realm = enter_realm(&*document);
let rendering_task_source = document.window().task_manager().rendering_task_source();
let _ =
rendering_task_source.queue_unconditionally(task!(update_the_rendering: move || { }));
@ -3129,10 +3132,6 @@ impl ScriptThread {
pipeline_id: incomplete.pipeline_id,
};
let task_manager = TaskManager::new(
Box::new(self.senders.self_sender.clone()),
incomplete.pipeline_id,
);
let paint_time_metrics = PaintTimeMetrics::new(
incomplete.pipeline_id,
self.senders.time_profiler_sender.clone(),
@ -3161,7 +3160,6 @@ impl ScriptThread {
let window = Window::new(
self.js_runtime.clone(),
self.senders.self_sender.clone(),
task_manager,
self.layout_factory.create(layout_config),
self.senders.image_cache_sender.clone(),
self.image_cache.clone(),