replace the ceiling token with a preemption threshold token

This commit is contained in:
Jorge Aparicio 2017-05-08 12:05:42 -05:00
parent 2063697c62
commit fc4cb7d472
18 changed files with 260 additions and 216 deletions

View file

@ -1,6 +1,6 @@
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C1, C2, C3, C4, C5, P2, Resource};
use rtfm::{C1, C2, C3, C4, C5, P2, Resource, T2};
static R1: Resource<i32, C4> = Resource::new(0);
static R2: Resource<i32, C3> = Resource::new(0);
@ -9,27 +9,27 @@ static R4: Resource<i32, C5> = Resource::new(0);
static R5: Resource<i32, C1> = Resource::new(0);
static R6: Resource<i32, C2> = Resource::new(0);
fn j1(prio: P2, ceil: C2) {
ceil.raise(
&R1, |ceil| {
// NOTE SC = System Ceiling, P = task Priority
fn j1(prio: P2, thr: T2) {
thr.raise(
&R1, |thr| {
// NOTE PT = Preemption Threshold, TP = Task Priority
// CAN access a resource with ceiling RC when SC > RC
let r2 = R2.access(&prio, ceil);
// CAN access a resource with ceiling RC when PT > RC
let r2 = R2.access(&prio, thr);
// CAN access a resource with ceiling RC when SC == RC
let r3 = R3.access(&prio, ceil);
// CAN access a resource with ceiling RC when PT == RC
let r3 = R3.access(&prio, thr);
// CAN'T access a resource with ceiling RC when SC < RC
let r4 = R4.access(&prio, ceil);
// CAN'T access a resource with ceiling RC when PT < RC
let r4 = R4.access(&prio, thr);
//~^ error
// CAN'T access a resource with ceiling RC when RC < P
let r5 = R5.access(&prio, ceil);
// CAN'T access a resource with ceiling RC when RC < TP
let r5 = R5.access(&prio, thr);
//~^ error
// CAN access a resource with ceiling RC when RC == P
let r6 = R6.access(&prio, ceil);
// CAN access a resource with ceiling RC when RC == tP
let r6 = R6.access(&prio, thr);
}
);
}

View file

@ -1,19 +1,19 @@
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C2, C3, P0, P2, Resource};
use rtfm::{C2, C3, P0, P2, Resource, T2};
static R1: Resource<(), C3> = Resource::new(());
fn j1(prio: P2, ceil: C2) {
let c3 = ceil.raise(
&R1, |ceil| {
fn j1(prio: P2, thr: T2) {
let t3 = thr.raise(
&R1, |thr| {
// forbidden: ceiling token can't outlive the critical section
ceil //~ error
thr //~ error
}
);
// Would be bad: lockless access to a resource with ceiling = 3
let r2 = R1.access(&prio, c3);
let r2 = R1.access(&prio, t3);
}
fn j2(prio: P0) {

View file

@ -1,42 +1,36 @@
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C16, C1, C2, C3, P1, P16, P2, P3, Resource};
use rtfm::{CMax, C2, P1, P2, P3, PMax, Resource, T1, T2, T3, TMax};
static R1: Resource<i32, C2> = Resource::new(0);
// You CAN'T use `raise` to lower the system ceiling
fn j1(prio: P3, ceil: C3) {
ceil.raise(&R1, |ceil| {});
//~^ error
}
// You don't need to raise the ceiling to access a resource with ceiling equal
// to the task priority.
fn j2(prio: P2, ceil: C2) {
ceil.raise(&R1, |_| {});
fn j1(prio: P2, thr: T2) {
thr.raise(&R1, |_| {});
//~^ error
// OK
let r1 = R1.access(&prio, &ceil);
let r1 = R1.access(&prio, &thr);
}
// You CAN access a resource with ceiling C from a task with priority P if C > P
// and you raise the ceiling first
fn j3(prio: P1, ceil: C1) {
// if you raise the preemption threshold first
fn j2(prio: P1, thr: T1) {
// OK
ceil.raise(&R1, |ceil| { let r1 = R1.access(&prio, ceil); })
thr.raise(&R1, |thr| { let r1 = R1.access(&prio, thr); })
}
static R2: Resource<i32, C16> = Resource::new(0);
static R2: Resource<i32, CMax> = Resource::new(0);
// Tasks with priority less than P16 can't access a resource with ceiling C16
fn j4(prio: P1, ceil: C1) {
ceil.raise(&R2, |ceil| {});
// Tasks with priority less than P16 can't access a resource with ceiling CMax
fn j4(prio: P1, thr: T1) {
thr.raise(&R2, |thr| {});
//~^ error
}
// Only tasks with priority P16 can access a resource with ceiling C16
fn j5(prio: P16, ceil: C16) {
// Only tasks with priority P16 can directly access a resource with ceiling CMax
fn j5(prio: PMax, thr: TMax) {
// OK
let r2 = R2.access(&prio, &ceil);
let r2 = R2.access(&prio, &thr);
}

View file

@ -5,7 +5,7 @@
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C16, P0, P1};
use rtfm::{P0, P1, T0, TMax};
use device::interrupt::Exti0;
peripherals!(device, {
@ -22,9 +22,9 @@ peripherals!(device, {
tasks!(device, {});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}

View file

@ -6,7 +6,7 @@
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C0, C16, P0, P1};
use rtfm::{P0, P1, T0, TMax};
use device::interrupt::Exti0;
peripherals!(device, {
@ -28,9 +28,9 @@ mod foo {
tasks!(device, {});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}

View file

@ -1,13 +1,13 @@
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C1, C2, C3, C4, P1, P3, Resource};
use rtfm::{C2, P1, P3, Resource, T1, T3};
static R1: Resource<i32, C2> = Resource::new(0);
fn j1(prio: P1, ceil: C1) {
ceil.raise(
&R1, |ceil| {
let r1 = R1.access(&prio, ceil);
fn j1(prio: P1, thr: T1) {
thr.raise(
&R1, |thr| {
let r1 = R1.access(&prio, thr);
// `j2` preempts this critical section
rtfm::request(j2);
@ -15,12 +15,12 @@ fn j1(prio: P1, ceil: C1) {
);
}
fn j2(_task: Task, prio: P3, ceil: C3) {
fn j2(_task: Task, prio: P3, thr: T3) {
rtfm::atomic(
|ceil| {
// OK C2 (R1's ceiling) <= C16 (system ceiling)
|thr| {
// OK C2 (R1's ceiling) <= T16 (preemption threshold)
// BAD C2 (R1's ceiling) < P3 (j2's priority)
let r1 = R1.access(&prio, &ceil);
let r1 = R1.access(&prio, &thr);
//~^ error
},
);

View file

@ -1,14 +1,14 @@
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C1, C2, C3, C4, P1, P3, Resource};
use rtfm::{C2, C4, P1, P3, Resource, T1, T3};
static R1: Resource<i32, C2> = Resource::new(0);
static R2: Resource<i32, C4> = Resource::new(0);
fn j1(prio: P1, ceil: C1) {
ceil.raise(
&R1, |ceil| {
let r1 = R1.access(&prio, ceil);
fn j1(prio: P1, thr: T1) {
thr.raise(
&R1, |thr| {
let r1 = R1.access(&prio, thr);
// `j2` preempts this critical section
rtfm::request(j2);
@ -16,12 +16,12 @@ fn j1(prio: P1, ceil: C1) {
);
}
fn j2(_task: Task, prio: P3, ceil: C3) {
ceil.raise(
&R2, |ceil| {
// OK C2 (R1's ceiling) <= C4 (system ceiling)
fn j2(_task: Task, prio: P3, thr: T3) {
thr.raise(
&R2, |thr| {
// OK C2 (R1's ceiling) <= T4 (preemption threshold)
// BAD C2 (R1's ceiling) < P3 (j2's priority)
let r1 = R1.access(&prio, ceil);
let r1 = R1.access(&prio, thr);
//~^ error
}
);

24
tests/cfail/raise.rs Normal file
View file

@ -0,0 +1,24 @@
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C2, CMax, P1, P3, Resource, T1, T3};
static R1: Resource<i32, C2> = Resource::new(0);
// You CAN'T use `raise` to lower the preemption level
fn j1(prio: P3, thr: T3) {
thr.raise(&R1, |thr| {});
//~^ error
}
static R2: Resource<i32, CMax> = Resource::new(0);
// You CAN'T `raise` the preemption level to the maximum
fn j2(prio: P1, thr: T1) {
thr.raise(&R2, |thr| {});
//~^ error
// Instead use `rtfm::atomic` to access a resource with ceiling C16
rtfm::atomic(|thr| {
let r2 = R2.access(&prio, thr);
});
}

View file

@ -5,7 +5,7 @@
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C0, C1, C16, P0, P1};
use rtfm::{P0, P1, T0, T1, TMax};
use device::interrupt::Exti0;
// WRONG: Tasks can't have a priority of 0.
@ -18,13 +18,13 @@ tasks!(device, {
},
});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}
fn j1(_task: Exti0, _prio: P1, _ceil: C1) {}
fn j1(_task: Exti0, _prio: P1, _thr: T1) {}
// fake device crate
extern crate core;

View file

@ -5,7 +5,7 @@
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C0, C1, C16, C2, P0, P1, P2};
use rtfm::{P0, P1, P2, T0, T1, T2, TMax};
use device::interrupt::{Exti0, Exti1};
// WRONG: Two tasks mapped to the same interrupt handler
@ -22,15 +22,15 @@ tasks!(device, {
},
});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}
fn j1(_task: Exti0, _prio: P1, _ceil: C1) {}
fn j1(_task: Exti0, _prio: P1, _thr: T1) {}
fn j2(_task: Exti0, _prio: P2, _ceil: C2) {}
fn j2(_task: Exti0, _prio: P2, _thr: T2) {}
// fake device crate
extern crate core;

View file

@ -6,7 +6,7 @@
extern crate cortex_m_rtfm as rtfm;
use device::interrupt::Exti0;
use rtfm::{C0, C1, C16, P0, P1};
use rtfm::{P0, P1, T0, T1, TMax};
tasks!(device, {
j1: Task {
@ -16,12 +16,12 @@ tasks!(device, {
},
});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
// WRONG. `idle` must have signature `fn(P0, C0) -> !`
fn idle(_: P0, _: C0) {}
fn idle(_: P0, _: T0) {}
fn j1(_task: Exti0, _prio: P1, _ceil: C1) {}
fn j1(_task: Exti0, _prio: P1, _thr: T1) {}
// fake device crate
extern crate core;

View file

@ -5,7 +5,7 @@
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C0, C1, C16, P0, P1};
use rtfm::{P0, P1, T0, T1, TMax};
use device::interrupt::Exti0;
tasks!(device, {
@ -16,14 +16,14 @@ tasks!(device, {
},
});
// WRONG. `init` must have signature `fn(P0, &C16)`
fn init(_: P0, _: &C1) {}
// WRONG. `init` must have signature `fn(P0, &TMax)`
fn init(_: P0, _: &T1) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}
fn j1(_task: Exti0, _prio: P1, _ceil: C1) {}
fn j1(_task: Exti0, _prio: P1, _thr: T1) {}
// fake device crate
extern crate core;

View file

@ -6,7 +6,7 @@
extern crate cortex_m_rtfm as rtfm;
use device::interrupt::Exti0;
use rtfm::{C0, C1, C16, C2, P0, P1, P2};
use rtfm::{P0, P1, P2, T0, T1, T2, TMax};
tasks!(device, {
j1: Task {
@ -16,14 +16,14 @@ tasks!(device, {
},
});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}
// Wrong priority token. Declared P1, got P2
fn j1(_task: Exti0, _prio: P2, _ceil: C2) {}
fn j1(_task: Exti0, _prio: P2, _thr: T2) {}
// fake device crate
extern crate core;

View file

@ -6,7 +6,7 @@
extern crate cortex_m_rtfm as rtfm;
use device::interrupt::{Exti0, Exti1};
use rtfm::{C0, C1, C16, P0, P1};
use rtfm::{P0, P1, T0, T1, TMax};
tasks!(device, {
j1: Task {
@ -16,14 +16,14 @@ tasks!(device, {
},
});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}
// Wrong task token. Declared Exti0, got Exti1
fn j1(_task: Exti1, _prio: P1, _ceil: C1) {}
fn j1(_task: Exti1, _prio: P1, _thr: T1) {}
// fake device crate
extern crate core;

View file

@ -5,7 +5,7 @@
#[macro_use]
extern crate cortex_m_rtfm as rtfm;
use rtfm::{C0, C1, C16, C2, P0, P1};
use rtfm::{C2, P0, P1, T0, T2, TMax};
use device::interrupt::Exti0;
tasks!(device, {
@ -16,14 +16,14 @@ tasks!(device, {
},
});
fn init(_: P0, _: &C16) {}
fn init(_: P0, _: &TMax) {}
fn idle(_: P0, _: C0) -> ! {
fn idle(_: P0, _: T0) -> ! {
loop {}
}
// Wrong ceiling token. `prio` and `ceil` must match in levels
fn j1(_task: Exti0, _prio: P1, _ceil: C2) {}
// Wrong ceiling token. `prio` and `thr` must match in levels
fn j1(_task: Exti0, _prio: P1, _thr: T2) {}
// fake device crate
extern crate core;