|
| 1 | +use core::ops::{Deref, DerefMut}; |
| 2 | + |
| 3 | +use crate::core_local::core_scheduler; |
| 4 | +#[cfg(feature = "net")] |
| 5 | +use crate::executor::network::NETWORK_WAKER; |
| 6 | +use crate::set_oneshot_timer; |
| 7 | + |
| 8 | +/// A possible timer interrupt source (i.e. reason the timer interrupt was set |
| 9 | +/// up). |
| 10 | +#[derive(Debug, PartialEq, Eq)] |
| 11 | +pub enum Source { |
| 12 | + Network, |
| 13 | + Scheduler, |
| 14 | +} |
| 15 | + |
| 16 | +/// A slot in the timer list. Each source is represented once. This is so that |
| 17 | +/// we can have multiple timers at the same time with only one hardware timer. |
| 18 | +#[derive(Debug)] |
| 19 | +pub struct Slot { |
| 20 | + /// Timer source. |
| 21 | + source: Source, |
| 22 | + /// Point in time at which to wake up (in microsecond precision). |
| 23 | + /// A value of [`u64::MAX`] means the timer is not set. |
| 24 | + wakeup_time: u64, |
| 25 | +} |
| 26 | + |
| 27 | +// List of timers with one entry for every possible source. |
| 28 | +#[derive(Debug)] |
| 29 | +pub struct TimerList([Slot; 2]); |
| 30 | + |
| 31 | +impl TimerList { |
| 32 | + pub fn new() -> Self { |
| 33 | + Self([ |
| 34 | + Slot { |
| 35 | + source: Source::Network, |
| 36 | + wakeup_time: u64::MAX, |
| 37 | + }, |
| 38 | + Slot { |
| 39 | + source: Source::Scheduler, |
| 40 | + wakeup_time: u64::MAX, |
| 41 | + }, |
| 42 | + ]) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Deref for TimerList { |
| 47 | + type Target = [Slot; 2]; |
| 48 | + |
| 49 | + fn deref(&self) -> &Self::Target { |
| 50 | + &self.0 |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl DerefMut for TimerList { |
| 55 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 56 | + &mut self.0 |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Create a new timer, overriding any previous timer for the source. |
| 61 | +#[cfg(feature = "net")] |
| 62 | +#[inline] |
| 63 | +pub fn create_timer(source: Source, wakeup_micros: u64) { |
| 64 | + create_timer_abs( |
| 65 | + source, |
| 66 | + crate::arch::processor::get_timer_ticks() + wakeup_micros, |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +/// Crete a new timer, but with an absolute wakeup time. |
| 71 | +pub fn create_timer_abs(source: Source, wakeup_time: u64) { |
| 72 | + let timers = &mut core_scheduler().timers; |
| 73 | + |
| 74 | + // SAFETY: Our timer list has an entry for every possible source |
| 75 | + let previous_entry = timers |
| 76 | + .iter_mut() |
| 77 | + .find(|slot| slot.source == source) |
| 78 | + .unwrap(); |
| 79 | + |
| 80 | + // Overwrite the wakeup time |
| 81 | + previous_entry.wakeup_time = previous_entry.wakeup_time.min(wakeup_time); |
| 82 | + |
| 83 | + // If this timer is the one closest in the future, set the real timer to it |
| 84 | + // SAFETY: There's more than 1 slot |
| 85 | + if timers.iter().map(|slot| slot.wakeup_time).min().unwrap() == wakeup_time { |
| 86 | + set_oneshot_timer(Some(wakeup_time)); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Clears the timer slot for the currently active timer and sets the next timer or disables it if no timer is pending. |
| 91 | +pub fn clear_active_and_set_next() { |
| 92 | + let timers = &mut core_scheduler().timers; |
| 93 | + |
| 94 | + // SAFETY: There's more than 1 slot |
| 95 | + let lowest_timer = timers |
| 96 | + .iter_mut() |
| 97 | + .min_by(|a, b| a.wakeup_time.cmp(&b.wakeup_time)) |
| 98 | + .unwrap(); |
| 99 | + |
| 100 | + assert!(lowest_timer.wakeup_time != u64::MAX); |
| 101 | + |
| 102 | + // TODO: Do we really want to do this here? |
| 103 | + match lowest_timer.source { |
| 104 | + #[cfg(feature = "net")] |
| 105 | + Source::Network => NETWORK_WAKER.lock().wake(), |
| 106 | + _ => {} // no-op, we always poll after a timer interrupt |
| 107 | + } |
| 108 | + |
| 109 | + trace!("Cleared active timer {lowest_timer:?}"); |
| 110 | + |
| 111 | + lowest_timer.wakeup_time = u64::MAX; |
| 112 | + |
| 113 | + // We may receive a timer interrupt earlier than expected |
| 114 | + // This appears to only be the case in QEMU, it seems like timer ticks |
| 115 | + // do not advance linearly there? |
| 116 | + // Either way, this means that QEMU *thinks* the time has passed, so it |
| 117 | + // probably has and knows better than we do. |
| 118 | + // We can cheat a bit and adjust all timers slightly based on this |
| 119 | + if lowest_timer.wakeup_time > crate::arch::processor::get_timer_ticks() { |
| 120 | + let offset = lowest_timer.wakeup_time - crate::arch::processor::get_timer_ticks(); |
| 121 | + |
| 122 | + for timer in timers.iter_mut() { |
| 123 | + if timer.wakeup_time != u64::MAX { |
| 124 | + timer.wakeup_time -= offset; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // SAFETY: There's more than 1 slot |
| 130 | + let new_lowest_timer = timers.iter().map(|slot| slot.wakeup_time).min().unwrap(); |
| 131 | + |
| 132 | + if new_lowest_timer == u64::MAX { |
| 133 | + set_oneshot_timer(None); |
| 134 | + } else { |
| 135 | + set_oneshot_timer(Some(new_lowest_timer)); |
| 136 | + } |
| 137 | +} |
0 commit comments