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

@ -306,10 +306,7 @@ impl FakeXRDeviceMethods<crate::DomTypeHolder> for FakeXRDevice {
let global = self.global();
let p = Promise::new(&global, can_gc);
let mut trusted = Some(TrustedPromise::new(p.clone()));
let (task_source, canceller) = global
.as_window()
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
ROUTER.add_typed_route(
@ -318,7 +315,7 @@ impl FakeXRDeviceMethods<crate::DomTypeHolder> for FakeXRDevice {
let trusted = trusted
.take()
.expect("disconnect callback called multiple times");
let _ = task_source.queue_with_canceller(trusted.resolve_task(()), &canceller);
let _ = task_source.queue(trusted.resolve_task(()));
}),
);
self.disconnect(sender);

View file

@ -201,22 +201,16 @@ impl XRSession {
fn setup_raf_loop(&self, frame_receiver: IpcReceiver<Frame>) {
let this = Trusted::new(self);
let global = self.global();
let window = global.as_window();
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
ROUTER.add_typed_route(
frame_receiver,
Box::new(move |message| {
let frame: Frame = message.unwrap();
let time = CrossProcessInstant::now();
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(xr_raf_callback: move || {
this.root().raf_callback(frame, time);
}),
&canceller,
);
let _ = task_source.queue(task!(xr_raf_callback: move || {
this.root().raf_callback(frame, time);
}));
}),
);
@ -230,22 +224,16 @@ impl XRSession {
fn attach_event_handler(&self) {
let this = Trusted::new(self);
let global = self.global();
let window = global.as_window();
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
ROUTER.add_typed_route(
receiver.to_ipc_receiver(),
Box::new(move |message| {
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(xr_event_callback: move || {
this.root().event_callback(message.unwrap(), CanGc::note());
}),
&canceller,
);
let _ = task_source.queue(task!(xr_event_callback: move || {
this.root().event_callback(message.unwrap(), CanGc::note());
}));
}),
);
@ -266,21 +254,14 @@ impl XRSession {
return;
}
let global = self.global();
let window = global.as_window();
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = self.global().task_manager().dom_manipulation_task_source();
let this = Trusted::new(self);
// Queue a task so that it runs after resolve()'s microtasks complete
// so that content has a chance to attach a listener for inputsourceschange
let _ = task_source.queue_with_canceller(
task!(session_initial_inputs: move || {
let this = this.root();
this.input_sources.add_input_sources(&this, &initial_inputs, CanGc::note());
}),
&canceller,
);
let _ = task_source.queue(task!(session_initial_inputs: move || {
let this = this.root();
this.input_sources.add_input_sources(&this, &initial_inputs, CanGc::note());
}));
}
fn event_callback(&self, event: XREvent, can_gc: CanGc) {
@ -1055,26 +1036,20 @@ impl XRSessionMethods<crate::DomTypeHolder> for XRSession {
let this = Trusted::new(self);
let global = self.global();
let window = global.as_window();
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
ROUTER.add_typed_route(
receiver.to_ipc_receiver(),
Box::new(move |message| {
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(update_session_framerate: move || {
let session = this.root();
session.apply_nominal_framerate(message.unwrap(), CanGc::note());
if let Some(promise) = session.update_framerate_promise.borrow_mut().take() {
promise.resolve_native(&());
};
}),
&canceller,
);
let _ = task_source.queue(task!(update_session_framerate: move || {
let session = this.root();
session.apply_nominal_framerate(message.unwrap(), CanGc::note());
if let Some(promise) = session.update_framerate_promise.borrow_mut().take() {
promise.resolve_native(&());
};
}));
}),
);

View file

@ -118,10 +118,7 @@ impl XRSystemMethods<crate::DomTypeHolder> for XRSystem {
let promise = Promise::new(&self.global(), can_gc);
let mut trusted = Some(TrustedPromise::new(promise.clone()));
let global = self.global();
let window = global.as_window();
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
ROUTER.add_typed_route(
receiver.to_ipc_receiver(),
@ -140,15 +137,13 @@ impl XRSystemMethods<crate::DomTypeHolder> for XRSystem {
return;
};
if let Ok(()) = message {
let _ =
task_source.queue_with_canceller(trusted.resolve_task(true), &canceller);
let _ = task_source.queue(trusted.resolve_task(true));
} else {
let _ =
task_source.queue_with_canceller(trusted.resolve_task(false), &canceller);
let _ = task_source.queue(trusted.resolve_task(false));
};
}),
);
if let Some(mut r) = window.webxr_registry() {
if let Some(mut r) = global.as_window().webxr_registry() {
r.supports_session(mode.convert(), sender);
}
@ -239,9 +234,7 @@ impl XRSystemMethods<crate::DomTypeHolder> for XRSystem {
let mut trusted = Some(TrustedPromise::new(promise.clone()));
let this = Trusted::new(self);
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
let (frame_sender, frame_receiver) = ipc_crate::channel().unwrap();
let mut frame_receiver = Some(frame_receiver);
@ -258,12 +251,9 @@ impl XRSystemMethods<crate::DomTypeHolder> for XRSystem {
error!("requestSession callback given incorrect payload");
return;
};
let _ = task_source.queue_with_canceller(
task!(request_session: move || {
this.root().session_obtained(message, trusted.root(), mode, frame_receiver);
}),
&canceller,
);
let _ = task_source.queue(task!(request_session: move || {
this.root().session_obtained(message, trusted.root(), mode, frame_receiver);
}));
}),
);
if let Some(mut r) = window.webxr_registry() {
@ -314,9 +304,7 @@ impl XRSystem {
// https://github.com/immersive-web/navigation/issues/10
pub fn dispatch_sessionavailable(&self) {
let xr = Trusted::new(self);
let global = self.global();
let window = global.as_window();
window
self.global()
.task_manager()
.dom_manipulation_task_source()
.queue(
@ -327,8 +315,7 @@ impl XRSystem {
ScriptThread::set_user_interacting(true);
xr.upcast::<EventTarget>().fire_bubbling_event(atom!("sessionavailable"), CanGc::note());
ScriptThread::set_user_interacting(interacting);
}),
window.upcast(),
})
)
.unwrap();
}

View file

@ -147,13 +147,10 @@ impl XRTestMethods<crate::DomTypeHolder> for XRTest {
};
let global = self.global();
let window = global.as_window();
let this = Trusted::new(self);
let mut trusted = Some(TrustedPromise::new(p.clone()));
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
ROUTER.add_typed_route(
@ -166,15 +163,12 @@ impl XRTestMethods<crate::DomTypeHolder> for XRTest {
let message =
message.expect("SimulateDeviceConnection callback given incorrect payload");
let _ = task_source.queue_with_canceller(
task!(request_session: move || {
this.root().device_obtained(message, trusted);
}),
&canceller,
);
let _ = task_source.queue(task!(request_session: move || {
this.root().device_obtained(message, trusted);
}));
}),
);
if let Some(mut r) = window.webxr_registry() {
if let Some(mut r) = global.as_window().webxr_registry() {
r.simulate_device_connection(init, sender);
}
@ -206,10 +200,7 @@ impl XRTestMethods<crate::DomTypeHolder> for XRTest {
devices.clear();
let mut trusted = Some(TrustedPromise::new(p.clone()));
let (task_source, canceller) = global
.as_window()
.task_manager()
.dom_manipulation_task_source_with_canceller();
let task_source = global.task_manager().dom_manipulation_task_source();
ROUTER.add_typed_route(
receiver.to_ipc_receiver(),
@ -219,8 +210,7 @@ impl XRTestMethods<crate::DomTypeHolder> for XRTest {
let trusted = trusted
.take()
.expect("DisconnectAllDevices disconnected more devices than expected");
let _ =
task_source.queue_with_canceller(trusted.resolve_task(()), &canceller);
let _ = task_source.queue(trusted.resolve_task(()));
}
}),
);