script: Make timers per-process (#34581)

Before all timers were managed by the Constellation process, meaning
that they had to trigger IPC calls to be scheduled and fired. Currently,
timers are only used in the `ScriptThread`, so it makes sense that they
are per-process.

This change restores the timer thread functionality that existed before
avoided entirely. Completion is done using a callback that is sent to
the timer thread similarly to how fetch is done. This allows reusing the
existing task queue without making any new channels.

Fixes #15219.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2024-12-24 10:53:35 +01:00 committed by GitHub
parent ff7626bfc6
commit 226299380d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 366 additions and 324 deletions

View file

@ -10,21 +10,21 @@ use std::rc::Rc;
use std::time::{Duration, Instant};
use deny_public_fields::DenyPublicFields;
use ipc_channel::ipc::IpcSender;
use js::jsapi::Heap;
use js::jsval::{JSVal, UndefinedValue};
use js::rust::HandleValue;
use script_traits::{TimerEvent, TimerEventId, TimerEventRequest, TimerSchedulerMsg, TimerSource};
use servo_config::pref;
use timers::{TimerEventId, TimerEventRequest, TimerSource};
use crate::dom::bindings::callback::ExceptionHandling::Report;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::document::FakeRequestAnimationFrameCallback;
use crate::dom::eventsource::EventSourceTimeoutCallback;
use crate::dom::globalscope::GlobalScope;
use crate::dom::globalscope::{GlobalScope, TimerListener};
use crate::dom::htmlmetaelement::RefreshRedirectDue;
use crate::dom::testbinding::TestBindingCallback;
use crate::dom::xmlhttprequest::XHRTimeoutCallback;
@ -37,17 +37,11 @@ pub struct OneshotTimerHandle(i32);
#[derive(DenyPublicFields, JSTraceable, MallocSizeOf)]
pub struct OneshotTimers {
global_scope: DomRoot<GlobalScope>,
#[ignore_malloc_size_of = "Missing malloc_size_of for task types"]
#[no_trace]
timer_listener: TimerListener,
js_timers: JsTimers,
#[ignore_malloc_size_of = "Defined in std"]
#[no_trace]
/// The sender, to be cloned for each timer,
/// on which the timer scheduler in the constellation can send an event
/// when the timer is due.
timer_event_chan: DomRefCell<Option<IpcSender<TimerEvent>>>,
#[ignore_malloc_size_of = "Defined in std"]
#[no_trace]
/// The sender to the timer scheduler in the constellation.
scheduler_chan: IpcSender<TimerSchedulerMsg>,
next_timer_handle: Cell<OneshotTimerHandle>,
timers: DomRefCell<Vec<OneshotTimer>>,
suspended_since: Cell<Option<Instant>>,
@ -124,11 +118,11 @@ impl PartialEq for OneshotTimer {
}
impl OneshotTimers {
pub fn new(scheduler_chan: IpcSender<TimerSchedulerMsg>) -> OneshotTimers {
pub fn new(global_scope: &GlobalScope, timer_listener: TimerListener) -> OneshotTimers {
OneshotTimers {
global_scope: DomRoot::from_ref(global_scope),
timer_listener,
js_timers: JsTimers::default(),
timer_event_chan: DomRefCell::new(None),
scheduler_chan,
next_timer_handle: Cell::new(OneshotTimerHandle(1)),
timers: DomRefCell::new(Vec::new()),
suspended_since: Cell::new(None),
@ -137,12 +131,6 @@ impl OneshotTimers {
}
}
pub fn setup_scheduling(&self, timer_event_chan: IpcSender<TimerEvent>) {
let mut chan = self.timer_event_chan.borrow_mut();
assert!(chan.is_none());
*chan = Some(timer_event_chan);
}
pub fn schedule_callback(
&self,
callback: OneshotTimerCallback,
@ -291,24 +279,19 @@ impl OneshotTimers {
}
let timers = self.timers.borrow();
let Some(timer) = timers.last() else {
return;
};
if let Some(timer) = timers.last() {
let expected_event_id = self.invalidate_expected_event_id();
let expected_event_id = self.invalidate_expected_event_id();
let event_request = TimerEventRequest {
callback: self.timer_listener.clone().into_callback(),
source: timer.source,
id: expected_event_id,
duration: timer.scheduled_for - Instant::now(),
};
let delay = timer.scheduled_for - Instant::now();
let request = TimerEventRequest(
self.timer_event_chan
.borrow()
.clone()
.expect("Timer event chan not setup to schedule timers."),
timer.source,
expected_event_id,
delay,
);
self.scheduler_chan
.send(TimerSchedulerMsg(request))
.unwrap();
}
self.global_scope.schedule_timer(event_request);
}
fn invalidate_expected_event_id(&self) -> TimerEventId {