diff --git a/macros/src/codegen/shared_resources.rs b/macros/src/codegen/shared_resources.rs index 0e61b67936..18e96b074e 100644 --- a/macros/src/codegen/shared_resources.rs +++ b/macros/src/codegen/shared_resources.rs @@ -67,9 +67,11 @@ pub fn codegen( } )); + // ptr is an Fn closure that return *mut T let ptr = quote!( #(#cfgs)* - #mangled_name.get_mut() as *mut _ + || { #mangled_name.get_mut() as *mut _ } + // || { #mangled_name.get_mut_unchecked().as_mut_ptr() } ); let ceiling = match analysis.ownerships.get(name) { diff --git a/macros/src/codegen/shared_resources_struct.rs b/macros/src/codegen/shared_resources_struct.rs index f276724469..d6acce190c 100644 --- a/macros/src/codegen/shared_resources_struct.rs +++ b/macros/src/codegen/shared_resources_struct.rs @@ -47,18 +47,15 @@ pub fn codegen( let mangled_name = util::static_shared_resource_ident(&name); if !res.properties.lock_free { + lt = Some(quote!('a)); if access.is_shared() { // [&x] (shared) - lt = Some(quote!('a)); - fields.push(quote!( #(#cfgs)* pub #name: &'a #ty )); } else { // Resource proxy - lt = Some(quote!('a)); - fields.push(quote!( #(#cfgs)* pub #name: shared_resources::#name<'a> @@ -76,7 +73,7 @@ pub fn codegen( // Lock-all related fields_mut.push(quote!( #(#cfgs)* - pub #name: &'static mut #ty + pub #name: &#lt mut #ty )); values_mut.push(quote!( @@ -159,7 +156,7 @@ pub fn codegen( #[allow(non_snake_case)] #[allow(non_camel_case_types)] #[doc = #doc_mut] - pub struct #ident_mut { + pub struct #ident_mut<#lt> { #(#fields_mut,)* } ); @@ -176,16 +173,15 @@ pub fn codegen( extra, &vec![], // TODO: what cfg should go here? quote!(#ident), - quote!(#ident_mut), + quote!(#ident_mut<#lt>), max_ceiling, quote!(self.priority()), - quote!(&mut #ident_mut::new()), + quote!(|| { &mut #ident_mut::new() }), ), quote!( // Used by the lock-all API #[inline(always)] pub unsafe fn priority(&self) -> &rtic::export::Priority { - //panic!("here {:?}", self); self.#name.priority() } ), @@ -207,7 +203,7 @@ pub fn codegen( } // Used by the lock-all API - impl #ident_mut { + impl<#lt> #ident_mut<#lt> { #[inline(always)] pub unsafe fn new() -> Self { #ident_mut { diff --git a/src/export.rs b/src/export.rs index 89d1e18ba5..74b1d2ffc2 100644 --- a/src/export.rs +++ b/src/export.rs @@ -134,7 +134,7 @@ where #[cfg(armv7m)] #[inline(always)] pub unsafe fn lock( - ptr: *mut T, + ptr: impl Fn() -> *mut T, priority: &Priority, ceiling: u8, nvic_prio_bits: u8, @@ -145,19 +145,19 @@ pub unsafe fn lock( if current < ceiling { if ceiling == (1 << nvic_prio_bits) { priority.set(u8::max_value()); - let r = interrupt::free(|_| f(&mut *ptr)); + let r = interrupt::free(|_| f(&mut *ptr())); priority.set(current); r } else { priority.set(ceiling); basepri::write(logical2hw(ceiling, nvic_prio_bits)); - let r = f(&mut *ptr); + let r = f(&mut *ptr()); // inside of lock basepri::write(logical2hw(current, nvic_prio_bits)); priority.set(current); r } } else { - f(&mut *ptr) + f(&mut *ptr()) } } @@ -171,7 +171,7 @@ pub unsafe fn lock( #[cfg(not(armv7m))] #[inline(always)] pub unsafe fn lock( - ptr: *mut T, + ptr: impl Fn() -> *mut T, priority: &Priority, ceiling: u8, _nvic_prio_bits: u8, @@ -181,11 +181,11 @@ pub unsafe fn lock( if current < ceiling { priority.set(u8::max_value()); - let r = interrupt::free(|_| f(&mut *ptr)); + let r = interrupt::free(|_| f(&mut *ptr())); priority.set(current); r } else { - f(&mut *ptr) + f(&mut *ptr()) } }