Monotonic trait is safe; add MultiCore trait

This commit is contained in:
Jorge Aparicio 2019-06-24 14:09:12 +02:00
parent 4e51bb68b9
commit 596cf585ea
7 changed files with 35 additions and 9 deletions

View file

@ -8,7 +8,7 @@ use core::{
}; };
use bare_metal::Nr; use bare_metal::Nr;
use rtfm::Monotonic; use rtfm::{Monotonic, MultiCore};
// both cores have the exact same interrupts // both cores have the exact same interrupts
pub use Interrupt_0 as Interrupt_1; pub use Interrupt_0 as Interrupt_1;
@ -21,7 +21,7 @@ pub fn xpend(_core: u8, _interrupt: impl Nr) {}
/// Fake monotonic timer /// Fake monotonic timer
pub struct MT; pub struct MT;
unsafe impl Monotonic for MT { impl Monotonic for MT {
type Instant = Instant; type Instant = Instant;
fn ratio() -> u32 { fn ratio() -> u32 {
@ -41,6 +41,8 @@ unsafe impl Monotonic for MT {
} }
} }
impl MultiCore for MT {}
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Clone, Copy, Eq, PartialEq)]
pub struct Instant(i32); pub struct Instant(i32);

View file

@ -8,7 +8,7 @@ use core::{
}; };
use bare_metal::Nr; use bare_metal::Nr;
use rtfm::Monotonic; use rtfm::{Monotonic, MultiCore};
// both cores have the exact same interrupts // both cores have the exact same interrupts
pub use Interrupt_0 as Interrupt_1; pub use Interrupt_0 as Interrupt_1;
@ -21,7 +21,7 @@ pub fn xpend(_core: u8, _interrupt: impl Nr) {}
/// Fake monotonic timer /// Fake monotonic timer
pub struct MT; pub struct MT;
unsafe impl Monotonic for MT { impl Monotonic for MT {
type Instant = Instant; type Instant = Instant;
fn ratio() -> u32 { fn ratio() -> u32 {
@ -41,6 +41,8 @@ unsafe impl Monotonic for MT {
} }
} }
impl MultiCore for MT {}
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Clone, Copy, Eq, PartialEq)]
pub struct Instant(i32); pub struct Instant(i32);

View file

@ -32,7 +32,7 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
// generate a `main` function for each core // generate a `main` function for each core
for core in 0..app.args.cores { for core in 0..app.args.cores {
let assertion_stmts = assertions::codegen(core, analysis); let assertion_stmts = assertions::codegen(core, analysis, extra);
let (const_app_pre_init, pre_init_stmts) = pre_init::codegen(core, &app, analysis, extra); let (const_app_pre_init, pre_init_stmts) = pre_init::codegen(core, &app, analysis, extra);

View file

@ -1,10 +1,10 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::quote;
use crate::analyze::Analysis; use crate::{analyze::Analysis, check::Extra};
/// Generates compile-time assertions that check that types implement the `Send` / `Sync` traits /// Generates compile-time assertions that check that types implement the `Send` / `Sync` traits
pub fn codegen(core: u8, analysis: &Analysis) -> Vec<TokenStream2> { pub fn codegen(core: u8, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
let mut stmts = vec![]; let mut stmts = vec![];
// we don't generate *all* assertions on all cores because the user could conditionally import a // we don't generate *all* assertions on all cores because the user could conditionally import a
@ -22,5 +22,12 @@ pub fn codegen(core: u8, analysis: &Analysis) -> Vec<TokenStream2> {
} }
} }
// if the `schedule` API is used in more than one core then we need to check that the
// `monotonic` timer can be used in multi-core context
if analysis.timer_queues.len() > 1 && analysis.timer_queues.contains_key(&core) {
let monotonic = extra.monotonic();
stmts.push(quote!(rtfm::export::assert_multicore::<#monotonic>();));
}
stmts stmts
} }

View file

@ -116,6 +116,11 @@ pub struct Duration {
} }
impl Duration { impl Duration {
/// Creates a new `Duration` from the specified number of clock cycles
pub fn from_cycles(cycles: u32) -> Self {
Duration { inner: cycles }
}
/// Returns the total number of clock cycles contained by this `Duration` /// Returns the total number of clock cycles contained by this `Duration`
pub fn as_cycles(&self) -> u32 { pub fn as_cycles(&self) -> u32 {
self.inner self.inner
@ -181,7 +186,7 @@ impl U32Ext for u32 {
pub struct CYCCNT; pub struct CYCCNT;
#[cfg(not(feature = "heterogeneous"))] #[cfg(not(feature = "heterogeneous"))]
unsafe impl crate::Monotonic for CYCCNT { impl crate::Monotonic for CYCCNT {
type Instant = Instant; type Instant = Instant;
fn ratio() -> u32 { fn ratio() -> u32 {

View file

@ -108,6 +108,13 @@ where
{ {
} }
#[inline(always)]
pub fn assert_multicore<T>()
where
T: super::MultiCore,
{
}
#[cfg(armv7m)] #[cfg(armv7m)]
#[inline(always)] #[inline(always)]
pub unsafe fn lock<T, R>( pub unsafe fn lock<T, R>(

View file

@ -117,7 +117,7 @@ impl From<cortex_m::Peripherals> for Peripherals {
} }
/// A monotonic clock / counter /// A monotonic clock / counter
pub unsafe trait Monotonic { pub trait Monotonic {
/// A measurement of this clock /// A measurement of this clock
type Instant: Copy + Ord + Sub; type Instant: Copy + Ord + Sub;
@ -134,6 +134,9 @@ pub unsafe trait Monotonic {
fn zero() -> Self::Instant; fn zero() -> Self::Instant;
} }
/// A marker trait that indicates that it is correct to use this type in multi-core context
pub trait MultiCore {}
/// Sets the given `interrupt` as pending /// Sets the given `interrupt` as pending
/// ///
/// This is a convenience function around /// This is a convenience function around