Closed perlindgren closed 2 years ago
Overall I think you have captured what I was thinking about, great job! This is a feature that will help users express their intent (moving or having a resource).
Regarding the static mut for task local.
fn uart0(c: uart0::Context) {
static mut KALLE: u32 = 0;
*KALLE += 1;
c.resources.p.enqueue(42).unwrap();
}
Is accepted, while, the below code is rejected.... spot the diff...
#[task(binds = UART0, resources = [p])]
fn uart0(c: uart0::Context) {
c.resources.p.enqueue(42).unwrap();
static mut KALLE: u32 = 0;
*KALLE += 1;
}
This is due an implementation detail (not really a bug, just not implemented). A more severe problem is that static mut without unsafe is NOT idiomatic Rust (perhaps one of the objections from @therealprof).
The above proposal would remove this problem, by having idiomatic access to mutable resources, declared inside of the Resources
section. The example is kind of lengthy, but it depicts all permutations of use (I think). In practical use it will be as short and to the point as the rest of RTFM.
The overarching ideas have been implemented in RTIC 1.0.0. Please re-open if needed.
EDIT 2020-04-38
Following up on a discussion with @korken89, and the recent comparison done by @therealprof on various ways to share data to interrupt handlers. RTFM came out on top regarding, robustness/reliability (static guarantees), performance and code-size, but argued against due to added complexity.
The added complexity with RTFM can to some extent be explained by the asymmetric resource access pattern (tasks with highest or unique access to a resource gets a owned pointer the resource, while other tasks need to use the lock API).
One way to approach the problem is to follow the lines of #17, with the addition of explicitly moved resources.
Semantics:
[lock_free], there might be several tasks with the same priority accessing the resource without critical section.
[task_local], there must be only one task, similar to a task local resource, but (optionally) set-up by init. This is similar to move. (Using the keyword
move
causes problem, since it is reserved "r#move", would be acceptable but somewhat ugly.)Implementation:
lock_free
resources will be clear.Ergonomics/Usability: Syntax can be discussed. It should however indicate the semantics in an intuitive way.
lock_free
refers to the scheduling (you get exclusive access).task_local
the resource is moved to the task (becoming a task local resource).Limitations: This will not allow further
move
of resources at run-time (following the static nature and guarantees by RTFM).Alternative solutions. RTFM already allows you to express this behaviour implicitly, so doing nothing is an option. However, these new constructs might appeal to the non-RTFM:ers out there. With the current implementation, the asymmetric API can be confusing. This approach would go well with #17, letting the designer having explicit control over
task_local
andlock_free
resources.Implications. A stepping stone towards a fully symmetric API. We can also get rid of task local variables as
static
, now they will be declared together with other resources instead, for good/bad, not sure.