mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-16 21:05:35 +01:00
fix: make mutexes !Send
Each mutex is generated uniquely for each task, it is unsound to send them between tasks. But they are `Send`. Before, it wasn't an issue, because you couldn't share non-`'static` data between them, but with \#1043 you can make the mutex `'static`. Thus we need to use actual tools that Rust provides and out out from `Send`. Currently, mutexes are simple ZSTs with `PhantomData<&'a ()>`, which is `Send`. We replace it with `PhantomData<(&'a (), *const u8)>`, and return `Sync` back via `unsafe` implementation. It is trivially sound, because mutexes have no method methods that accept `&self`. See https://doc.rust-lang.org/std/sync/struct.Exclusive.html for details.
This commit is contained in:
parent
dbc5d3ceca
commit
3d0ced0bd1
1 changed files with 6 additions and 1 deletions
|
|
@ -62,9 +62,14 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
|
|||
#[allow(non_camel_case_types)]
|
||||
#(#cfgs)*
|
||||
pub struct #shared_name<'a> {
|
||||
__rtic_internal_p: ::core::marker::PhantomData<&'a ()>,
|
||||
__rtic_internal_p: ::core::marker::PhantomData<(&'a (), *const u8)>,
|
||||
}
|
||||
|
||||
// Opt out from `Send`.
|
||||
// `#shared_name` is trivially `Sync` because there are no `&self` methods.
|
||||
// See https://doc.rust-lang.org/std/sync/struct.Exclusive.html .
|
||||
unsafe impl<'a> Sync for #shared_name<'a> {}
|
||||
|
||||
#(#cfgs)*
|
||||
impl<'a> #shared_name<'a> {
|
||||
#[inline(always)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue