aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/codegen.rs2
-rw-r--r--macros/src/codegen/dispatchers.rs12
-rw-r--r--macros/src/codegen/locals.rs6
-rw-r--r--macros/src/codegen/module.rs38
-rw-r--r--macros/src/codegen/post_init.rs15
-rw-r--r--macros/src/codegen/pre_init.rs6
-rw-r--r--macros/src/codegen/resources.rs58
-rw-r--r--macros/src/codegen/resources_struct.rs6
-rw-r--r--macros/src/codegen/software_tasks.rs14
-rw-r--r--macros/src/codegen/timer_queue.rs17
10 files changed, 116 insertions, 58 deletions
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs
index cf728a7d..167ba500 100644
--- a/macros/src/codegen.rs
+++ b/macros/src/codegen.rs
@@ -125,7 +125,7 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
rtic::export::interrupt::free(|_| {
use rtic::Monotonic as _;
use rtic::time::Clock as _;
- if let Some(m) = unsafe{ #app_path::#ident.as_ref() } {
+ if let Some(m) = unsafe{ #app_path::#ident.get_mut_unchecked() } {
if let Ok(v) = m.try_now() {
v
} else {
diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs
index dc33b1af..6aca901a 100644
--- a/macros/src/codegen/dispatchers.rs
+++ b/macros/src/codegen/dispatchers.rs
@@ -59,7 +59,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
// );
items.push(quote!(
#[doc(hidden)]
- static mut #rq: #rq_ty = #rq_expr;
+ static #rq: rtic::RacyCell<#rq_ty> = rtic::RacyCell::new(#rq_expr);
));
let arms = channel
@@ -86,8 +86,12 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
#(#cfgs)*
#t::#name => {
let #tupled =
- #inputs.get_unchecked(usize::from(index)).as_ptr().read();
- #fq.split().0.enqueue_unchecked(index);
+ #inputs
+ .get_unchecked()
+ .get_unchecked(usize::from(index))
+ .as_ptr()
+ .read();
+ #fq.get_mut_unchecked().split().0.enqueue_unchecked(index);
let priority = &rtic::export::Priority::new(PRIORITY);
#app_path::#name(
#locals_new
@@ -100,7 +104,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
.collect::<Vec<_>>();
stmts.push(quote!(
- while let Some((task, index)) = #rq.split().1.dequeue() {
+ while let Some((task, index)) = #rq.get_mut_unchecked().split().1.dequeue() {
match task {
#(#arms)*
}
diff --git a/macros/src/codegen/locals.rs b/macros/src/codegen/locals.rs
index 5725a151..0fb8c6d2 100644
--- a/macros/src/codegen/locals.rs
+++ b/macros/src/codegen/locals.rs
@@ -50,11 +50,11 @@ pub fn codegen(
items.push(quote!(
#(#cfgs)*
#[doc(hidden)]
- static mut #name: #ty = #expr
+ static #name: rtic::RacyCell<#ty> = rtic::RacyCell::new(#expr)
));
values.push(quote!(
#(#cfgs)*
- #name: &mut #name
+ #name: #name.get_mut_unchecked()
));
names.push(name);
pats.push(quote!(
@@ -64,7 +64,7 @@ pub fn codegen(
}
if lt.is_some() && has_cfgs {
- fields.push(quote!(__marker__: core::marker::PhantomData<&'a mut ()>));
+ fields.push(quote!(__marker__: core::marker::PhantomData<&'a ()>));
values.push(quote!(__marker__: core::marker::PhantomData));
}
diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs
index 50146c02..8c148ccb 100644
--- a/macros/src/codegen/module.rs
+++ b/macros/src/codegen/module.rs
@@ -245,14 +245,15 @@ pub fn codegen(
let input = #tupled;
unsafe {
- if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.dequeue()) {
+ if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.get_mut_unchecked().dequeue()) {
#app_path::#inputs
+ .get_mut_unchecked()
.get_unchecked_mut(usize::from(index))
.as_mut_ptr()
.write(input);
rtic::export::interrupt::free(|_| {
- #app_path::#rq.enqueue_unchecked((#app_path::#t::#name, index));
+ #app_path::#rq.get_mut_unchecked().enqueue_unchecked((#app_path::#t::#name, index));
});
rtic::pend(#device::#enum_::#interrupt);
@@ -304,6 +305,9 @@ pub fn codegen(
let user_imports = &app.user_imports;
let tq_marker = util::mark_internal_ident(&util::timer_queue_marker_ident());
+ // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
+ // items.push(quote!(#[doc = #doc]));
+
items.push(quote!(
/// Holds methods related to this monotonic
pub mod #m {
@@ -325,12 +329,16 @@ pub fn codegen(
impl SpawnHandle {
pub fn cancel(self) -> Result<#ty, ()> {
rtic::export::interrupt::free(|_| unsafe {
- let tq = &mut *#app_path::#tq.as_mut_ptr();
+ let tq = &mut *#app_path::#tq.get_mut_unchecked().as_mut_ptr();
if let Some((_task, index)) = tq.cancel_marker(self.marker) {
// Get the message
- let msg = #app_path::#inputs.get_unchecked(usize::from(index)).as_ptr().read();
+ let msg = #app_path::#inputs
+ .get_unchecked()
+ .get_unchecked(usize::from(index))
+ .as_ptr()
+ .read();
// Return the index to the free queue
- #app_path::#fq.split().0.enqueue_unchecked(index);
+ #app_path::#fq.get_mut_unchecked().split().0.enqueue_unchecked(index);
Ok(msg)
} else {
@@ -350,10 +358,10 @@ pub fn codegen(
pub fn reschedule_at(self, instant: rtic::time::Instant<#app_path::#mono_type>) -> Result<Self, ()>
{
rtic::export::interrupt::free(|_| unsafe {
- let marker = #tq_marker;
- #tq_marker = #tq_marker.wrapping_add(1);
+ let marker = *#tq_marker.get_mut_unchecked();
+ *#tq_marker.get_mut_unchecked() = #tq_marker.get_mut_unchecked().wrapping_add(1);
- let tq = &mut *#app_path::#tq.as_mut_ptr();
+ let tq = &mut *#app_path::#tq.get_mut_unchecked().as_mut_ptr();
tq.update_marker(self.marker, marker, instant, || #pend).map(|_| SpawnHandle { marker })
})
@@ -373,7 +381,7 @@ pub fn codegen(
D::T: Into<<#app_path::#mono_type as rtic::time::Clock>::T>,
{
- let instant = if rtic::export::interrupt::free(|_| unsafe { #app_path::#m_ident.is_none() }) {
+ let instant = if rtic::export::interrupt::free(|_| unsafe { #app_path::#m_ident.get_mut_unchecked().is_none() }) {
rtic::time::Instant::new(0)
} else {
#app_path::monotonics::#m::now()
@@ -390,19 +398,21 @@ pub fn codegen(
) -> Result<SpawnHandle, #ty> {
unsafe {
let input = #tupled;
- if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.dequeue()) {
+ if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.get_mut_unchecked().dequeue()) {
#app_path::#inputs
+ .get_mut_unchecked()
.get_unchecked_mut(usize::from(index))
.as_mut_ptr()
.write(input);
#app_path::#instants
+ .get_mut_unchecked()
.get_unchecked_mut(usize::from(index))
.as_mut_ptr()
.write(instant);
rtic::export::interrupt::free(|_| {
- let marker = #tq_marker;
+ let marker = *#tq_marker.get_mut_unchecked();
let nr = rtic::export::NotReady {
instant,
index,
@@ -410,15 +420,15 @@ pub fn codegen(
marker,
};
- #tq_marker = #tq_marker.wrapping_add(1);
+ *#tq_marker.get_mut_unchecked() = #tq_marker.get_mut_unchecked().wrapping_add(1);
- let tq = unsafe { &mut *#app_path::#tq.as_mut_ptr() };
+ let tq = &mut *#app_path::#tq.get_mut_unchecked().as_mut_ptr();
tq.enqueue_unchecked(
nr,
|| #enable_interrupt,
|| #pend,
- #app_path::#m_ident.as_mut());
+ #app_path::#m_ident.get_mut_unchecked().as_mut());
Ok(SpawnHandle { marker })
})
diff --git a/macros/src/codegen/post_init.rs b/macros/src/codegen/post_init.rs
index 96c5df80..7dd15903 100644
--- a/macros/src/codegen/post_init.rs
+++ b/macros/src/codegen/post_init.rs
@@ -17,16 +17,23 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
// If it's live
let cfgs = app.late_resources[name].cfgs.clone();
if analysis.locations.get(name).is_some() {
- // Need to also include the cfgs
stmts.push(quote!(
- #(#cfgs)*
- #mangled_name.as_mut_ptr().write(late.#name);
+ // We include the cfgs
+ #(#cfgs)*
+ // Late resource is a RacyCell<MaybeUninit<T>>
+ // - `get_mut_unchecked` to obtain `MaybeUninit<T>`
+ // - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>`
+ // - `write` the defined value for the late resource T
+ #mangled_name.get_mut_unchecked().as_mut_ptr().write(late.#name);
));
}
}
}
for (i, (monotonic, _)) in app.monotonics.iter().enumerate() {
+ // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
+ // stmts.push(quote!(#[doc = #doc]));
+
let idx = Index {
index: i as u32,
span: Span::call_site(),
@@ -36,7 +43,7 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
// Store the monotonic
let name = util::monotonic_ident(&monotonic.to_string());
let name = util::mark_internal_ident(&name);
- stmts.push(quote!(#name = Some(monotonics.#idx);));
+ stmts.push(quote!(*#name.get_mut_unchecked() = Some(monotonics.#idx);));
}
// Enable the interrupts -- this completes the `init`-ialization phase
diff --git a/macros/src/codegen/pre_init.rs b/macros/src/codegen/pre_init.rs
index 287f41a4..3a62da50 100644
--- a/macros/src/codegen/pre_init.rs
+++ b/macros/src/codegen/pre_init.rs
@@ -20,7 +20,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
let fq_ident = util::mark_internal_ident(&fq_ident);
stmts.push(quote!(
- (0..#cap).for_each(|i| #fq_ident.enqueue_unchecked(i));
+ (0..#cap).for_each(|i| #fq_ident.get_mut_unchecked().enqueue_unchecked(i));
));
}
@@ -86,7 +86,9 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
let tq = util::mark_internal_ident(&tq);
// Initialize timer queues
- stmts.push(quote!(#tq.as_mut_ptr().write(rtic::export::TimerQueue::new());));
+ stmts.push(
+ quote!(#tq.get_mut_unchecked().as_mut_ptr().write(rtic::export::TimerQueue::new());),
+ );
// Compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
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<T>`
+/// Late resource are stored in `RacyCell<MaybeUninit<T>>`
+///
+/// Safety:
+/// - RacyCell<T> access is `unsafe`.
+/// - RacyCell<MaybeUninit> 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<TokenStream2>,
// 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<core::mem::MaybeUninit<#ty>>),
+ 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,
diff --git a/macros/src/codegen/resources_struct.rs b/macros/src/codegen/resources_struct.rs
index 8ed8a291..6fe4678a 100644
--- a/macros/src/codegen/resources_struct.rs
+++ b/macros/src/codegen/resources_struct.rs
@@ -79,9 +79,9 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
let is_late = expr.is_none();
if is_late {
let expr = if access.is_exclusive() {
- quote!(&mut *#mangled_name.as_mut_ptr())
+ quote!(&mut *#mangled_name.get_mut_unchecked().as_mut_ptr())
} else {
- quote!(&*#mangled_name.as_ptr())
+ quote!(&*#mangled_name.get_unchecked().as_ptr())
};
values.push(quote!(
@@ -91,7 +91,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
} else {
values.push(quote!(
#(#cfgs)*
- #name: &#mut_ #mangled_name
+ #name: #mangled_name.get_mut_unchecked()
));
}
}
diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs
index a39fe4cc..e42fb88d 100644
--- a/macros/src/codegen/software_tasks.rs
+++ b/macros/src/codegen/software_tasks.rs
@@ -52,7 +52,7 @@ pub fn codegen(
// /// Queue version of a free-list that keeps track of empty slots in
// /// the following buffers
#[doc(hidden)]
- static mut #fq: #fq_ty = #fq_expr;
+ static #fq: rtic::RacyCell<#fq_ty> = rtic::RacyCell::new(#fq_expr);
));
let elems = &(0..cap)
@@ -65,13 +65,15 @@ pub fn codegen(
let mono_type = &monotonic.ty;
let uninit = mk_uninit();
+ // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
mod_app.push(quote!(
#uninit
// /// Buffer that holds the instants associated to the inputs of a task
+ // #[doc = #doc]
#[doc(hidden)]
- static mut #instants:
- [core::mem::MaybeUninit<rtic::time::Instant<#mono_type>>; #cap_lit] =
- [#(#elems,)*];
+ static #instants:
+ rtic::RacyCell<[core::mem::MaybeUninit<rtic::time::Instant<#mono_type>>; #cap_lit]> =
+ rtic::RacyCell::new([#(#elems,)*]);
));
}
@@ -82,8 +84,8 @@ pub fn codegen(
#uninit
// /// Buffer that holds the inputs of a task
#[doc(hidden)]
- static mut #inputs_ident: [core::mem::MaybeUninit<#input_ty>; #cap_lit] =
- [#(#elems,)*];
+ static #inputs_ident: rtic::RacyCell<[core::mem::MaybeUninit<#input_ty>; #cap_lit]> =
+ rtic::RacyCell::new([#(#elems,)*]);
));
// `${task}Resources`
diff --git a/macros/src/codegen/timer_queue.rs b/macros/src/codegen/timer_queue.rs
index 0d2c51ea..ed89af66 100644
--- a/macros/src/codegen/timer_queue.rs
+++ b/macros/src/codegen/timer_queue.rs
@@ -15,7 +15,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
// #[doc = #doc]
#[doc(hidden)]
#[allow(non_camel_case_types)]
- static mut #tq_marker: u32 = 0;
+ static #tq_marker: rtic::RacyCell<u32> = rtic::RacyCell::new(0);
));
let t = util::schedule_t_ident();
@@ -71,9 +71,11 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
let tq_ty =
quote!(core::mem::MaybeUninit<rtic::export::TimerQueue<#mono_type, #t, #n>>);
+ // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
items.push(quote!(
#[doc(hidden)]
- static mut #tq: #tq_ty = core::mem::MaybeUninit::uninit();
+ static #tq: rtic::RacyCell<#tq_ty> =
+ rtic::RacyCell::new(core::mem::MaybeUninit::uninit());
));
let mono = util::monotonic_ident(&monotonic_name);
@@ -82,7 +84,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
items.push(quote!(
#[doc(hidden)]
- static mut #mono: Option<#mono_type> = None;
+ static #mono: rtic::RacyCell<Option<#mono_type>> = rtic::RacyCell::new(None);
));
}
@@ -113,7 +115,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
quote!(
#(#cfgs)*
#t::#name => {
- rtic::export::interrupt::free(|_| #rq.split().0.enqueue_unchecked((#rqt::#name, index)));
+ rtic::export::interrupt::free(|_| #rq.get_mut_unchecked().split().0.enqueue_unchecked((#rqt::#name, index)));
#pend
}
@@ -132,10 +134,9 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
#[no_mangle]
#[allow(non_snake_case)]
unsafe fn #bound_interrupt() {
-
while let Some((task, index)) = rtic::export::interrupt::free(|_|
- if let Some(mono) = #app_path::#m_ident.as_mut() {
- (&mut *#tq.as_mut_ptr()).dequeue(|| #disable_isr, mono)
+ if let Some(mono) = #app_path::#m_ident.get_mut_unchecked().as_mut() {
+ (&mut *#tq.get_mut_unchecked().as_mut_ptr()).dequeue(|| #disable_isr, mono)
} else {
// We can only use the timer queue if `init` has returned, and it
// writes the `Some(monotonic)` we are accessing here.
@@ -147,7 +148,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
}
}
- rtic::export::interrupt::free(|_| if let Some(mono) = #app_path::#m_ident.as_mut() {
+ rtic::export::interrupt::free(|_| if let Some(mono) = #app_path::#m_ident.get_mut_unchecked().as_mut() {
mono.on_interrupt();
});
}