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

@ -2,110 +2,148 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use core::cell::RefCell;
use core::sync::atomic::Ordering;
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use base::id::PipelineId;
use crate::dom::bindings::cell::DomRefCell;
use crate::script_runtime::ScriptChan;
use crate::task::TaskCanceller;
use crate::task_source::{TaskSource, TaskSourceName};
macro_rules! task_source_functions {
($self:ident, $task_source:ident) => {
pub(crate) fn $task_source(&$self) -> TaskSource {
$self.$task_source.clone()
}
};
($self:ident, $with_canceller:ident, $task_source:ident) => {
pub(crate) fn $with_canceller(&$self) -> (TaskSource, TaskCanceller) {
($self.$task_source.clone(), $self.task_canceller($self.$task_source.name))
}
#[derive(JSTraceable, MallocSizeOf)]
enum TaskCancellers {
/// A shared canceller that is used for workers, which can create multiple TaskManagers, but all
/// of them need to have the same canceller flag for all task sources.
Shared(TaskCanceller),
/// For `Window` each `TaskSource` has its own canceller.
OnePerTaskSource(RefCell<HashMap<TaskSourceName, TaskCanceller>>),
}
impl TaskCancellers {
fn get(&self, name: TaskSourceName) -> TaskCanceller {
match self {
Self::Shared(canceller) => canceller.clone(),
Self::OnePerTaskSource(map) => map.borrow_mut().entry(name).or_default().clone(),
}
}
fn cancel_all_tasks_and_ignore_future_tasks(&self) {
match self {
Self::Shared(canceller) => canceller.cancelled.store(true, Ordering::SeqCst),
Self::OnePerTaskSource(..) => {
// We must create the canceller if they aren't created because we want future
// tasks to be ignored completely.
for task_source_name in TaskSourceName::all() {
self.get(*task_source_name)
.cancelled
.store(true, Ordering::SeqCst)
}
},
}
}
fn cancel_pending_tasks_for_source(&self, task_source_name: TaskSourceName) {
let Self::OnePerTaskSource(map) = self else {
unreachable!(
"It isn't possible to cancel pending tasks for Worker \
TaskManager's without ignoring future tasks."
)
};
// Remove the canceller from the map so that the next time a task like this is
// queued, it has a fresh, uncancelled canceller.
if let Some(canceller) = map.borrow_mut().remove(&task_source_name) {
// Cancel any tasks that use the current canceller.
canceller.cancelled.store(true, Ordering::SeqCst);
}
}
}
macro_rules! task_source_functions {
($self:ident, $task_source:ident, $task_source_name:ident) => {
pub(crate) fn $task_source(&$self) -> TaskSource {
$self.$task_source.clone()
$self.task_source_for_task_source_name(TaskSourceName::$task_source_name)
}
};
}
#[derive(JSTraceable, MallocSizeOf)]
pub struct TaskManager {
#[ignore_malloc_size_of = "task sources are hard"]
pub task_cancellers: DomRefCell<HashMap<TaskSourceName, Arc<AtomicBool>>>,
dom_manipulation_task_source: TaskSource,
file_reading_task_source: TaskSource,
gamepad_task_source: TaskSource,
history_traversal_task_source: TaskSource,
media_element_task_source: TaskSource,
networking_task_source: TaskSource,
performance_timeline_task_source: TaskSource,
port_message_queue: TaskSource,
user_interaction_task_source: TaskSource,
remote_event_task_source: TaskSource,
rendering_task_source: TaskSource,
timer_task_source: TaskSource,
websocket_task_source: TaskSource,
#[ignore_malloc_size_of = "We need to push the measurement of this down into the ScriptChan trait"]
sender: RefCell<Option<Box<dyn ScriptChan + Send>>>,
#[no_trace]
pipeline_id: PipelineId,
cancellers: TaskCancellers,
}
impl TaskManager {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(sender: Box<dyn ScriptChan + Send>, pipeline_id: PipelineId) -> Self {
let task_source = |name| TaskSource {
sender: sender.as_boxed(),
pipeline_id,
name,
pub(crate) fn new(
sender: Option<Box<dyn ScriptChan + Send>>,
pipeline_id: PipelineId,
shared_canceller: Option<TaskCanceller>,
) -> Self {
let cancellers = match shared_canceller {
Some(shared_canceller) => TaskCancellers::Shared(shared_canceller),
None => TaskCancellers::OnePerTaskSource(Default::default()),
};
let sender = RefCell::new(sender);
TaskManager {
dom_manipulation_task_source: task_source(TaskSourceName::DOMManipulation),
file_reading_task_source: task_source(TaskSourceName::FileReading),
gamepad_task_source: task_source(TaskSourceName::Gamepad),
history_traversal_task_source: task_source(TaskSourceName::HistoryTraversal),
media_element_task_source: task_source(TaskSourceName::MediaElement),
networking_task_source: task_source(TaskSourceName::Networking),
performance_timeline_task_source: task_source(TaskSourceName::PerformanceTimeline),
port_message_queue: task_source(TaskSourceName::PortMessage),
user_interaction_task_source: task_source(TaskSourceName::UserInteraction),
remote_event_task_source: task_source(TaskSourceName::RemoteEvent),
rendering_task_source: task_source(TaskSourceName::Rendering),
timer_task_source: task_source(TaskSourceName::Timer),
websocket_task_source: task_source(TaskSourceName::WebSocket),
task_cancellers: Default::default(),
sender,
pipeline_id,
cancellers,
}
}
task_source_functions!(
self,
dom_manipulation_task_source_with_canceller,
dom_manipulation_task_source
);
task_source_functions!(self, gamepad_task_source);
task_source_functions!(
self,
media_element_task_source_with_canceller,
media_element_task_source
);
task_source_functions!(self, user_interaction_task_source);
task_source_functions!(
self,
networking_task_source_with_canceller,
networking_task_source
);
task_source_functions!(self, file_reading_task_source);
task_source_functions!(self, performance_timeline_task_source);
task_source_functions!(self, port_message_queue);
task_source_functions!(self, remote_event_task_source);
task_source_functions!(self, rendering_task_source);
task_source_functions!(self, timer_task_source);
task_source_functions!(self, websocket_task_source);
fn task_source_for_task_source_name(&self, name: TaskSourceName) -> TaskSource {
let Some(sender) = self
.sender
.borrow()
.as_ref()
.map(|sender| sender.as_boxed())
else {
unreachable!("Tried to enqueue task for DedicatedWorker while not handling a message.")
};
pub fn task_canceller(&self, name: TaskSourceName) -> TaskCanceller {
let mut flags = self.task_cancellers.borrow_mut();
let cancel_flag = flags.entry(name).or_default();
TaskCanceller {
cancelled: cancel_flag.clone(),
TaskSource {
sender,
pipeline_id: self.pipeline_id,
name,
canceller: self.cancellers.get(name),
}
}
/// Update the sender for this [`TaskSource`]. This is used by dedicated workers, which only have a
/// sender while handling messages (as their sender prevents the main thread Worker object from being
/// garbage collected).
pub(crate) fn set_sender(&self, sender: Option<Box<dyn ScriptChan + Send>>) {
*self.sender.borrow_mut() = sender;
}
/// Cancel all queued but unexecuted tasks and ignore all subsequently queued tasks.
pub(crate) fn cancel_all_tasks_and_ignore_future_tasks(&self) {
self.cancellers.cancel_all_tasks_and_ignore_future_tasks();
}
/// Cancel all queued but unexecuted tasks for the given task source, but subsequently queued
/// tasks will not be ignored.
pub(crate) fn cancel_pending_tasks_for_source(&self, task_source_name: TaskSourceName) {
self.cancellers
.cancel_pending_tasks_for_source(task_source_name);
}
task_source_functions!(self, dom_manipulation_task_source, DOMManipulation);
task_source_functions!(self, file_reading_task_source, FileReading);
task_source_functions!(self, gamepad_task_source, Gamepad);
task_source_functions!(self, media_element_task_source, MediaElement);
task_source_functions!(self, networking_task_source, Networking);
task_source_functions!(self, performance_timeline_task_source, PerformanceTimeline);
task_source_functions!(self, port_message_queue, PortMessage);
task_source_functions!(self, remote_event_task_source, RemoteEvent);
task_source_functions!(self, rendering_task_source, Rendering);
task_source_functions!(self, timer_task_source, Timer);
task_source_functions!(self, user_interaction_task_source, UserInteraction);
task_source_functions!(self, websocket_task_source, WebSocket);
}