-
Notifications
You must be signed in to change notification settings - Fork 101
Tracking Issue for Mutex Trait RFC [#377] #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As for the name, I suggest |
@Disasm Thanks! I think we should have a time period for proposing names and then we go for a vote. |
Having already had a full RFC for the concept I don't think we really need a vote on the name; I'd be happy for you to go ahead with whatever you think is sensible. |
I also don't think we need a vote. It's bikeshedding really... |
I agree with @adamgreig and @therealprof. We shouldn't watershed the name choice, it's not that important. |
As an outside view: given that
calling it |
Hi @korken89 |
Thanks for the input, let's have a small discussion (yay/nay) on @geomasi, I'll fix it now. |
In today's WG meeting we decided to go with |
Lets continue, @rust-embedded/hal do you agree that this fits under HAL or would you propose another group? |
@korken89 HAL is maybe not ideal, how do we feel about @rust-embedded/resources ? |
IMO HAL team (focused on abstractions and code portability) makes more sense than Resources (focused on docs, PR, and guides). |
I don't quite see a Mutex trait as a Hardware abstraction but more of a implementation convention which seems closer to a resource. But hey, just throwing it out there, I'm not too attached to it. ;) |
You guys are using a different meaning of resource here, where the "resources" that James speaks of is what the WG team refers to ;) Theres lots of bikeshedding potential here. IMO a mutex is not really abstracting over hardware. You can implement it with intermediary crates that already abstract over e.g. interrupt masking and atomic operations. What we have here is a classical SW interface, like for example pthreads. First sentence of phtreads manpage:
So following the pthreads example, what we are cooking up here is an API. Edit: |
I'm certainly aware of the role of the team, especially since I happen to be a member... ;)
As James said, the role of the team is managing documentation and guidelines and to me the mutex traits feels a lot closer to a guideline for writing embedded software in Rust than hardware abstraction. |
It feels like both HAL and Resources do not match perfectly with the purpose of this mutex crate.
I suggest placing this crate inside the Resources team, just to save time required for this to happen. |
Resources seems like a good placement, but HAL team should be closely involved because they are the ones that can provide the best "hands on" requirements. |
I was just made aware of this RFC and was quite surprised by parts of it. I hope this is an appropriate channel to ask such questions -- if not, please direct me elsewhere. :) In particular, I am quite surprised by the argument of
fn bump_concurrently(x: &Mutex<i32>) {
rayon::join(
|| *x.lock().unwrap() += 1,
|| *x.lock().unwrap() += 1,
);
} Is that correct?
static MUT: Mutex<RefCell<i32>> = Mutex::new(RefCell::new(0));
#[entry]
fn init() -> ! {
let mut r = &MUT; // Note that r is mutable
r.lock(|data| {
let mut r2 = &MUT;
r2.lock(|data| {/* oops, acquired the same lock twice. deadlock? */} );
});
} It seems deadlocks are only avoided for generic code that doesn't know that the type implementing
fn nest(
mtx1: &mut impl core_mutex::Mutex<Data = i32>,
mtx2: &mut impl core_mutex::Mutex<Data = i32>,
) {
mtx1.lock(|data| mtx2.lock(|data| {}) )
}
// And now concurrently call `nest(mtx1, mtx2)` and `nest(mtx2, mtx1)`. So, "deadlock-safe by construction" seems to be a best-effort property, but not a full guarantee? (With "by construction" I am expecting guarantees, like Rust code being "memory safe by construction".) |
Yes. This is a byproduct of the decisions which lead to this mutex design. For the full history, take a look at my original MR to To summarize, the mutex design tries to abstract over a very broad range of 'things' which could be considered a mutex. This includes the more traditional mutex types (like the one in I tried to describe the differences in more detail in this comment.
It does. The deadlock prevention only works as long as the types are not But IMO, the trait design does not help with deadlock prevention too much, it just needs to be this way to allow implementation for types like RTFM's mutex which do deadlock prevention.
Yes. As with your previous point, I don't think we gain deadlock prevention just from the mutex design, but from certain implementations and the traits need to be compatible with those ... |
Ah, that makes a lot of sense! Thanks for explaining that. |
So, resources team for now then seems to be the consensus :) |
@korken89 So how do we get this repository started? Do you want me to create one? |
@therealprof That would be great! I will then add implementation and then we can start reviewing it. |
https://github.com/rust-embedded/mutex-trait And I assume you hit the close button by accident? |
Ops, thanks for fixing! |
I have added implementation and fixed the repo settings: https://github.com/rust-embedded/mutex-trait |
Friendly reminder for review |
I took a brief look at the crate:
Except the possible merge into |
The "deadlock safe by construction" thing doesn't work out when the trait is implemented on immutable references by the way. let a = core::cell::RefCell::new(0);
(&a).lock(|av| {
*av += 1;
(&a).lock(|_no| {});
}); It also forces one to use the |
You can avoid writing (&a) by simply changing it to take the reference earlier, e.g. let a = core::cell::RefCell::new(0);
let a = &a;
a.lock(|av| {
*av += 1;
a.lock(|_no| {});
}); |
Sure, but it still feels like an odd API |
Most of my comments in #395 (comment) are addressed in rust-embedded/mutex-trait#5, should be ready for 0.1 soon ™️ |
Why are |
Ah, missed those when adding the type. Opened rust-embedded/mutex-trait#6 to add them. |
This is now live 🎉 I've set the resources team as the crate owner for now since I believe that was our conclusion at the time, but perhaps we can discuss that in the upcoming chat about teams. |
I am looking to implement CriticalSection and multithreaded (CS+Spinlock) mutex for ESP32. I am wondering what the status of this RFC and its implementation is. I noticed rust-embedded/mutex-trait#12, which mentions changes might be desirable. Is that something likely to happen soon? |
This is the tracking issue for the Mutex trait RFC #377, the following questions need to be ironed out before the RFC is implemented:
core-mutex
was proposed,mutex-trait
was selected)Lets start with 1 and 2.
CC @rust-embedded/all
The text was updated successfully, but these errors were encountered: