2019-04-21 20:45:24 +02:00
|
|
|
# Critical sections
|
|
|
|
|
|
|
|
When a resource (static variable) is shared between two, or more, tasks that run
|
2019-08-21 10:17:27 +02:00
|
|
|
at different priorities some form of mutual exclusion is required to mutate the
|
2019-04-21 20:45:24 +02:00
|
|
|
memory in a data race free manner. In RTFM we use priority-based critical
|
2019-08-21 10:17:27 +02:00
|
|
|
sections to guarantee mutual exclusion (see the [Immediate Ceiling Priority
|
|
|
|
Protocol][icpp]).
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
[icpp]: https://en.wikipedia.org/wiki/Priority_ceiling_protocol
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
The critical section consists of temporarily raising the *dynamic* priority of
|
|
|
|
the task. While a task is within this critical section all the other tasks that
|
|
|
|
may request the resource are *not allowed to start*.
|
|
|
|
|
|
|
|
How high must the dynamic priority be to ensure mutual exclusion on a particular
|
2019-08-21 10:28:07 +02:00
|
|
|
resource? The [ceiling analysis](ceilings.html) is in charge of
|
2019-04-21 20:45:24 +02:00
|
|
|
answering that question and will be discussed in the next section. This section
|
|
|
|
will focus on the implementation of the critical section.
|
|
|
|
|
|
|
|
## Resource proxy
|
|
|
|
|
|
|
|
For simplicity, let's look at a resource shared by two tasks that run at
|
|
|
|
different priorities. Clearly one of the task can preempt the other; to prevent
|
|
|
|
a data race the *lower priority* task must use a critical section when it needs
|
|
|
|
to modify the shared memory. On the other hand, the higher priority task can
|
|
|
|
directly modify the shared memory because it can't be preempted by the lower
|
|
|
|
priority task. To enforce the use of a critical section on the lower priority
|
2019-08-21 10:17:27 +02:00
|
|
|
task we give it a *resource proxy*, whereas we give a unique reference
|
2019-04-21 20:45:24 +02:00
|
|
|
(`&mut-`) to the higher priority task.
|
|
|
|
|
|
|
|
The example below shows the different types handed out to each task:
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
#[rtfm::app(device = ..)]
|
|
|
|
const APP: () = {
|
2019-08-21 10:17:27 +02:00
|
|
|
struct Resources {
|
|
|
|
#[init(0)]
|
|
|
|
x: u64,
|
|
|
|
}
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[interrupt(binds = UART0, priority = 1, resources = [x])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn foo(c: foo::Context) {
|
|
|
|
// resource proxy
|
2019-08-21 10:17:27 +02:00
|
|
|
let mut x: resources::x = c.resources.x;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
x.lock(|x: &mut u64| {
|
|
|
|
// critical section
|
|
|
|
*x += 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[interrupt(binds = UART1, priority = 2, resources = [x])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn bar(c: foo::Context) {
|
2019-08-21 10:17:27 +02:00
|
|
|
let mut x: &mut u64 = c.resources.x;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
*x += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ..
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
Now let's see how these types are created by the framework.
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
fn foo(c: foo::Context) {
|
|
|
|
// .. user code ..
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(c: bar::Context) {
|
|
|
|
// .. user code ..
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod resources {
|
2019-08-21 10:17:27 +02:00
|
|
|
pub struct x {
|
2019-04-21 20:45:24 +02:00
|
|
|
// ..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod foo {
|
|
|
|
pub struct Resources {
|
2019-08-21 10:17:27 +02:00
|
|
|
pub x: resources::x,
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Context {
|
|
|
|
pub resources: Resources,
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Resources<'a> {
|
2019-08-21 10:17:27 +02:00
|
|
|
pub x: &'a mut u64,
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Context {
|
|
|
|
pub resources: Resources,
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const APP: () = {
|
2019-08-21 10:17:27 +02:00
|
|
|
static mut x: u64 = 0;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
impl rtfm::Mutex for resources::x {
|
2019-04-21 20:45:24 +02:00
|
|
|
type T = u64;
|
|
|
|
|
|
|
|
fn lock<R>(&mut self, f: impl FnOnce(&mut u64) -> R) -> R {
|
|
|
|
// we'll check this in detail later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe fn UART0() {
|
|
|
|
foo(foo::Context {
|
|
|
|
resources: foo::Resources {
|
2019-08-21 10:17:27 +02:00
|
|
|
x: resources::x::new(/* .. */),
|
2019-04-21 20:45:24 +02:00
|
|
|
},
|
|
|
|
// ..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe fn UART1() {
|
|
|
|
bar(bar::Context {
|
|
|
|
resources: bar::Resources {
|
2019-08-21 10:17:27 +02:00
|
|
|
x: &mut x,
|
2019-04-21 20:45:24 +02:00
|
|
|
},
|
|
|
|
// ..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
## `lock`
|
|
|
|
|
|
|
|
Let's now zoom into the critical section itself. In this example, we have to
|
|
|
|
raise the dynamic priority to at least `2` to prevent a data race. On the
|
|
|
|
Cortex-M architecture the dynamic priority can be changed by writing to the
|
|
|
|
`BASEPRI` register.
|
|
|
|
|
|
|
|
The semantics of the `BASEPRI` register are as follows:
|
|
|
|
|
|
|
|
- Writing a value of `0` to `BASEPRI` disables its functionality.
|
|
|
|
- Writing a non-zero value to `BASEPRI` changes the priority level required for
|
|
|
|
interrupt preemption. However, this only has an effect when the written value
|
|
|
|
is *lower* than the priority level of current execution context, but note that
|
|
|
|
a lower hardware priority level means higher logical priority
|
|
|
|
|
|
|
|
Thus the dynamic priority at any point in time can be computed as
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
dynamic_priority = max(hw2logical(BASEPRI), hw2logical(static_priority))
|
|
|
|
```
|
|
|
|
|
|
|
|
Where `static_priority` is the priority programmed in the NVIC for the current
|
|
|
|
interrupt, or a logical `0` when the current context is `idle`.
|
|
|
|
|
|
|
|
In this particular example we could implement the critical section as follows:
|
|
|
|
|
|
|
|
> **NOTE:** this is a simplified implementation
|
|
|
|
|
|
|
|
``` rust
|
2019-08-21 10:17:27 +02:00
|
|
|
impl rtfm::Mutex for resources::x {
|
2019-04-21 20:45:24 +02:00
|
|
|
type T = u64;
|
|
|
|
|
|
|
|
fn lock<R, F>(&mut self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut u64) -> R,
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
// start of critical section: raise dynamic priority to `2`
|
|
|
|
asm!("msr BASEPRI, 192" : : : "memory" : "volatile");
|
|
|
|
|
|
|
|
// run user code within the critical section
|
2019-08-21 10:17:27 +02:00
|
|
|
let r = f(&mut x);
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// end of critical section: restore dynamic priority to its static value (`1`)
|
|
|
|
asm!("msr BASEPRI, 0" : : : "memory" : "volatile");
|
|
|
|
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Here it's important to use the `"memory"` clobber in the `asm!` block. It
|
|
|
|
prevents the compiler from reordering memory operations across it. This is
|
2019-08-21 10:17:27 +02:00
|
|
|
important because accessing the variable `x` outside the critical section would
|
2019-04-21 20:45:24 +02:00
|
|
|
result in a data race.
|
|
|
|
|
|
|
|
It's important to note that the signature of the `lock` method prevents nesting
|
|
|
|
calls to it. This is required for memory safety, as nested calls would produce
|
2019-08-21 10:17:27 +02:00
|
|
|
multiple unique references (`&mut-`) to `x` breaking Rust aliasing rules. See
|
2019-04-21 20:45:24 +02:00
|
|
|
below:
|
|
|
|
|
|
|
|
``` rust
|
2019-08-21 10:17:27 +02:00
|
|
|
#[interrupt(binds = UART0, priority = 1, resources = [x])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn foo(c: foo::Context) {
|
|
|
|
// resource proxy
|
2019-08-21 10:17:27 +02:00
|
|
|
let mut res: resources::x = c.resources.x;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
res.lock(|x: &mut u64| {
|
|
|
|
res.lock(|alias: &mut u64| {
|
2019-08-21 10:17:27 +02:00
|
|
|
//~^ error: `res` has already been uniquely borrowed (`&mut-`)
|
2019-04-21 20:45:24 +02:00
|
|
|
// ..
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Nesting
|
|
|
|
|
|
|
|
Nesting calls to `lock` on the *same* resource must be rejected by the compiler
|
|
|
|
for memory safety but nesting `lock` calls on *different* resources is a valid
|
|
|
|
operation. In that case we want to make sure that nesting critical sections
|
|
|
|
never results in lowering the dynamic priority, as that would be unsound, and we
|
|
|
|
also want to optimize the number of writes to the `BASEPRI` register and
|
|
|
|
compiler fences. To that end we'll track the dynamic priority of the task using
|
|
|
|
a stack variable and use that to decide whether to write to `BASEPRI` or not. In
|
|
|
|
practice, the stack variable will be optimized away by the compiler but it still
|
|
|
|
provides extra information to the compiler.
|
|
|
|
|
|
|
|
Consider this program:
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
#[rtfm::app(device = ..)]
|
|
|
|
const APP: () = {
|
2019-08-21 10:17:27 +02:00
|
|
|
struct Resources {
|
|
|
|
#[init(0)]
|
|
|
|
x: u64,
|
|
|
|
#[init(0)]
|
|
|
|
y: u64,
|
|
|
|
}
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
#[init]
|
|
|
|
fn init() {
|
|
|
|
rtfm::pend(Interrupt::UART0);
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[interrupt(binds = UART0, priority = 1, resources = [x, y])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn foo(c: foo::Context) {
|
2019-08-21 10:17:27 +02:00
|
|
|
let mut x = c.resources.x;
|
|
|
|
let mut y = c.resources.y;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
y.lock(|y| {
|
|
|
|
*y += 1;
|
|
|
|
|
|
|
|
*x.lock(|x| {
|
|
|
|
x += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
*y += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
// mid-point
|
|
|
|
|
|
|
|
x.lock(|x| {
|
|
|
|
*x += 1;
|
|
|
|
|
|
|
|
y.lock(|y| {
|
|
|
|
*y += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
*x += 1;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[interrupt(binds = UART1, priority = 2, resources = [x])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn bar(c: foo::Context) {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[interrupt(binds = UART2, priority = 3, resources = [y])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn baz(c: foo::Context) {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
|
|
|
// ..
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
The code generated by the framework looks like this:
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
// omitted: user code
|
|
|
|
|
|
|
|
pub mod resources {
|
2019-08-21 10:17:27 +02:00
|
|
|
pub struct x<'a> {
|
2019-04-21 20:45:24 +02:00
|
|
|
priority: &'a Cell<u8>,
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
impl<'a> x<'a> {
|
2019-04-21 20:45:24 +02:00
|
|
|
pub unsafe fn new(priority: &'a Cell<u8>) -> Self {
|
2019-08-21 10:17:27 +02:00
|
|
|
x { priority }
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn priority(&self) -> &Cell<u8> {
|
|
|
|
self.priority
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
// repeat for `y`
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod foo {
|
|
|
|
pub struct Context {
|
|
|
|
pub resources: Resources,
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Resources<'a> {
|
2019-08-21 10:17:27 +02:00
|
|
|
pub x: resources::x<'a>,
|
|
|
|
pub y: resources::y<'a>,
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const APP: () = {
|
2019-08-21 10:17:27 +02:00
|
|
|
use cortex_m::register::basepri;
|
|
|
|
|
2019-04-21 20:45:24 +02:00
|
|
|
#[no_mangle]
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe fn UART1() {
|
2019-04-21 20:45:24 +02:00
|
|
|
// the static priority of this interrupt (as specified by the user)
|
2019-08-21 10:17:27 +02:00
|
|
|
const PRIORITY: u8 = 2;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// take a snashot of the BASEPRI
|
2019-08-21 10:17:27 +02:00
|
|
|
let initial = basepri::read();
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
let priority = Cell::new(PRIORITY);
|
2019-08-21 10:17:27 +02:00
|
|
|
bar(bar::Context {
|
|
|
|
resources: bar::Resources::new(&priority),
|
2019-04-21 20:45:24 +02:00
|
|
|
// ..
|
|
|
|
});
|
|
|
|
|
|
|
|
// roll back the BASEPRI to the snapshot value we took before
|
2019-08-21 10:17:27 +02:00
|
|
|
basepri::write(initial); // same as the `asm!` block we saw before
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
// similarly for `UART0` / `foo` and `UART2` / `baz`
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
impl<'a> rtfm::Mutex for resources::x<'a> {
|
2019-04-21 20:45:24 +02:00
|
|
|
type T = u64;
|
|
|
|
|
|
|
|
fn lock<R>(&mut self, f: impl FnOnce(&mut u64) -> R) -> R {
|
|
|
|
unsafe {
|
|
|
|
// the priority ceiling of this resource
|
|
|
|
const CEILING: u8 = 2;
|
|
|
|
|
|
|
|
let current = self.priority().get();
|
|
|
|
if current < CEILING {
|
|
|
|
// raise dynamic priority
|
|
|
|
self.priority().set(CEILING);
|
2019-08-21 10:17:27 +02:00
|
|
|
basepri::write(logical2hw(CEILING));
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
let r = f(&mut y);
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// restore dynamic priority
|
2019-08-21 10:17:27 +02:00
|
|
|
basepri::write(logical2hw(current));
|
2019-04-21 20:45:24 +02:00
|
|
|
self.priority().set(current);
|
|
|
|
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
// dynamic priority is high enough
|
2019-08-21 10:17:27 +02:00
|
|
|
f(&mut y)
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
// repeat for resource `y`
|
2019-04-21 20:45:24 +02:00
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
At the end the compiler will optimize the function `foo` into something like
|
|
|
|
this:
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
fn foo(c: foo::Context) {
|
|
|
|
// NOTE: BASEPRI contains the value `0` (its reset value) at this point
|
|
|
|
|
|
|
|
// raise dynamic priority to `3`
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe { basepri::write(160) }
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
// the two operations on `y` are merged into one
|
|
|
|
y += 2;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
// BASEPRI is not modified to access `x` because the dynamic priority is high enough
|
|
|
|
x += 1;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// lower (restore) the dynamic priority to `1`
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe { basepri::write(224) }
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// mid-point
|
|
|
|
|
|
|
|
// raise dynamic priority to `2`
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe { basepri::write(192) }
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
x += 1;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// raise dynamic priority to `3`
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe { basepri::write(160) }
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
y += 1;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// lower (restore) the dynamic priority to `2`
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe { basepri::write(192) }
|
2019-04-21 20:45:24 +02:00
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
// NOTE: it would be sound to merge this operation on `x` with the previous one but
|
2019-04-21 20:45:24 +02:00
|
|
|
// compiler fences are coarse grained and prevent such optimization
|
2019-08-21 10:17:27 +02:00
|
|
|
x += 1;
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// lower (restore) the dynamic priority to `1`
|
2019-08-21 10:17:27 +02:00
|
|
|
unsafe { basepri::write(224) }
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
// NOTE: BASEPRI contains the value `224` at this point
|
|
|
|
// the UART0 handler will restore the value to `0` before returning
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## The BASEPRI invariant
|
|
|
|
|
|
|
|
An invariant that the RTFM framework has to preserve is that the value of the
|
|
|
|
BASEPRI at the start of an *interrupt* handler must be the same value it has
|
|
|
|
when the interrupt handler returns. BASEPRI may change during the execution of
|
|
|
|
the interrupt handler but running an interrupt handler from start to finish
|
|
|
|
should not result in an observable change of BASEPRI.
|
|
|
|
|
|
|
|
This invariant needs to be preserved to avoid raising the dynamic priority of a
|
|
|
|
handler through preemption. This is best observed in the following example:
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
#[rtfm::app(device = ..)]
|
|
|
|
const APP: () = {
|
2019-08-21 10:17:27 +02:00
|
|
|
struct Resources {
|
|
|
|
#[init(0)]
|
|
|
|
x: u64,
|
|
|
|
}
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
#[init]
|
|
|
|
fn init() {
|
|
|
|
// `foo` will run right after `init` returns
|
|
|
|
rtfm::pend(Interrupt::UART0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[task(binds = UART0, priority = 1)]
|
|
|
|
fn foo() {
|
|
|
|
// BASEPRI is `0` at this point; the dynamic priority is currently `1`
|
|
|
|
|
|
|
|
// `bar` will preempt `foo` at this point
|
|
|
|
rtfm::pend(Interrupt::UART1);
|
|
|
|
|
|
|
|
// BASEPRI is `192` at this point (due to a bug); the dynamic priority is now `2`
|
|
|
|
// this function returns to `idle`
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[task(binds = UART1, priority = 2, resources = [x])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn bar() {
|
|
|
|
// BASEPRI is `0` (dynamic priority = 2)
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
x.lock(|x| {
|
2019-04-21 20:45:24 +02:00
|
|
|
// BASEPRI is raised to `160` (dynamic priority = 3)
|
|
|
|
|
|
|
|
// ..
|
|
|
|
});
|
|
|
|
|
|
|
|
// BASEPRI is restored to `192` (dynamic priority = 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[idle]
|
|
|
|
fn idle() -> ! {
|
|
|
|
// BASEPRI is `192` (due to a bug); dynamic priority = 2
|
|
|
|
|
|
|
|
// this has no effect due to the BASEPRI value
|
|
|
|
// the task `foo` will never be executed again
|
|
|
|
rtfm::pend(Interrupt::UART0);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:17:27 +02:00
|
|
|
#[task(binds = UART2, priority = 3, resources = [x])]
|
2019-04-21 20:45:24 +02:00
|
|
|
fn baz() {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
IMPORTANT: let's say we *forget* to roll back `BASEPRI` in `UART1` -- this would
|
|
|
|
be a bug in the RTFM code generator.
|
|
|
|
|
|
|
|
``` rust
|
|
|
|
// code generated by RTFM
|
|
|
|
|
|
|
|
const APP: () = {
|
|
|
|
// ..
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe fn UART1() {
|
|
|
|
// the static priority of this interrupt (as specified by the user)
|
|
|
|
const PRIORITY: u8 = 2;
|
|
|
|
|
|
|
|
// take a snashot of the BASEPRI
|
2019-08-21 10:17:27 +02:00
|
|
|
let initial = basepri::read();
|
2019-04-21 20:45:24 +02:00
|
|
|
|
|
|
|
let priority = Cell::new(PRIORITY);
|
|
|
|
bar(bar::Context {
|
|
|
|
resources: bar::Resources::new(&priority),
|
|
|
|
// ..
|
|
|
|
});
|
|
|
|
|
|
|
|
// BUG: FORGOT to roll back the BASEPRI to the snapshot value we took before
|
2019-08-21 10:17:27 +02:00
|
|
|
basepri::write(initial);
|
2019-04-21 20:45:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
The consequence is that `idle` will run at a dynamic priority of `2` and in fact
|
|
|
|
the system will never again run at a dynamic priority lower than `2`. This
|
|
|
|
doesn't compromise the memory safety of the program but affects task scheduling:
|
|
|
|
in this particular case tasks with a priority of `1` will never get a chance to
|
|
|
|
run.
|