diff options
Diffstat (limited to 'embassy-executor/src/metadata.rs')
| -rw-r--r-- | embassy-executor/src/metadata.rs | 64 |
1 files changed, 59 insertions, 5 deletions
diff --git a/embassy-executor/src/metadata.rs b/embassy-executor/src/metadata.rs index fd8095629..81c5afafb 100644 --- a/embassy-executor/src/metadata.rs +++ b/embassy-executor/src/metadata.rs | |||
| @@ -7,12 +7,14 @@ use core::task::Poll; | |||
| 7 | use critical_section::Mutex; | 7 | use critical_section::Mutex; |
| 8 | 8 | ||
| 9 | use crate::raw; | 9 | use crate::raw; |
| 10 | #[cfg(feature = "scheduler-deadline")] | ||
| 11 | use crate::raw::Deadline; | ||
| 10 | 12 | ||
| 11 | /// Metadata associated with a task. | 13 | /// Metadata associated with a task. |
| 12 | pub struct Metadata { | 14 | pub struct Metadata { |
| 13 | #[cfg(feature = "metadata-name")] | 15 | #[cfg(feature = "metadata-name")] |
| 14 | name: Mutex<Cell<Option<&'static str>>>, | 16 | name: Mutex<Cell<Option<&'static str>>>, |
| 15 | #[cfg(feature = "metadata-deadline")] | 17 | #[cfg(feature = "scheduler-deadline")] |
| 16 | deadline: raw::Deadline, | 18 | deadline: raw::Deadline, |
| 17 | } | 19 | } |
| 18 | 20 | ||
| @@ -23,7 +25,7 @@ impl Metadata { | |||
| 23 | name: Mutex::new(Cell::new(None)), | 25 | name: Mutex::new(Cell::new(None)), |
| 24 | // NOTE: The deadline is set to zero to allow the initializer to reside in `.bss`. This | 26 | // NOTE: The deadline is set to zero to allow the initializer to reside in `.bss`. This |
| 25 | // will be lazily initalized in `initialize_impl` | 27 | // will be lazily initalized in `initialize_impl` |
| 26 | #[cfg(feature = "metadata-deadline")] | 28 | #[cfg(feature = "scheduler-deadline")] |
| 27 | deadline: raw::Deadline::new_unset(), | 29 | deadline: raw::Deadline::new_unset(), |
| 28 | } | 30 | } |
| 29 | } | 31 | } |
| @@ -59,10 +61,62 @@ impl Metadata { | |||
| 59 | critical_section::with(|cs| self.name.borrow(cs).set(Some(name))) | 61 | critical_section::with(|cs| self.name.borrow(cs).set(Some(name))) |
| 60 | } | 62 | } |
| 61 | 63 | ||
| 62 | /// Earliest Deadline First scheduler Deadline. This field should not be accessed | 64 | /// Get this task's deadline. |
| 63 | /// outside the context of the task itself as it being polled by the executor. | 65 | #[cfg(feature = "scheduler-deadline")] |
| 64 | #[cfg(feature = "metadata-deadline")] | ||
| 65 | pub fn deadline(&self) -> &raw::Deadline { | 66 | pub fn deadline(&self) -> &raw::Deadline { |
| 66 | &self.deadline | 67 | &self.deadline |
| 67 | } | 68 | } |
| 69 | |||
| 70 | /// Set this task's deadline. | ||
| 71 | /// | ||
| 72 | /// This method does NOT check whether the deadline has already passed. | ||
| 73 | #[cfg(feature = "scheduler-deadline")] | ||
| 74 | pub fn set_deadline(&self, instant_ticks: u64) { | ||
| 75 | self.deadline.set(instant_ticks); | ||
| 76 | } | ||
| 77 | |||
| 78 | /// Remove this task's deadline. | ||
| 79 | /// This brings it back to the defaul where it's not scheduled ahead of other tasks. | ||
| 80 | #[cfg(feature = "scheduler-deadline")] | ||
| 81 | pub fn unset_deadline(&self) { | ||
| 82 | self.deadline.set(Deadline::UNSET_DEADLINE_TICKS); | ||
| 83 | } | ||
| 84 | |||
| 85 | /// Set this task's deadline `duration_ticks` in the future from when | ||
| 86 | /// this future is polled. This deadline is saturated to the max tick value. | ||
| 87 | /// | ||
| 88 | /// Analogous to `Timer::after`. | ||
| 89 | #[cfg(all(feature = "scheduler-deadline", feature = "embassy-time-driver"))] | ||
| 90 | pub fn set_deadline_after(&self, duration_ticks: u64) { | ||
| 91 | let now = embassy_time_driver::now(); | ||
| 92 | |||
| 93 | // Since ticks is a u64, saturating add is PROBABLY overly cautious, leave | ||
| 94 | // it for now, we can probably make this wrapping_add for performance | ||
| 95 | // reasons later. | ||
| 96 | let deadline = now.saturating_add(duration_ticks); | ||
| 97 | |||
| 98 | self.set_deadline(deadline); | ||
| 99 | } | ||
| 100 | |||
| 101 | /// Set the this task's deadline `increment_ticks` from the previous deadline. | ||
| 102 | /// | ||
| 103 | /// This deadline is saturated to the max tick value. | ||
| 104 | /// | ||
| 105 | /// Note that by default (unless otherwise set), tasks start life with the deadline | ||
| 106 | /// not set, which means this method will have no effect. | ||
| 107 | /// | ||
| 108 | /// Analogous to one increment of `Ticker::every().next()`. | ||
| 109 | /// | ||
| 110 | /// Returns the deadline that was set. | ||
| 111 | #[cfg(feature = "scheduler-deadline")] | ||
| 112 | pub fn increment_deadline(&self, duration_ticks: u64) { | ||
| 113 | let last = self.deadline().instant_ticks(); | ||
| 114 | |||
| 115 | // Since ticks is a u64, saturating add is PROBABLY overly cautious, leave | ||
| 116 | // it for now, we can probably make this wrapping_add for performance | ||
| 117 | // reasons later. | ||
| 118 | let deadline = last.saturating_add(duration_ticks); | ||
| 119 | |||
| 120 | self.set_deadline(deadline); | ||
| 121 | } | ||
| 68 | } | 122 | } |
