From 6bf1c76d842b40cd1b24b4a517e42e624ade836f Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 21 Jul 2021 15:46:09 +0200 Subject: [PATCH 1/6] book/resources: do not use the lock API in the very first example instead stick to `#[local]` resources --- book/en/src/by-example/resources.md | 29 ++++++++------ examples/resource.rs | 61 +++++++++++++++++------------ 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/book/en/src/by-example/resources.md b/book/en/src/by-example/resources.md index 3a3e0b7660..b0e7590445 100644 --- a/book/en/src/by-example/resources.md +++ b/book/en/src/by-example/resources.md @@ -7,9 +7,11 @@ Resources are data visible only to functions declared within the `#[app]` module. The framework gives the user complete control over which context can access which resource. -All resources are declared as a single `struct` within the `#[app]` -module. Each field in the structure corresponds to a different resource. -The `struct` must be annotated with the following attribute: `#[resources]`. +All resources are declared as *two* `struct`s within the `#[app]` module. +Each field in these structures corresponds to a different resource. +One `struct` must be annotated with the attribute `#[local]`. +The other `struct` must be annotated with the attribute `#[shared]`. +The difference between these two sets of resources will be covered later. Resources can optionally be given an initial value using the `#[init]` attribute. Resources that are not given an initial value are referred to as @@ -17,12 +19,13 @@ attribute. Resources that are not given an initial value are referred to as page. Each context (task handler, `init` or `idle`) must declare the resources it -intends to access in its corresponding metadata attribute using the `resources` -argument. This argument takes a list of resource names as its value. The listed -resources are made available to the context under the `resources` field of the -`Context` structure. +intends to access in its corresponding metadata attribute using either the +`local` or `shared` argument. This argument takes a list of resource names as +its value. The listed resources are made available to the context under the +`local` and `shared` fields of the `Context` structure. -The example application shown below contains two interrupt handlers that share access to a resource named `shared`. +The example application shown below contains two interrupt handlers. +Each handler has access to its own `#[local]` resource. ``` rust {{#include ../../../../examples/resource.rs}} @@ -33,13 +36,14 @@ $ cargo run --example resource {{#include ../../../../ci/expected/resource.run}} ``` -Note that the `shared` resource cannot be accessed from `idle`. Attempting to do so results in a compile error. +A `#[local]` resource cannot be accessed from outside the task it was associated to in a `#[task]` attribute. +Assigning the same `#[local]` resource to more than one task is a compile-time error. ## `lock` -Critical sections are required to access shared mutable data in a data race-free manner. +Critical sections are required to access `#[shared]` resources in a data race-free manner. -The `resources` field of the passed `Context` implements the [`Mutex`] trait for each shared resource accessible to the task. +The `shared` field of the passed `Context` implements the [`Mutex`] trait for each shared resource accessible to the task. The only method on this trait, [`lock`], runs its closure argument in a critical section. @@ -91,7 +95,7 @@ $ cargo run --example late {{#include ../../../../ci/expected/late.run}} ``` -## Only shared access +## Only shared (`&-`) access By default the framework assumes that all tasks require exclusive access (`&mut-`) to resources but it is possible to specify that a task only requires shared access (`&-`) to a resource using the `&resource_name` syntax in the `resources` list. @@ -121,4 +125,3 @@ There exists two other options dealing with resources this is safe. * `#[task_local]`: there must be only one task using this resource, similar to a `static mut` task local resource, but (optionally) set-up by init. - diff --git a/examples/resource.rs b/examples/resource.rs index 2c7dffe334..260f67535b 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -13,55 +13,68 @@ mod app { use lm3s6965::Interrupt; #[shared] - struct Shared { - shared: u32, - } + struct Shared {} #[local] - struct Local {} + struct Local { + local_to_uart0: i64, + local_to_uart1: i64, + } #[init] fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { rtic::pend(Interrupt::UART0); rtic::pend(Interrupt::UART1); - (Shared { shared: 0 }, Local {}, init::Monotonics()) + ( + Shared {}, + Local { + local_to_uart0: 0, + local_to_uart1: 0, + }, + init::Monotonics(), + ) } - // `shared` cannot be accessed from this context + // `#[local]` resources cannot be accessed from this context #[idle] fn idle(_cx: idle::Context) -> ! { debug::exit(debug::EXIT_SUCCESS); - // error: no `shared` field in `idle::Context` - // _cx.shared.shared += 1; + // error: no `local` field in `idle::Context` + // _cx.local.local_to_uart0 += 1; + + // error: no `local` field in `idle::Context` + // _cx.local.local_to_uart1 += 1; loop { cortex_m::asm::nop(); } } - // `shared` can be accessed from this context + // `local_to_uart0` can only be accessed from this context // defaults to priority 1 - #[task(binds = UART0, shared = [shared])] - fn uart0(mut cx: uart0::Context) { - let shared = cx.shared.shared.lock(|shared| { - *shared += 1; - *shared - }); + #[task(binds = UART0, local = [local_to_uart0])] + fn uart0(cx: uart0::Context) { + *cx.local.local_to_uart0 += 1; + let local_to_uart0 = cx.local.local_to_uart0; - hprintln!("UART0: shared = {}", shared).unwrap(); + // error: no `local_to_uart1` field in `uart0::LocalResources` + cx.local.local_to_uart1 += 1; + + hprintln!("UART0: local_to_uart0 = {}", local_to_uart0).unwrap(); } - // `shared` can be accessed from this context + // `shared` can only be accessed from this context // explicitly set to priority 2 - #[task(binds = UART1, shared = [shared], priority = 2)] - fn uart1(mut cx: uart1::Context) { - let shared = cx.shared.shared.lock(|shared| { - *shared += 1; - *shared - }); + #[task(binds = UART1, local = [local_to_uart1], priority = 2)] + fn uart1(cx: uart1::Context) { + *cx.local.local_to_uart1 += 1; + let local_to_uart1 = cx.local.local_to_uart1; - hprintln!("UART1: shared = {}", shared).unwrap(); + // error: no `local_to_uart0` field in `uart1::LocalResources` + // cx.local.local_to_uart0 += 1; + + hprintln!("UART1: local_to_uart1 = {}", local_to_uart1).unwrap(); } } From cd4e8183f658a52dc9c3aafc789a2f87a5c6345e Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 21 Jul 2021 15:59:08 +0200 Subject: [PATCH 2/6] book/resources: remove mentions of the field attribute #[init()] it no longer exists. all resources are now late resources --- book/en/src/by-example/resources.md | 30 +++++------------------------ examples/resource.rs | 1 + 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/book/en/src/by-example/resources.md b/book/en/src/by-example/resources.md index b0e7590445..11ba4cef90 100644 --- a/book/en/src/by-example/resources.md +++ b/book/en/src/by-example/resources.md @@ -13,17 +13,16 @@ One `struct` must be annotated with the attribute `#[local]`. The other `struct` must be annotated with the attribute `#[shared]`. The difference between these two sets of resources will be covered later. -Resources can optionally be given an initial value using the `#[init]` -attribute. Resources that are not given an initial value are referred to as -*late* resources and are covered in more detail in a follow-up section in this -page. - Each context (task handler, `init` or `idle`) must declare the resources it intends to access in its corresponding metadata attribute using either the `local` or `shared` argument. This argument takes a list of resource names as its value. The listed resources are made available to the context under the `local` and `shared` fields of the `Context` structure. +All resources are initialized at runtime, after the `#[init]` function returns. +The `#[init]` function must return the initial values for all resources; hence its return type includes the types of the `#[shared]` and `#[local]` structs. +Because resources are uninitialized during the execution of the `#[init]` function, they cannot be accessed within the `#[init]` function. + The example application shown below contains two interrupt handlers. Each handler has access to its own `#[local]` resource. @@ -56,7 +55,7 @@ The critical section created by the `lock` API is based on dynamic priorities: i [icpp]: https://en.wikipedia.org/wiki/Priority_ceiling_protocol [srp]: https://en.wikipedia.org/wiki/Stack_Resource_Policy -In the example below we have three interrupt handlers with priorities ranging from one to three. The two handlers with the lower priorities contend for the `shared` resource and need to lock the resource for accessing the data. The highest priority handler, which do nat access the `shared` resource, is free to preempt the critical section created by the +In the example below we have three interrupt handlers with priorities ranging from one to three. The two handlers with the lower priorities contend for the `shared` resource and need to lock the resource for accessing the data. The highest priority handler, which do not access the `shared` resource, is free to preempt the critical section created by the lowest priority handler. ``` rust @@ -76,25 +75,6 @@ As an extension to `lock`, and to reduce rightward drift, locks can be taken as {{#include ../../../../examples/multilock.rs}} ``` -## Late resources - -Late resources are resources that are not given an initial value at compile time using the `#[init]` attribute but instead are initialized at runtime using the `init::LateResources` values returned by the `init` function. - -Late resources are useful e.g., to *move* (as in transferring the ownership of) peripherals initialized in `init` into tasks. - -The example below uses late resources to establish a lockless, one-way channel between the `UART0` interrupt handler and the `idle` task. A single producer single consumer [`Queue`] is used as the channel. The queue is split into consumer and producer end points in `init` and then each end point is stored in a different resource; `UART0` owns the producer resource and `idle` owns the consumer resource. - -[`Queue`]: ../../../api/heapless/spsc/struct.Queue.html - -``` rust -{{#include ../../../../examples/late.rs}} -``` - -``` console -$ cargo run --example late -{{#include ../../../../ci/expected/late.run}} -``` - ## Only shared (`&-`) access By default the framework assumes that all tasks require exclusive access (`&mut-`) to resources but it is possible to specify that a task only requires shared access (`&-`) to a resource using the `&resource_name` syntax in the `resources` list. diff --git a/examples/resource.rs b/examples/resource.rs index 260f67535b..693ce89618 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -28,6 +28,7 @@ mod app { ( Shared {}, + // initial values for the `#[local]` resources Local { local_to_uart0: 0, local_to_uart1: 0, From ae1f9008a4b8b4e12d4580e25f240d9ee352e6a4 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 22 Jul 2021 08:28:11 +0200 Subject: [PATCH 3/6] comment out line that doesn't compile --- examples/resource.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/resource.rs b/examples/resource.rs index 693ce89618..dca0b37034 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -61,7 +61,7 @@ mod app { let local_to_uart0 = cx.local.local_to_uart0; // error: no `local_to_uart1` field in `uart0::LocalResources` - cx.local.local_to_uart1 += 1; + // cx.local.local_to_uart1 += 1; hprintln!("UART0: local_to_uart0 = {}", local_to_uart0).unwrap(); } From af631719f46f2669dad98461c9fdacbf7d16f83f Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 22 Jul 2021 08:42:44 +0200 Subject: [PATCH 4/6] update expected example output --- ci/expected/resource.run | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/expected/resource.run b/ci/expected/resource.run index ca12f0ed5e..87e2b37b57 100644 --- a/ci/expected/resource.run +++ b/ci/expected/resource.run @@ -1,2 +1,2 @@ -UART1: shared = 1 -UART0: shared = 2 +UART1: local_to_uart1 = 1 +UART0: local_to_uart0 = 2 From f9a7efb235854bcb73390988f43b2fae0868e99a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 22 Jul 2021 08:58:17 +0200 Subject: [PATCH 5/6] update expected example output (take 2) --- ci/expected/resource.run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/expected/resource.run b/ci/expected/resource.run index 87e2b37b57..9b60960f44 100644 --- a/ci/expected/resource.run +++ b/ci/expected/resource.run @@ -1,2 +1,2 @@ UART1: local_to_uart1 = 1 -UART0: local_to_uart0 = 2 +UART0: local_to_uart0 = 1 From 5805a05fac2fc5824009586b3ee2fd36dc27fbbf Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 22 Jul 2021 09:17:39 +0200 Subject: [PATCH 6/6] book/resources: rm #[task_local] mention; add #[lock_free] example the #[task_local] attribute was removed --- book/en/src/by-example/resources.md | 18 +++++---- ci/expected/lock-free.run | 14 +++++++ examples/lock-free.rs | 60 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 ci/expected/lock-free.run create mode 100644 examples/lock-free.rs diff --git a/book/en/src/by-example/resources.md b/book/en/src/by-example/resources.md index 11ba4cef90..855cde93a6 100644 --- a/book/en/src/by-example/resources.md +++ b/book/en/src/by-example/resources.md @@ -97,11 +97,15 @@ $ cargo run --example only-shared-access ## Lock-free resource access of mutable resources -There exists two other options dealing with resources +A critical section is *not* required to access a `#[shared]` resource that's only accessed by tasks running at the *same* priority. +In this case, you can opt out of the `lock` API by adding the `#[lock_free]` field-level attribute to the resource declaration (see example below). +Note that this is merely a convenience: if you do use the `lock` API, at runtime the framework will *not* produce a critical section. -* `#[lock_free]`: there might be several tasks with the same priority - accessing the resource without critical section. Since tasks with the - same priority never can preempt another task on the same priority - this is safe. -* `#[task_local]`: there must be only one task using this resource, - similar to a `static mut` task local resource, but (optionally) set-up by init. +``` rust +{{#include ../../../../examples/lock-free.rs}} +``` + +``` console +$ cargo run --example lock-free +{{#include ../../../../ci/expected/lock-free.run}} +``` diff --git a/ci/expected/lock-free.run b/ci/expected/lock-free.run new file mode 100644 index 0000000000..56f47a0be4 --- /dev/null +++ b/ci/expected/lock-free.run @@ -0,0 +1,14 @@ +GPIOA/start + GPIOA/counter = 1 +GPIOA/end +GPIOB/start + GPIOB/counter = 2 +GPIOB/end +GPIOA/start + GPIOA/counter = 3 +GPIOA/end +GPIOB/start + GPIOB/counter = 4 +GPIOB/end +GPIOA/start + GPIOA/counter = 5 diff --git a/examples/lock-free.rs b/examples/lock-free.rs new file mode 100644 index 0000000000..db74c7d8b0 --- /dev/null +++ b/examples/lock-free.rs @@ -0,0 +1,60 @@ +//! examples/lock-free.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + use cortex_m_semihosting::{debug, hprintln}; + use lm3s6965::Interrupt; + + #[shared] + struct Shared { + #[lock_free] // <- lock-free shared resource + counter: u64, + } + + #[local] + struct Local {} + + #[init] + fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { + rtic::pend(Interrupt::GPIOA); + + (Shared { counter: 0 }, Local {}, init::Monotonics()) + } + + #[task(binds = GPIOA, shared = [counter])] // <- same priority + fn gpioa(c: gpioa::Context) { + hprintln!("GPIOA/start").unwrap(); + rtic::pend(Interrupt::GPIOB); + + *c.shared.counter += 1; // <- no lock API required + let counter = *c.shared.counter; + hprintln!(" GPIOA/counter = {}", counter).unwrap(); + + if counter == 5 { + debug::exit(debug::EXIT_SUCCESS); + } + hprintln!("GPIOA/end").unwrap(); + } + + #[task(binds = GPIOB, shared = [counter])] // <- same priority + fn gpiob(c: gpiob::Context) { + hprintln!("GPIOB/start").unwrap(); + rtic::pend(Interrupt::GPIOA); + + *c.shared.counter += 1; // <- no lock API required + let counter = *c.shared.counter; + hprintln!(" GPIOB/counter = {}", counter).unwrap(); + + if counter == 5 { + debug::exit(debug::EXIT_SUCCESS); + } + hprintln!("GPIOB/end").unwrap(); + } +}