script: Eliminate code duplication in the task queue (#34798)

Instead of creating a type for each `TaskSource` variety have each `TaskSource`
hold the same kind of sender (this was inconsistent before, but each
sender was effectively the same trait object), a pipeline, and a
`TaskSourceName`. This elminates the need to reimplement the same
queuing code for every task source.

In addition, have workers hold their own `TaskManager`. This allows just
exposing the manager on the `GlobalScope`. Currently the `TaskCanceller`
is different, but this will also be eliminated in a followup change.

This is a the first step toward having a shared set of `Sender`s on
`GlobalScope`.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-01 14:50:52 +01:00 committed by GitHub
parent deb819f233
commit 77cfca65c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
67 changed files with 432 additions and 1200 deletions

View file

@ -9,35 +9,22 @@ use std::sync::Arc;
use base::id::PipelineId;
use crate::dom::bindings::cell::DomRefCell;
use crate::messaging::MainThreadScriptChan;
use crate::script_runtime::ScriptChan;
use crate::task::TaskCanceller;
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::file_reading::FileReadingTaskSource;
use crate::task_source::gamepad::GamepadTaskSource;
use crate::task_source::history_traversal::HistoryTraversalTaskSource;
use crate::task_source::media_element::MediaElementTaskSource;
use crate::task_source::networking::NetworkingTaskSource;
use crate::task_source::performance_timeline::PerformanceTimelineTaskSource;
use crate::task_source::port_message::PortMessageQueue;
use crate::task_source::remote_event::RemoteEventTaskSource;
use crate::task_source::rendering::RenderingTaskSource;
use crate::task_source::timer::TimerTaskSource;
use crate::task_source::user_interaction::UserInteractionTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
use crate::task_source::TaskSourceName;
use crate::task_source::{TaskSource, TaskSourceName};
macro_rules! task_source_functions {
($self:ident, $task_source:ident, $task_source_type:ident, $task_source_name:ident) => {
pub(crate) fn $task_source(&$self) -> $task_source_type {
($self:ident, $task_source:ident) => {
pub(crate) fn $task_source(&$self) -> TaskSource {
$self.$task_source.clone()
}
};
($self:ident, $with_canceller:ident, $task_source:ident, $task_source_type:ident, $task_source_name:ident) => {
pub(crate) fn $with_canceller(&$self) -> ($task_source_type, TaskCanceller) {
($self.$task_source.clone(), $self.task_canceller(TaskSourceName::$task_source_name))
($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))
}
pub(crate) fn $task_source(&$self) -> $task_source_type {
pub(crate) fn $task_source(&$self) -> TaskSource {
$self.$task_source.clone()
}
};
@ -47,57 +34,44 @@ macro_rules! task_source_functions {
pub struct TaskManager {
#[ignore_malloc_size_of = "task sources are hard"]
pub task_cancellers: DomRefCell<HashMap<TaskSourceName, Arc<AtomicBool>>>,
#[ignore_malloc_size_of = "task sources are hard"]
dom_manipulation_task_source: DOMManipulationTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
file_reading_task_source: FileReadingTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
gamepad_task_source: GamepadTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
history_traversal_task_source: HistoryTraversalTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
media_element_task_source: MediaElementTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
networking_task_source: NetworkingTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
performance_timeline_task_source: PerformanceTimelineTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
port_message_queue: PortMessageQueue,
#[ignore_malloc_size_of = "task sources are hard"]
user_interaction_task_source: UserInteractionTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
remote_event_task_source: RemoteEventTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
rendering_task_source: RenderingTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
timer_task_source: TimerTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
websocket_task_source: WebsocketTaskSource,
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,
}
impl TaskManager {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(sender: Box<MainThreadScriptChan>, pipeline_id: PipelineId) -> Self {
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,
};
TaskManager {
dom_manipulation_task_source: DOMManipulationTaskSource(sender.clone(), pipeline_id),
file_reading_task_source: FileReadingTaskSource(sender.clone(), pipeline_id),
gamepad_task_source: GamepadTaskSource(sender.clone(), pipeline_id),
history_traversal_task_source: HistoryTraversalTaskSource(
sender.0.clone(),
pipeline_id,
),
media_element_task_source: MediaElementTaskSource(sender.0.clone(), pipeline_id),
networking_task_source: NetworkingTaskSource(sender.clone(), pipeline_id),
performance_timeline_task_source: PerformanceTimelineTaskSource(
sender.clone(),
pipeline_id,
),
port_message_queue: PortMessageQueue(sender.clone(), pipeline_id),
user_interaction_task_source: UserInteractionTaskSource(sender.0.clone(), pipeline_id),
remote_event_task_source: RemoteEventTaskSource(sender.clone(), pipeline_id),
rendering_task_source: RenderingTaskSource(sender.clone(), pipeline_id),
timer_task_source: TimerTaskSource(sender.clone(), pipeline_id),
websocket_task_source: WebsocketTaskSource(sender.clone(), pipeline_id),
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(),
}
}
@ -105,64 +79,27 @@ impl TaskManager {
task_source_functions!(
self,
dom_manipulation_task_source_with_canceller,
dom_manipulation_task_source,
DOMManipulationTaskSource,
DOMManipulation
dom_manipulation_task_source
);
task_source_functions!(self, gamepad_task_source, GamepadTaskSource, Gamepad);
task_source_functions!(self, gamepad_task_source);
task_source_functions!(
self,
media_element_task_source_with_canceller,
media_element_task_source,
MediaElementTaskSource,
MediaElement
media_element_task_source
);
task_source_functions!(
self,
user_interaction_task_source,
UserInteractionTaskSource,
UserInteraction
);
task_source_functions!(self, user_interaction_task_source);
task_source_functions!(
self,
networking_task_source_with_canceller,
networking_task_source,
NetworkingTaskSource,
Networking
networking_task_source
);
task_source_functions!(
self,
file_reading_task_source,
FileReadingTaskSource,
FileReading
);
task_source_functions!(
self,
performance_timeline_task_source,
PerformanceTimelineTaskSource,
PerformanceTimeline
);
task_source_functions!(self, port_message_queue, PortMessageQueue, PortMessage);
task_source_functions!(
self,
remote_event_task_source,
RemoteEventTaskSource,
RemoteEvent
);
task_source_functions!(self, rendering_task_source, RenderingTaskSource, Rendering);
task_source_functions!(self, timer_task_source, TimerTaskSource, Timer);
task_source_functions!(self, websocket_task_source, WebsocketTaskSource, Websocket);
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);
pub fn task_canceller(&self, name: TaskSourceName) -> TaskCanceller {
let mut flags = self.task_cancellers.borrow_mut();