From 6aa0fb450f417ce899b43f4539eb226b391a0f2e Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Thu, 8 Apr 2021 18:25:09 +0200 Subject: Goodbye static mut --- macros/src/codegen/resources.rs | 58 ++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'macros/src/codegen/resources.rs') diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index fa52b86d..53523696 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -4,13 +4,20 @@ use rtic_syntax::{analyze::Ownership, ast::App}; use crate::{analyze::Analysis, check::Extra, codegen::util}; -/// Generates `static [mut]` variables and resource proxies +/// Generates `static` variables and resource proxies +/// Early resources are stored in `RacyCell` +/// Late resource are stored in `RacyCell>` +/// +/// Safety: +/// - RacyCell access is `unsafe`. +/// - RacyCell is always written to before user access, thus +// the generated code for user access can safely `assume_init`. pub fn codegen( app: &App, analysis: &Analysis, extra: &Extra, ) -> ( - // mod_app -- the `static [mut]` variables behind the proxies + // mod_app -- the `static` variables behind the proxies Vec, // mod_resources -- the `resources` module TokenStream2, @@ -24,35 +31,50 @@ pub fn codegen( let mangled_name = util::mark_internal_ident(&name); { + // TODO: do we really need this in the single core case + // late resources in `util::link_section_uninit` let section = if expr.is_none() { util::link_section_uninit(true) } else { None }; + // resource type and assigned value let (ty, expr) = if let Some(expr) = expr { - (quote!(#ty), quote!(#expr)) + // early resource + ( + quote!(rtic::RacyCell<#ty>), + quote!(rtic::RacyCell::new(#expr)), + ) } else { + // late resource ( - quote!(core::mem::MaybeUninit<#ty>), - quote!(core::mem::MaybeUninit::uninit()), + quote!(rtic::RacyCell>), + quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())), ) }; let attrs = &res.attrs; + + // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); mod_app.push(quote!( #[allow(non_upper_case_globals)] + // #[doc = #doc] #[doc(hidden)] #(#attrs)* #(#cfgs)* #section - static mut #mangled_name: #ty = #expr; + static #mangled_name: #ty = #expr; )); } let r_prop = &res.properties; + // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); + if !r_prop.task_local && !r_prop.lock_free { mod_resources.push(quote!( + // #[doc = #doc] + #[doc(hidden)] #[allow(non_camel_case_types)] #(#cfgs)* pub struct #name<'a> { @@ -73,15 +95,23 @@ pub fn codegen( } )); - let ptr = if expr.is_none() { - quote!( - #(#cfgs)* - #mangled_name.as_mut_ptr() + let (ptr, _doc) = if expr.is_none() { + // late resource + ( + quote!( + #(#cfgs)* + #mangled_name.get_mut_unchecked().as_mut_ptr() + ), + "late", ) } else { - quote!( - #(#cfgs)* - &mut #mangled_name + // early resource + ( + quote!( + #(#cfgs)* + #mangled_name.get_mut_unchecked() + ), + "early", ) }; @@ -92,6 +122,8 @@ pub fn codegen( None => 0, }; + // let doc = format!(" RTIC internal ({} resource): {}:{}", doc, file!(), line!()); + mod_app.push(util::impl_mutex( extra, cfgs, -- cgit v1.2.3 From 50f26e78eda57cb1f9f815aa93b357c2cd1a0205 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Thu, 22 Apr 2021 18:38:42 +0200 Subject: Keep comments --- macros/src/codegen/dispatchers.rs | 2 ++ macros/src/codegen/module.rs | 1 + macros/src/codegen/post_init.rs | 1 + macros/src/codegen/resources.rs | 4 +++- macros/src/codegen/software_tasks.rs | 1 + macros/src/codegen/timer_queue.rs | 4 ++++ 6 files changed, 12 insertions(+), 1 deletion(-) (limited to 'macros/src/codegen/resources.rs') diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index 6aca901a..382e33fe 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -26,6 +26,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec>(); + // For future use // let doc = format!( // "Software tasks to be dispatched at priority level {}", // level, @@ -53,6 +54,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec Vec { } for (i, (monotonic, _)) in app.monotonics.iter().enumerate() { + // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); // stmts.push(quote!(#[doc = #doc])); diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index 53523696..a623ea60 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -31,7 +31,6 @@ pub fn codegen( let mangled_name = util::mark_internal_ident(&name); { - // TODO: do we really need this in the single core case // late resources in `util::link_section_uninit` let section = if expr.is_none() { util::link_section_uninit(true) @@ -56,6 +55,7 @@ pub fn codegen( let attrs = &res.attrs; + // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); mod_app.push(quote!( #[allow(non_upper_case_globals)] @@ -69,6 +69,7 @@ pub fn codegen( } let r_prop = &res.properties; + // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); if !r_prop.task_local && !r_prop.lock_free { @@ -122,6 +123,7 @@ pub fn codegen( None => 0, }; + // For future use // let doc = format!(" RTIC internal ({} resource): {}:{}", doc, file!(), line!()); mod_app.push(util::impl_mutex( diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index e42fb88d..0372e8ec 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -65,6 +65,7 @@ pub fn codegen( let mono_type = &monotonic.ty; let uninit = mk_uninit(); + // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); mod_app.push(quote!( #uninit diff --git a/macros/src/codegen/timer_queue.rs b/macros/src/codegen/timer_queue.rs index ed89af66..14e91054 100644 --- a/macros/src/codegen/timer_queue.rs +++ b/macros/src/codegen/timer_queue.rs @@ -35,6 +35,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec>(); + // For future use // let doc = "Tasks that can be scheduled".to_string(); items.push(quote!( // #[doc = #doc] @@ -61,6 +62,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec Vec>); + // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); items.push(quote!( #[doc(hidden)] @@ -80,6 +83,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec