aboutsummaryrefslogtreecommitdiffstats
path: root/components/constellation/timer_scheduler.rs
diff options
context:
space:
mode:
authorMartin Robinson <mrobinson@igalia.com>2024-12-24 10:53:35 +0100
committerGitHub <noreply@github.com>2024-12-24 09:53:35 +0000
commit226299380d8506b22692f2ab4302564cd33c42ff (patch)
tree6ddf2131e9def28f724afa0a92b4546a69de1e77 /components/constellation/timer_scheduler.rs
parentff7626bfc61aef3a294c0e286c124fb3f5bed495 (diff)
downloadservo-226299380d8506b22692f2ab4302564cd33c42ff.tar.gz
servo-226299380d8506b22692f2ab4302564cd33c42ff.zip
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>
Diffstat (limited to 'components/constellation/timer_scheduler.rs')
-rw-r--r--components/constellation/timer_scheduler.rs73
1 files changed, 0 insertions, 73 deletions
diff --git a/components/constellation/timer_scheduler.rs b/components/constellation/timer_scheduler.rs
deleted file mode 100644
index 6481696fc73..00000000000
--- a/components/constellation/timer_scheduler.rs
+++ /dev/null
@@ -1,73 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * 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 std::cmp::{self, Ord};
-use std::collections::BinaryHeap;
-use std::time::{Duration, Instant};
-
-use script_traits::{TimerEvent, TimerEventRequest, TimerSchedulerMsg};
-
-pub struct TimerScheduler(BinaryHeap<ScheduledEvent>);
-
-struct ScheduledEvent {
- request: TimerEventRequest,
- for_time: Instant,
-}
-
-impl Ord for ScheduledEvent {
- fn cmp(&self, other: &ScheduledEvent) -> cmp::Ordering {
- self.for_time.cmp(&other.for_time).reverse()
- }
-}
-
-impl PartialOrd for ScheduledEvent {
- fn partial_cmp(&self, other: &ScheduledEvent) -> Option<cmp::Ordering> {
- Some(self.cmp(other))
- }
-}
-
-impl Eq for ScheduledEvent {}
-impl PartialEq for ScheduledEvent {
- fn eq(&self, other: &ScheduledEvent) -> bool {
- std::ptr::eq(self, other)
- }
-}
-
-impl TimerScheduler {
- pub fn new() -> Self {
- TimerScheduler(BinaryHeap::<ScheduledEvent>::new())
- }
-
- /// Dispatch any events whose due time is past,
- /// and return a timeout corresponding to the earliest scheduled event, if any.
- pub fn check_timers(&mut self) -> Option<Duration> {
- let now = Instant::now();
- loop {
- match self.0.peek() {
- // Dispatch the event if its due time is past
- Some(event) if event.for_time <= now => {
- let TimerEventRequest(ref sender, source, id, _) = event.request;
- let _ = sender.send(TimerEvent(source, id));
- },
- // Do not schedule a timeout.
- None => return None,
- // Schedule a timeout for the earliest event.
- Some(event) => return Some(event.for_time - now),
- }
- // Remove the event from the priority queue
- // (Note this only executes when the first event has been dispatched).
- self.0.pop();
- }
- }
-
- /// Handle an incoming timer request.
- pub fn handle_timer_request(&mut self, request: TimerSchedulerMsg) {
- let TimerEventRequest(_, _, _, delay) = request.0;
- let event = ScheduledEvent {
- request: request.0,
- for_time: Instant::now() + delay,
- };
- self.0.push(event);
- }
-}