aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen.rs
diff options
context:
space:
mode:
authorGravatar bors[bot] <bors[bot]@users.noreply.github.com> 2018-11-04 18:58:45 +0000
committerGravatar bors[bot] <bors[bot]@users.noreply.github.com> 2018-11-04 18:58:45 +0000
commit8a27efeaeb5483dc12370add4b3786bdd20a4973 (patch)
tree00e10c34cc22f80aee45c51d945e31c237c74eeb /macros/src/codegen.rs
parent16d473a9b6827aa7ffa9ce92e4e532eff9a091d2 (diff)
parenta2792182952c953aab2c442bb02ac1f2e4986e67 (diff)
downloadrtic-8a27efeaeb5483dc12370add4b3786bdd20a4973.tar.gz
rtic-8a27efeaeb5483dc12370add4b3786bdd20a4973.tar.zst
rtic-8a27efeaeb5483dc12370add4b3786bdd20a4973.zip
Merge #99
99: impl Mutex on all shared resources r=japaric a=japaric document how to write generic code that operates on resources Co-authored-by: Jorge Aparicio <jorge@japaric.io>
Diffstat (limited to 'macros/src/codegen.rs')
-rw-r--r--macros/src/codegen.rs76
1 files changed, 57 insertions, 19 deletions
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs
index ff1062ae..416b0070 100644
--- a/macros/src/codegen.rs
+++ b/macros/src/codegen.rs
@@ -676,6 +676,7 @@ fn prelude(
}
} else {
let ownership = &analysis.ownerships[name];
+ let mut exclusive = false;
if ownership.needs_lock(logical_prio) {
may_call_lock = true;
@@ -710,28 +711,61 @@ fn prelude(
exprs.push(quote!(#name: <#name as owned_singleton::Singleton>::new()));
} else {
needs_unsafe = true;
- defs.push(quote!(pub #name: &'a mut #name));
- exprs.push(
- quote!(#name: &mut <#name as owned_singleton::Singleton>::new()),
- );
+ if ownership.is_owned() || mut_.is_none() {
+ defs.push(quote!(pub #name: &'a #mut_ #name));
+ let alias = mk_ident();
+ items.push(quote!(
+ let #mut_ #alias = unsafe {
+ <#name as owned_singleton::Singleton>::new()
+ };
+ ));
+ exprs.push(quote!(#name: &#mut_ #alias));
+ } else {
+ may_call_lock = true;
+ defs.push(quote!(pub #name: rtfm::Exclusive<'a, #name>));
+ let alias = mk_ident();
+ items.push(quote!(
+ let #mut_ #alias = unsafe {
+ <#name as owned_singleton::Singleton>::new()
+ };
+ ));
+ exprs.push(quote!(
+ #name: rtfm::Exclusive(&mut #alias)
+ ));
+ }
}
continue;
} else {
- defs.push(quote!(pub #name: &#lt #mut_ #ty));
+ if ownership.is_owned() || mut_.is_none() {
+ defs.push(quote!(pub #name: &#lt #mut_ #ty));
+ } else {
+ exclusive = true;
+ may_call_lock = true;
+ defs.push(quote!(pub #name: rtfm::Exclusive<#lt, #ty>));
+ }
}
}
let alias = &ctxt.statics[name];
needs_unsafe = true;
if initialized {
- exprs.push(quote!(#name: &#mut_ #alias));
+ if exclusive {
+ exprs.push(quote!(#name: rtfm::Exclusive(&mut #alias)));
+ } else {
+ exprs.push(quote!(#name: &#mut_ #alias));
+ }
} else {
let method = if mut_.is_some() {
quote!(get_mut)
} else {
quote!(get_ref)
};
- exprs.push(quote!(#name: #alias.#method() ));
+
+ if exclusive {
+ exprs.push(quote!(#name: rtfm::Exclusive(#alias.#method()) ));
+ } else {
+ exprs.push(quote!(#name: #alias.#method() ));
+ }
}
}
}
@@ -1655,19 +1689,23 @@ fn mk_resource(
};
items.push(quote!(
- unsafe impl<'a> rtfm::Mutex for #path<'a> {
- const CEILING: u8 = #ceiling;
- const NVIC_PRIO_BITS: u8 = #device::NVIC_PRIO_BITS;
- type Data = #ty;
+ impl<'a> rtfm::Mutex for #path<'a> {
+ type T = #ty;
- #[inline(always)]
- unsafe fn priority(&self) -> &core::cell::Cell<u8> {
- &self.#priority
- }
-
- #[inline(always)]
- fn ptr(&self) -> *mut Self::Data {
- unsafe { #ptr }
+ #[inline]
+ fn lock<R, F>(&mut self, f: F) -> R
+ where
+ F: FnOnce(&mut Self::T) -> R,
+ {
+ unsafe {
+ rtfm::export::claim(
+ #ptr,
+ &self.#priority,
+ #ceiling,
+ #device::NVIC_PRIO_BITS,
+ f,
+ )
+ }
}
}
));