mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-17 21:35:20 +01:00
Now all locks are symmetric
Test fixes Fix test Fix comment
This commit is contained in:
parent
b3aa9e99a9
commit
e8eca4be37
22 changed files with 183 additions and 223 deletions
48
examples/big-struct-opt.rs
Normal file
48
examples/big-struct-opt.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
//! examples/big-struct-opt.rs
|
||||
//!
|
||||
//! Example on how to initialize a large struct without needing to copy it via `LateResources`,
|
||||
//! effectively saving stack space needed for the copies.
|
||||
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use panic_halt as _;
|
||||
|
||||
/// Some big struct
|
||||
pub struct BigStruct {
|
||||
/// Big content
|
||||
pub data: [u8; 2048],
|
||||
}
|
||||
|
||||
impl BigStruct {
|
||||
fn new() -> Self {
|
||||
BigStruct { data: [22; 2048] }
|
||||
}
|
||||
}
|
||||
|
||||
#[rtic::app(device = lm3s6965)]
|
||||
mod app {
|
||||
use super::BigStruct;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
#[resources]
|
||||
struct Resources {
|
||||
big_struct: &'static mut BigStruct,
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_: init::Context) -> init::LateResources {
|
||||
let big_struct = unsafe {
|
||||
static mut BIG_STRUCT: MaybeUninit<BigStruct> = MaybeUninit::uninit();
|
||||
|
||||
// write directly into the static storage
|
||||
BIG_STRUCT.as_mut_ptr().write(BigStruct::new());
|
||||
&mut *BIG_STRUCT.as_mut_ptr()
|
||||
};
|
||||
|
||||
init::LateResources {
|
||||
// assign the reference so we can use the resource
|
||||
big_struct,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -41,12 +41,12 @@ mod app {
|
|||
}
|
||||
|
||||
#[task(capacity = 2, resources = [count])]
|
||||
fn foo(_cx: foo::Context) {
|
||||
fn foo(mut _cx: foo::Context) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
*_cx.resources.count += 1;
|
||||
_cx.resources.count.lock(|count| *count += 1);
|
||||
|
||||
log::spawn(*_cx.resources.count).unwrap();
|
||||
log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
|
||||
}
|
||||
|
||||
// this wouldn't compile in `release` mode
|
||||
|
|
@ -59,12 +59,12 @@ mod app {
|
|||
// currently still present in the Tasks enum
|
||||
#[cfg(never)]
|
||||
#[task(capacity = 2, resources = [count])]
|
||||
fn foo2(_cx: foo2::Context) {
|
||||
fn foo2(mut _cx: foo2::Context) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
*_cx.resources.count += 10;
|
||||
_cx.resources.count.lock(|count| *count += 10);
|
||||
|
||||
log::spawn(*_cx.resources.count).unwrap();
|
||||
log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
|
||||
}
|
||||
|
||||
// this wouldn't compile in `release` mode
|
||||
|
|
|
|||
|
|
@ -38,12 +38,12 @@ mod app {
|
|||
}
|
||||
|
||||
#[task(capacity = 2, resources = [count])]
|
||||
fn foo(_cx: foo::Context) {
|
||||
fn foo(mut _cx: foo::Context) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
*_cx.resources.count += 1;
|
||||
_cx.resources.count.lock(|count| *count += 1);
|
||||
|
||||
log::spawn(*_cx.resources.count).unwrap();
|
||||
log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
|
||||
}
|
||||
|
||||
// this wouldn't compile in `release` mode
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ mod app {
|
|||
}
|
||||
|
||||
// Direct destructure
|
||||
#[task(binds = UART0, resources = [a, b, c])]
|
||||
#[task(binds = UART0, resources = [&a, &b, &c])]
|
||||
fn uart0(cx: uart0::Context) {
|
||||
let a = cx.resources.a;
|
||||
let b = cx.resources.b;
|
||||
|
|
@ -42,7 +42,7 @@ mod app {
|
|||
}
|
||||
|
||||
// De-structure-ing syntax
|
||||
#[task(binds = UART1, resources = [a, b, c])]
|
||||
#[task(binds = UART1, resources = [&a, &b, &c])]
|
||||
fn uart1(cx: uart1::Context) {
|
||||
let uart1::Resources { a, b, c } = cx.resources;
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ use rtic::Mutex;
|
|||
mod app {
|
||||
use cortex_m_semihosting::{debug, hprintln};
|
||||
use lm3s6965::Interrupt;
|
||||
use rtic::Exclusive;
|
||||
|
||||
#[resources]
|
||||
struct Resources {
|
||||
|
|
@ -49,11 +48,8 @@ mod app {
|
|||
|
||||
hprintln!("UART1(STATE = {})", *STATE).unwrap();
|
||||
|
||||
// just to show that `shared` can be accessed directly
|
||||
*c.resources.shared += 0;
|
||||
|
||||
// second argument has type `Exclusive<u32>`
|
||||
super::advance(STATE, Exclusive(c.resources.shared));
|
||||
// second argument has type `resources::shared`
|
||||
super::advance(STATE, c.resources.shared);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@ mod app {
|
|||
}
|
||||
|
||||
#[idle(resources = [c])]
|
||||
fn idle(c: idle::Context) -> ! {
|
||||
fn idle(mut c: idle::Context) -> ! {
|
||||
loop {
|
||||
if let Some(byte) = c.resources.c.dequeue() {
|
||||
if let Some(byte) = c.resources.c.lock(|c| c.dequeue()) {
|
||||
hprintln!("received message: {}", byte).unwrap();
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS);
|
||||
|
|
@ -48,7 +48,7 @@ mod app {
|
|||
}
|
||||
|
||||
#[task(binds = UART0, resources = [p])]
|
||||
fn uart0(c: uart0::Context) {
|
||||
c.resources.p.enqueue(42).unwrap();
|
||||
fn uart0(mut c: uart0::Context) {
|
||||
c.resources.p.lock(|p| p.enqueue(42).unwrap());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,11 +52,15 @@ mod app {
|
|||
}
|
||||
|
||||
#[task(binds = GPIOB, priority = 2, resources = [shared])]
|
||||
fn gpiob(c: gpiob::Context) {
|
||||
// the higher priority task does *not* need a critical section
|
||||
*c.resources.shared += 1;
|
||||
fn gpiob(mut c: gpiob::Context) {
|
||||
// the higher priority task does still need a critical section
|
||||
let shared = c.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
|
||||
hprintln!("D - shared = {}", *c.resources.shared).unwrap();
|
||||
*shared
|
||||
});
|
||||
|
||||
hprintln!("D - shared = {}", shared).unwrap();
|
||||
}
|
||||
|
||||
#[task(binds = GPIOC, priority = 3)]
|
||||
|
|
|
|||
|
|
@ -47,18 +47,23 @@ mod app {
|
|||
|
||||
// `shared` can be accessed from this context
|
||||
#[task(binds = UART0, resources = [shared])]
|
||||
fn uart0(cx: uart0::Context) {
|
||||
let shared: &mut u32 = cx.resources.shared;
|
||||
*shared += 1;
|
||||
fn uart0(mut cx: uart0::Context) {
|
||||
let shared = cx.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
*shared
|
||||
});
|
||||
|
||||
hprintln!("UART0: shared = {}", shared).unwrap();
|
||||
}
|
||||
|
||||
// `shared` can be accessed from this context
|
||||
#[task(binds = UART1, resources = [shared])]
|
||||
fn uart1(cx: uart1::Context) {
|
||||
*cx.resources.shared += 1;
|
||||
fn uart1(mut cx: uart1::Context) {
|
||||
let shared = cx.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
*shared
|
||||
});
|
||||
|
||||
hprintln!("UART1: shared = {}", cx.resources.shared).unwrap();
|
||||
hprintln!("UART1: shared = {}", shared).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,18 +42,23 @@ mod app {
|
|||
|
||||
// `shared` can be accessed from this context
|
||||
#[task(binds = UART0, resources = [shared])]
|
||||
fn uart0(cx: uart0::Context) {
|
||||
let shared: &mut u32 = cx.resources.shared;
|
||||
*shared += 1;
|
||||
fn uart0(mut cx: uart0::Context) {
|
||||
let shared = cx.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
*shared
|
||||
});
|
||||
|
||||
hprintln!("UART0: shared = {}", shared).unwrap();
|
||||
}
|
||||
|
||||
// `shared` can be accessed from this context
|
||||
#[task(binds = UART1, resources = [shared])]
|
||||
fn uart1(cx: uart1::Context) {
|
||||
*cx.resources.shared += 1;
|
||||
fn uart1(mut cx: uart1::Context) {
|
||||
let shared = cx.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
*shared
|
||||
});
|
||||
|
||||
hprintln!("UART1: shared = {}", cx.resources.shared).unwrap();
|
||||
hprintln!("UART1: shared = {}", shared).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
//! `examples/shared-with-init.rs`
|
||||
|
||||
#![deny(unsafe_code)]
|
||||
#![deny(warnings)]
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use panic_halt as _;
|
||||
use rtic::app;
|
||||
|
||||
pub struct MustBeSend;
|
||||
|
||||
#[app(device = lm3s6965)]
|
||||
mod app {
|
||||
use super::MustBeSend;
|
||||
use cortex_m_semihosting::debug;
|
||||
use lm3s6965::Interrupt;
|
||||
|
||||
#[resources]
|
||||
struct Resources {
|
||||
#[init(None)]
|
||||
shared: Option<MustBeSend>,
|
||||
}
|
||||
|
||||
#[init(resources = [shared])]
|
||||
fn init(c: init::Context) -> init::LateResources {
|
||||
// this `message` will be sent to task `UART0`
|
||||
let message = MustBeSend;
|
||||
*c.resources.shared = Some(message);
|
||||
|
||||
rtic::pend(Interrupt::UART0);
|
||||
|
||||
init::LateResources {}
|
||||
}
|
||||
|
||||
#[task(binds = UART0, resources = [shared])]
|
||||
fn uart0(c: uart0::Context) {
|
||||
if let Some(message) = c.resources.shared.take() {
|
||||
// `message` has been received
|
||||
drop(message);
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -36,9 +36,9 @@ mod app {
|
|||
}
|
||||
|
||||
#[idle(resources = [c])]
|
||||
fn idle(c: idle::Context) -> ! {
|
||||
fn idle(mut c: idle::Context) -> ! {
|
||||
loop {
|
||||
if let Some(byte) = c.resources.c.dequeue() {
|
||||
if let Some(byte) = c.resources.c.lock(|c| c.dequeue()) {
|
||||
hprintln!("received message: {}", byte).unwrap();
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS);
|
||||
|
|
@ -49,9 +49,9 @@ mod app {
|
|||
}
|
||||
|
||||
#[task(binds = UART0, resources = [p])]
|
||||
fn uart0(c: uart0::Context) {
|
||||
fn uart0(mut c: uart0::Context) {
|
||||
static mut KALLE: u32 = 0;
|
||||
*KALLE += 1;
|
||||
c.resources.p.enqueue(42).unwrap();
|
||||
c.resources.p.lock(|p| p.enqueue(42).unwrap());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,11 +23,8 @@ mod app {
|
|||
y: Option<NotSend>,
|
||||
}
|
||||
|
||||
#[init(resources = [y])]
|
||||
fn init(c: init::Context) -> init::LateResources {
|
||||
// equivalent to late resource initialization
|
||||
*c.resources.y = Some(NotSend { _0: PhantomData });
|
||||
|
||||
#[init]
|
||||
fn init(_: init::Context) -> init::LateResources {
|
||||
init::LateResources {
|
||||
x: NotSend { _0: PhantomData },
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,29 +31,18 @@ mod app {
|
|||
s3: u32, // idle & uart0
|
||||
}
|
||||
|
||||
#[init(resources = [o1, o4, o5, o6, s3])]
|
||||
fn init(c: init::Context) -> init::LateResources {
|
||||
// owned by `init` == `&'static mut`
|
||||
let _: &'static mut u32 = c.resources.o1;
|
||||
|
||||
// owned by `init` == `&'static` if read-only
|
||||
let _: &'static u32 = c.resources.o6;
|
||||
|
||||
// `init` has exclusive access to all resources
|
||||
let _: &mut u32 = c.resources.o4;
|
||||
let _: &mut u32 = c.resources.o5;
|
||||
let _: &mut u32 = c.resources.s3;
|
||||
|
||||
#[init]
|
||||
fn init(_: init::Context) -> init::LateResources {
|
||||
init::LateResources {}
|
||||
}
|
||||
|
||||
#[idle(resources = [o2, &o4, s1, &s3])]
|
||||
fn idle(mut c: idle::Context) -> ! {
|
||||
// owned by `idle` == `&'static mut`
|
||||
let _: &'static mut u32 = c.resources.o2;
|
||||
let _: resources::o2 = c.resources.o2;
|
||||
|
||||
// owned by `idle` == `&'static` if read-only
|
||||
let _: &'static u32 = c.resources.o4;
|
||||
let _: &u32 = c.resources.o4;
|
||||
|
||||
// shared with `idle` == `Mutex`
|
||||
c.resources.s1.lock(|_| {});
|
||||
|
|
@ -69,13 +58,13 @@ mod app {
|
|||
#[task(binds = UART0, resources = [o3, s1, s2, &s3])]
|
||||
fn uart0(c: uart0::Context) {
|
||||
// owned by interrupt == `&mut`
|
||||
let _: &mut u32 = c.resources.o3;
|
||||
let _: resources::o3 = c.resources.o3;
|
||||
|
||||
// no `Mutex` proxy when access from highest priority task
|
||||
let _: &mut u32 = c.resources.s1;
|
||||
let _: resources::s1 = c.resources.s1;
|
||||
|
||||
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
|
||||
let _: &mut u32 = c.resources.s2;
|
||||
let _: resources::s2 = c.resources.s2;
|
||||
|
||||
// `&` if read-only
|
||||
let _: &u32 = c.resources.s3;
|
||||
|
|
@ -87,6 +76,6 @@ mod app {
|
|||
let _: &u32 = c.resources.o5;
|
||||
|
||||
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
|
||||
let _: &mut u32 = c.resources.s2;
|
||||
let _: resources::s2 = c.resources.s2;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,9 +61,11 @@ mod app {
|
|||
// l2 ok (task_local)
|
||||
// e1 ok (lock_free)
|
||||
#[task(priority = 1, binds = UART0, resources = [shared, l2, e1])]
|
||||
fn uart0(cx: uart0::Context) {
|
||||
let shared: &mut u32 = cx.resources.shared;
|
||||
*shared += 1;
|
||||
fn uart0(mut cx: uart0::Context) {
|
||||
let shared = cx.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
*shared
|
||||
});
|
||||
*cx.resources.e1 += 10;
|
||||
hprintln!("UART0: shared = {}", shared).unwrap();
|
||||
hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap();
|
||||
|
|
@ -73,9 +75,11 @@ mod app {
|
|||
// `shared` can be accessed from this context
|
||||
// e1 ok (lock_free)
|
||||
#[task(priority = 1, binds = UART1, resources = [shared, e1])]
|
||||
fn uart1(cx: uart1::Context) {
|
||||
let shared: &mut u32 = cx.resources.shared;
|
||||
*shared += 1;
|
||||
fn uart1(mut cx: uart1::Context) {
|
||||
let shared = cx.resources.shared.lock(|shared| {
|
||||
*shared += 1;
|
||||
*shared
|
||||
});
|
||||
|
||||
hprintln!("UART1: shared = {}", shared).unwrap();
|
||||
hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap();
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ mod app {
|
|||
#[task(priority = 2, resources = [shared])]
|
||||
fn foo(cx: foo::Context) {
|
||||
let _: cyccnt::Instant = cx.scheduled;
|
||||
let _: &mut u32 = cx.resources.shared;
|
||||
let _: resources::shared = cx.resources.shared;
|
||||
let _: foo::Resources = cx.resources;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue