|
| 1 | +use core::sync::atomic::{AtomicU64, Ordering}; |
| 2 | + |
| 3 | +#[cfg(feature = "net")] |
| 4 | +use crate::executor::network::NETWORK_WAKER; |
| 5 | +use crate::set_oneshot_timer; |
| 6 | + |
| 7 | +/// A possible timer interrupt source (i.e. reason the timer interrupt was set |
| 8 | +/// up). |
| 9 | +#[derive(PartialEq, Eq)] |
| 10 | +pub enum Source { |
| 11 | + Network, |
| 12 | + Scheduler, |
| 13 | +} |
| 14 | + |
| 15 | +/// A slot in the timer list. Each source is represented once. This is so that |
| 16 | +/// we can have multiple timers at the same time with only one hardware timer. |
| 17 | +struct Slot { |
| 18 | + /// Timer source. |
| 19 | + source: Source, |
| 20 | + /// Point in time at which to wake up (in microsecond precision). |
| 21 | + /// A value of [`u64::MAX`] means the timer is not set. |
| 22 | + wakeup_time: AtomicU64, |
| 23 | +} |
| 24 | + |
| 25 | +/// The actual timer list with one entry for each source. |
| 26 | +static TIMERS: [Slot; 2] = [ |
| 27 | + Slot { |
| 28 | + source: Source::Network, |
| 29 | + wakeup_time: AtomicU64::new(u64::MAX), |
| 30 | + }, |
| 31 | + Slot { |
| 32 | + source: Source::Scheduler, |
| 33 | + wakeup_time: AtomicU64::new(u64::MAX), |
| 34 | + }, |
| 35 | +]; |
| 36 | + |
| 37 | +/// Create a new timer, overriding any previous timer for the source. |
| 38 | +#[cfg(feature = "net")] |
| 39 | +pub fn create_timer(source: Source, wakeup_micros: u64) { |
| 40 | + trace!("Setting relative timer interrupt for {wakeup_micros}us"); |
| 41 | + |
| 42 | + create_timer_abs( |
| 43 | + source, |
| 44 | + crate::arch::processor::get_timer_ticks() + wakeup_micros, |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +/// Crete a new timer, but with an absolute wakeup time. |
| 49 | +pub fn create_timer_abs(source: Source, wakeup_time: u64) { |
| 50 | + trace!( |
| 51 | + "Setting an absolute timer interrupt for {}us", |
| 52 | + wakeup_time - crate::arch::processor::get_timer_ticks() |
| 53 | + ); |
| 54 | + |
| 55 | + { |
| 56 | + // SAFETY: Our timer list has an entry for every possible source |
| 57 | + let previous_entry = TIMERS.iter().find(|slot| slot.source == source).unwrap(); |
| 58 | + |
| 59 | + // Overwrite the wakeup time |
| 60 | + previous_entry |
| 61 | + .wakeup_time |
| 62 | + .store(wakeup_time, Ordering::Relaxed); |
| 63 | + } |
| 64 | + |
| 65 | + // If this timer is the one closest in the future, set the real timer to it |
| 66 | + // SAFETY: There's more than 1 slot |
| 67 | + if TIMERS |
| 68 | + .iter() |
| 69 | + .map(|slot| slot.wakeup_time.load(Ordering::Relaxed)) |
| 70 | + .min_by(|a, b| a.cmp(b)) |
| 71 | + .unwrap() |
| 72 | + == wakeup_time |
| 73 | + { |
| 74 | + trace!("Setting the oneshot timer now"); |
| 75 | + |
| 76 | + set_oneshot_timer(Some(wakeup_time)); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Sets the next timer or disables it if no timer is pending. |
| 81 | +pub fn set_next_timer() { |
| 82 | + // SAFETY: There's more than 1 slot |
| 83 | + let lowest_timer = TIMERS |
| 84 | + .iter() |
| 85 | + .map(|slot| slot.wakeup_time.load(Ordering::Relaxed)) |
| 86 | + .min_by(|a, b| a.cmp(b)) |
| 87 | + .unwrap(); |
| 88 | + |
| 89 | + if lowest_timer == u64::MAX { |
| 90 | + set_oneshot_timer(None); |
| 91 | + } else { |
| 92 | + set_oneshot_timer(Some(lowest_timer)); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Clears the timer slot for the currently active timer. |
| 97 | +pub fn clear_active() { |
| 98 | + // SAFETY: There's more than 1 slot |
| 99 | + let lowest_timer = TIMERS |
| 100 | + .iter() |
| 101 | + .min_by(|a, b| { |
| 102 | + a.wakeup_time |
| 103 | + .load(Ordering::Relaxed) |
| 104 | + .cmp(&b.wakeup_time.load(Ordering::Relaxed)) |
| 105 | + }) |
| 106 | + .unwrap(); |
| 107 | + |
| 108 | + // TODO: Do we really want to do this here? |
| 109 | + match lowest_timer.source { |
| 110 | + #[cfg(feature = "net")] |
| 111 | + Source::Network => NETWORK_WAKER.lock().wake(), |
| 112 | + _ => {} // no-op, we always poll after a timer interrupt |
| 113 | + } |
| 114 | + |
| 115 | + trace!("Cleared active timer"); |
| 116 | + |
| 117 | + lowest_timer.wakeup_time.store(u64::MAX, Ordering::Relaxed); |
| 118 | +} |
0 commit comments