-
Notifications
You must be signed in to change notification settings - Fork 101
a sound {bare_metal,cortex_m,etc}::Mutex #388
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
Conversation
Hey @japaric, I need to give this a deeper read, but it makes sense based on previous statements and explorations you've made re: the current assumptions of single core in the current rust-embedded ecosystem. Would you mind expanding how this proposal does or does not work with #377, particularly as it has recently entered FCP? If there are any soundness or convenience holes in #377, I'd prefer if we address those now, before merging the RFC. |
@jamesmunns This proposal is orthogonal to RFC #377. RFC #377 specifies a (unifies the) This proposal is about fixing a particular mutex implementation, the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me. Thanks for the detailed write-up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have waited for this day, thanks for starting the push towards single vs. multicore before it comes to bite us.
On the note of fixing or removing, I think this is a difficult question as it would incur extra cost on single core systems while being sound for multicore systems.
I will ponder the implications for this a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely in favor of going down this road, thanks for writing this up!
It would be great to figure out what it would take to stabilize auto trait
. The tracking issue rust-lang/rust#13231 is pretty inactive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for writing this up. Good stuff.
Since you asked for name-bikeshedding:
We could discuss if we want to aim for a more generic term than SingleCore. For example, there are SoCs with multiple SingleCore CPUs on them but still working with shared memory.
In such a case, SingleCore* has a tendency to become ambiguous.
SingleProcessingSync
might be the academically correct term (as in being the opposite of MultiProcessingSync).
Sorry I don't follow. If you have a SoC with multiple CPUs containing a single core the SoC is by definition multi core. Where exactly is the ambiguity here? |
I'm not too hot on the |
Co-Authored-By: Jonas Schievink <[email protected]>
3a6d0c0
Co-Authored-By: Jonas Schievink <[email protected]>
@therealprof perhaps @andre-richter is thinking of something along the lines of: a Cortex-M4 microprocessor has one core but one SoC / IC can contain two M4 microprocessors both connected to a memory block, external to the die of each microprocessor but within the die of the SoC / IC. This is not exactly the same an Intel microprocessor with 8 cores all connected to the same internal memory (e.g. L3 cache) -- in this case the cores and the memory are on the same microprocessor die. I don't think the concept of die / SoC is too relevant. In principle, one could have one M4 SoC / IC and one M3 SoC / IC both connected to external SRAM (another IC) and write an application as a single Rust crate for the whole system. Each SoC is single-core but the overall system is multi-core.
I think this name may be misleading. For example, one may have to deal with both traits, It is also possible to write a program that uses a single core for a multi-processing OS like Linux (using thread affinity). That program only needs to satisfy the I personally think that // crate-name: send_sync
/// trait definitions
pub use core::marker::Send as CrossCoreSend;
pub use core::marker::Sync as CrossCoreSync;
pub unsafe auto trait WithinACoreSend {}
pub unsafe auto trait WithinACoreSync {} // crate-name: heapless
/// trait user
mod pool {
use send_sync::CrossCoreSend;
// owned pointer managed by a pool allocator
pub struct Box<T> { /* .. */ }
unsafe impl<T> CrossCoreSend for Box<T> {} // instead of Send
} |
Indeed, I fully agree. For a long time people have been calling multiple cores in a single package multi-core indendent of their connection/configuration. In fact the first dual-core Intel chip (Presler) consisted of two dies placed in the same package. But it really does not matter. |
Thats what I meant. But I am okay only counting cores (sounds like a band name: "The Counting Cores" 🤣). SameCoreSync would read little easier, but might semantically not be as correct as "within a". Just thinking out loud. I really like CrossCore*. |
What about a future situation where multithreading within a single core cortex-m microprocessor becomes possible? |
While I think many like that a replacement for the re-exported |
bare_metal::Mutex
, re-exported in thecortex-m
and other crates, is a mutexbased on critical sections that temporarily disable all interrupts. As provided
today this abstraction is unsound in multi-core context because
Mutex
can bestored in
static
variables, which are visible to all cores, but interruptmasking is not sufficient to synchronize (potentially) parallel access (i.e.
access from different cores) to memory.
This document proposes that we deprecate the existing
Mutex
abstraction infavor of a mutex that properly expresses the idea that mutexes based on
interrupt-masking is only "Sync" in single-core context.
Rendered