2021-10-31 10:09:40 +01:00
|
|
|
use crate::Monotonic;
|
2020-12-12 23:24:54 +01:00
|
|
|
use core::cmp::Ordering;
|
2021-08-16 15:37:39 +02:00
|
|
|
use heapless::sorted_linked_list::{LinkedIndexU16, Min, SortedLinkedList};
|
2020-12-12 23:24:54 +01:00
|
|
|
|
2021-08-16 15:37:39 +02:00
|
|
|
pub struct TimerQueue<Mono, Task, const N: usize>(
|
|
|
|
pub SortedLinkedList<NotReady<Mono, Task>, LinkedIndexU16, Min, N>,
|
|
|
|
)
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Mono: Monotonic,
|
|
|
|
Task: Copy;
|
2020-12-12 23:24:54 +01:00
|
|
|
|
2021-04-03 19:30:34 +02:00
|
|
|
impl<Mono, Task, const N: usize> TimerQueue<Mono, Task, N>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Mono: Monotonic,
|
|
|
|
Task: Copy,
|
2020-12-12 23:24:54 +01:00
|
|
|
{
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Writing to memory with a transmute in order to enable
|
|
|
|
/// interrupts of the SysTick timer
|
|
|
|
///
|
|
|
|
/// Enqueue a task without checking if it is full
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn enqueue_unchecked<F1, F2>(
|
|
|
|
&mut self,
|
2020-12-13 17:48:11 +01:00
|
|
|
nr: NotReady<Mono, Task>,
|
2020-12-12 23:24:54 +01:00
|
|
|
enable_interrupt: F1,
|
|
|
|
pend_handler: F2,
|
2021-04-08 09:12:08 +02:00
|
|
|
mono: Option<&mut Mono>,
|
2020-12-12 23:24:54 +01:00
|
|
|
) where
|
|
|
|
F1: FnOnce(),
|
|
|
|
F2: FnOnce(),
|
|
|
|
{
|
|
|
|
// Check if the top contains a non-empty element and if that element is
|
|
|
|
// greater than nr
|
|
|
|
let if_heap_max_greater_than_nr = self
|
|
|
|
.0
|
|
|
|
.peek()
|
2021-03-14 21:27:21 +01:00
|
|
|
.map(|head| nr.instant < head.instant)
|
2020-12-12 23:24:54 +01:00
|
|
|
.unwrap_or(true);
|
2021-03-14 21:27:21 +01:00
|
|
|
|
2020-12-12 23:24:54 +01:00
|
|
|
if if_heap_max_greater_than_nr {
|
2021-03-14 21:27:21 +01:00
|
|
|
if Mono::DISABLE_INTERRUPT_ON_EMPTY_QUEUE && self.0.is_empty() {
|
2021-04-08 09:12:08 +02:00
|
|
|
if let Some(mono) = mono {
|
|
|
|
mono.enable_timer();
|
|
|
|
}
|
2020-12-12 23:24:54 +01:00
|
|
|
enable_interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
pend_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.0.push_unchecked(nr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the timer queue is empty.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
|
2021-03-14 21:27:21 +01:00
|
|
|
/// Cancel the marker value
|
|
|
|
pub fn cancel_marker(&mut self, marker: u32) -> Option<(Task, u8)> {
|
|
|
|
if let Some(val) = self.0.find_mut(|nr| nr.marker == marker) {
|
|
|
|
let nr = val.pop();
|
|
|
|
|
|
|
|
Some((nr.task, nr.index))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the instant at an marker value to a new instant
|
|
|
|
pub fn update_marker<F: FnOnce()>(
|
|
|
|
&mut self,
|
|
|
|
marker: u32,
|
|
|
|
new_marker: u32,
|
2021-10-31 10:09:40 +01:00
|
|
|
instant: Mono::Instant,
|
2021-03-14 21:27:21 +01:00
|
|
|
pend_handler: F,
|
|
|
|
) -> Result<(), ()> {
|
|
|
|
if let Some(mut val) = self.0.find_mut(|nr| nr.marker == marker) {
|
|
|
|
val.instant = instant;
|
|
|
|
val.marker = new_marker;
|
|
|
|
|
|
|
|
// On update pend the handler to reconfigure the next compare match
|
|
|
|
pend_handler();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:24:54 +01:00
|
|
|
/// Dequeue a task from the TimerQueue
|
2021-02-18 19:30:59 +01:00
|
|
|
pub fn dequeue<F>(&mut self, disable_interrupt: F, mono: &mut Mono) -> Option<(Task, u8)>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
|
|
|
F: FnOnce(),
|
|
|
|
{
|
2021-02-18 19:30:59 +01:00
|
|
|
mono.clear_compare_flag();
|
|
|
|
|
|
|
|
if let Some(instant) = self.0.peek().map(|p| p.instant) {
|
2021-10-31 10:09:40 +01:00
|
|
|
if instant <= mono.now() {
|
2021-02-18 19:30:59 +01:00
|
|
|
// task became ready
|
|
|
|
let nr = unsafe { self.0.pop_unchecked() };
|
2020-12-12 23:24:54 +01:00
|
|
|
|
2021-02-18 19:30:59 +01:00
|
|
|
Some((nr.task, nr.index))
|
|
|
|
} else {
|
2021-02-22 20:59:23 +01:00
|
|
|
// Set compare
|
2021-10-31 10:09:40 +01:00
|
|
|
mono.set_compare(instant);
|
2021-02-18 19:30:59 +01:00
|
|
|
|
|
|
|
// Double check that the instant we set is really in the future, else
|
|
|
|
// dequeue. If the monotonic is fast enough it can happen that from the
|
|
|
|
// read of now to the set of the compare, the time can overflow. This is to
|
|
|
|
// guard against this.
|
2021-10-31 10:09:40 +01:00
|
|
|
if instant <= mono.now() {
|
2021-02-18 19:30:59 +01:00
|
|
|
let nr = unsafe { self.0.pop_unchecked() };
|
2020-12-13 17:48:11 +01:00
|
|
|
|
|
|
|
Some((nr.task, nr.index))
|
|
|
|
} else {
|
2021-02-18 19:30:59 +01:00
|
|
|
None
|
2020-12-12 23:24:54 +01:00
|
|
|
}
|
2021-02-18 19:30:59 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The queue is empty, disable the interrupt.
|
|
|
|
if Mono::DISABLE_INTERRUPT_ON_EMPTY_QUEUE {
|
2020-12-12 23:24:54 +01:00
|
|
|
disable_interrupt();
|
2021-02-22 20:59:03 +01:00
|
|
|
mono.disable_timer();
|
2020-12-12 23:24:54 +01:00
|
|
|
}
|
2021-02-18 19:30:59 +01:00
|
|
|
|
|
|
|
None
|
2020-12-12 23:24:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
pub struct NotReady<Mono, Task>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Task: Copy,
|
|
|
|
Mono: Monotonic,
|
2020-12-12 23:24:54 +01:00
|
|
|
{
|
|
|
|
pub index: u8,
|
2021-10-31 10:09:40 +01:00
|
|
|
pub instant: Mono::Instant,
|
2020-12-13 17:48:11 +01:00
|
|
|
pub task: Task,
|
2021-03-11 19:12:02 +01:00
|
|
|
pub marker: u32,
|
2020-12-12 23:24:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
impl<Mono, Task> Eq for NotReady<Mono, Task>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Task: Copy,
|
|
|
|
Mono: Monotonic,
|
2020-12-12 23:24:54 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
impl<Mono, Task> Ord for NotReady<Mono, Task>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Task: Copy,
|
|
|
|
Mono: Monotonic,
|
2020-12-12 23:24:54 +01:00
|
|
|
{
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
self.instant.cmp(&other.instant)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
impl<Mono, Task> PartialEq for NotReady<Mono, Task>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Task: Copy,
|
|
|
|
Mono: Monotonic,
|
2020-12-12 23:24:54 +01:00
|
|
|
{
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.instant == other.instant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
impl<Mono, Task> PartialOrd for NotReady<Mono, Task>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Task: Copy,
|
|
|
|
Mono: Monotonic,
|
2020-12-12 23:24:54 +01:00
|
|
|
{
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
2021-12-25 13:17:16 +01:00
|
|
|
Some(self.cmp(other))
|
2020-12-12 23:24:54 +01:00
|
|
|
}
|
|
|
|
}
|