aboutsummaryrefslogtreecommitdiff
path: root/macros/src/analyze.rs
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2017-07-14 18:54:54 -0500
committerGravatar Jorge Aparicio <jorge@japaric.io> 2017-07-14 18:57:02 -0500
commit98596554b3d88a7619bdbc3ac7462a95b7263e96 (patch)
treeb04a8e1e6011f741e045044389e6189d49abf78a /macros/src/analyze.rs
parent59afbf02aa06d976dfd22df4cb87fadf6027a0fb (diff)
downloadrtic-98596554b3d88a7619bdbc3ac7462a95b7263e96.tar.gz
rtic-98596554b3d88a7619bdbc3ac7462a95b7263e96.tar.zst
rtic-98596554b3d88a7619bdbc3ac7462a95b7263e96.zip
split macro parser into its own crate and improve error handling / reporting
Diffstat (limited to 'macros/src/analyze.rs')
-rw-r--r--macros/src/analyze.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/macros/src/analyze.rs b/macros/src/analyze.rs
new file mode 100644
index 00000000..0fc125da
--- /dev/null
+++ b/macros/src/analyze.rs
@@ -0,0 +1,70 @@
+use std::cmp;
+use std::collections::HashMap;
+
+use syn::Ident;
+
+use check::App;
+
+pub type Ownerships = HashMap<Ident, Ownership>;
+
+pub enum Ownership {
+ /// Owned or co-owned by tasks that run at the same priority
+ Owned { priority: u8 },
+ /// Shared by tasks that run at different priorities.
+ ///
+ /// `ceiling` is the maximum value across all the task priorities
+ Shared { ceiling: u8 },
+}
+
+impl Ownership {
+ pub fn is_owned(&self) -> bool {
+ match *self {
+ Ownership::Owned { .. } => true,
+ _ => false,
+ }
+ }
+}
+
+pub fn app(app: &App) -> Ownerships {
+ let mut ownerships = HashMap::new();
+
+ for resource in &app.idle.resources {
+ ownerships.insert(resource.clone(), Ownership::Owned { priority: 0 });
+ }
+
+ for task in app.tasks.values() {
+ for resource in &task.resources {
+ if let Some(ownership) = ownerships.get_mut(resource) {
+ match *ownership {
+ Ownership::Owned { priority } => {
+ if priority == task.priority {
+ *ownership = Ownership::Owned { priority };
+ } else {
+ *ownership = Ownership::Shared {
+ ceiling: cmp::max(priority, task.priority),
+ };
+ }
+ }
+ Ownership::Shared { ceiling } => {
+ if task.priority > ceiling {
+ *ownership = Ownership::Shared {
+ ceiling: task.priority,
+ };
+ }
+ }
+ }
+
+ continue;
+ }
+
+ ownerships.insert(
+ resource.clone(),
+ Ownership::Owned {
+ priority: task.priority,
+ },
+ );
+ }
+ }
+
+ ownerships
+}