Closed perlindgren closed 2 years ago
The non symetrical API also introduces issues when using the resource proxy concrete types because the higher prio task doesn't get a resource proxy, instead it gets the resource directly. Example:
#[rtfm::app(device = lm3s6965)]
const APP: () = {
struct Resources {
// A resource
#[init(0)]
shared: u32,
}
#[task(resources = [shared])
fn normal_prio(c: normal_prio::Context) {
takes_proxy(c.resources.shared); // works
}
#[task(resources = [shared], priority = 5)
fn high_prio(c: high_prio::Context) {
// fails to compile, expected type `resources::shared` got `&mut u32`
takes_proxy(c.resources.shared);
}
};
fn takes_proxy(shared: resources::shared) {
shared.lock(|_s| {});
}
Close as solved.
Currently RTFM has the notion of asymmetric access to shared resources. Tasks with highest priority accessing
shared:T
gets a&mut T
API, while other tasks (if any) gets a resource proxy, with alock
API (formerclaim
). Theexamples/lock.rs
from the current upstream.This is unfortunate, as
gpiob
would need to be changed API if allowinggpioc
access toshared
.gpioa
to pass the resource access to another (external) function (like a HAL), we would need to wrapshared
byExclusive
(which gives alock
API to inner).Context
cannot have'static
lifetime if allowed to have&mut T
inresources
. This will prevent from captures in static stored generators (which requires'static
lifetime). Generators can be useful as is (initial experiments confirm the possibility), and are used under the hood forasync/await
, so if we want to keep this door open, we will eventually run into this problem.Proposal.
Offer only a single API (always resource proxy) for all shared resources. Pros:
Exclusive
.codegen
internally in RTFM. (Right now there a bunch of edge cases in thecodegen
).Cons:
Performance:
[resource] vs [mut resource] (or [&resource] vs [resource]) In a related RFC, Jorge proposed to distinguish between mutable and immutable access. With the above proposal, this will amount in a slight syntax extension. (Following Rust I would think that [mut resource] would be appropriate, but the &syntax would be backwards compatible, so I don't have a strong opinion).
Pros:
Cons:
Under the "Goodby Exclusive" proposal, the optimization can be done fully transparent to the API.
lock
will lend out&T
/&mut T
, for immutable/mutable access respectively and the compiler will block misuse.I might have missed something, looking forward to input. Best regards Per