2021-02-18 19:30:59 +01:00
|
|
|
use crate::{
|
|
|
|
time::{Clock, Instant},
|
|
|
|
Monotonic,
|
|
|
|
};
|
2020-12-12 23:24:54 +01:00
|
|
|
use core::cmp::Ordering;
|
|
|
|
use heapless::{binary_heap::Min, ArrayLength, BinaryHeap};
|
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
pub struct TimerQueue<Mono, Task, N>(pub BinaryHeap<NotReady<Mono, Task>, N, Min>)
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Mono: Monotonic,
|
|
|
|
N: ArrayLength<NotReady<Mono, Task>>,
|
|
|
|
Task: Copy;
|
2020-12-12 23:24:54 +01:00
|
|
|
|
2020-12-13 17:48:11 +01:00
|
|
|
impl<Mono, Task, N> TimerQueue<Mono, Task, N>
|
2020-12-12 23:24:54 +01:00
|
|
|
where
|
2020-12-13 17:48:11 +01:00
|
|
|
Mono: Monotonic,
|
|
|
|
N: ArrayLength<NotReady<Mono, Task>>,
|
|
|
|
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-02-22 20:59:03 +01:00
|
|
|
mono: &mut Mono,
|
2020-12-12 23:24:54 +01:00
|
|
|
) where
|
|
|
|
F1: FnOnce(),
|
|
|
|
F2: FnOnce(),
|
|
|
|
{
|
|
|
|
let mut is_empty = true;
|
|
|
|
// 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()
|
|
|
|
.map(|head| {
|
|
|
|
is_empty = false;
|
|
|
|
nr.instant < head.instant
|
|
|
|
})
|
|
|
|
.unwrap_or(true);
|
|
|
|
if if_heap_max_greater_than_nr {
|
2021-02-18 19:30:59 +01:00
|
|
|
if Mono::DISABLE_INTERRUPT_ON_EMPTY_QUEUE && is_empty {
|
2021-02-22 20:59:03 +01:00
|
|
|
// mem::transmute::<_, SYST>(()).enable_interrupt();A
|
|
|
|
mono.enable_timer();
|
2020-12-12 23:24:54 +01:00
|
|
|
enable_interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set SysTick pending
|
|
|
|
// SCB::set_pendst();
|
|
|
|
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-02-18 19:30:59 +01:00
|
|
|
#[inline]
|
|
|
|
fn unwrapper<T, E>(val: Result<T, E>) -> T {
|
|
|
|
if let Ok(v) = val {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
unreachable!("Your monotonic is not infallible")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:24:54 +01:00
|
|
|
/// Dequeue a task from the TimerQueue
|
|
|
|
#[inline]
|
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-02-22 20:15:13 +01:00
|
|
|
if instant <= Self::unwrapper(Clock::try_now(mono)) {
|
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
|
|
|
|
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-02-22 20:15:13 +01:00
|
|
|
if instant <= Self::unwrapper(Clock::try_now(mono)) {
|
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,
|
2020-12-13 17:48:11 +01:00
|
|
|
pub instant: Instant<Mono>,
|
|
|
|
pub task: Task,
|
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> {
|
|
|
|
Some(self.cmp(&other))
|
|
|
|
}
|
|
|
|
}
|