diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-09-11 16:15:27 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-09-11 16:33:48 +0200 |
| commit | 6ec9bcb1c4dfbe5fc5365d93e75c516bb03bf9fc (patch) | |
| tree | 9094e4fb8745849ada00f61144aba60d8a51b5bf /embassy-executor/src/metadata.rs | |
| parent | 42c68622eeba3be05e8f8ccdc4072b7aa57f78d1 (diff) | |
executor: add priority scheduler.
Diffstat (limited to 'embassy-executor/src/metadata.rs')
| -rw-r--r-- | embassy-executor/src/metadata.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/embassy-executor/src/metadata.rs b/embassy-executor/src/metadata.rs index 4220048a6..bc0df0f83 100644 --- a/embassy-executor/src/metadata.rs +++ b/embassy-executor/src/metadata.rs | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | #[cfg(feature = "metadata-name")] | 1 | #[cfg(feature = "metadata-name")] |
| 2 | use core::cell::Cell; | 2 | use core::cell::Cell; |
| 3 | use core::future::{poll_fn, Future}; | 3 | use core::future::{poll_fn, Future}; |
| 4 | #[cfg(feature = "scheduler-priority")] | ||
| 5 | use core::sync::atomic::{AtomicU8, Ordering}; | ||
| 4 | use core::task::Poll; | 6 | use core::task::Poll; |
| 5 | 7 | ||
| 6 | #[cfg(feature = "metadata-name")] | 8 | #[cfg(feature = "metadata-name")] |
| @@ -14,6 +16,8 @@ use crate::raw::Deadline; | |||
| 14 | pub struct Metadata { | 16 | pub struct Metadata { |
| 15 | #[cfg(feature = "metadata-name")] | 17 | #[cfg(feature = "metadata-name")] |
| 16 | name: Mutex<Cell<Option<&'static str>>>, | 18 | name: Mutex<Cell<Option<&'static str>>>, |
| 19 | #[cfg(feature = "scheduler-priority")] | ||
| 20 | priority: AtomicU8, | ||
| 17 | #[cfg(feature = "scheduler-deadline")] | 21 | #[cfg(feature = "scheduler-deadline")] |
| 18 | deadline: raw::Deadline, | 22 | deadline: raw::Deadline, |
| 19 | } | 23 | } |
| @@ -23,6 +27,8 @@ impl Metadata { | |||
| 23 | Self { | 27 | Self { |
| 24 | #[cfg(feature = "metadata-name")] | 28 | #[cfg(feature = "metadata-name")] |
| 25 | name: Mutex::new(Cell::new(None)), | 29 | name: Mutex::new(Cell::new(None)), |
| 30 | #[cfg(feature = "scheduler-priority")] | ||
| 31 | priority: AtomicU8::new(0), | ||
| 26 | // NOTE: The deadline is set to zero to allow the initializer to reside in `.bss`. This | 32 | // NOTE: The deadline is set to zero to allow the initializer to reside in `.bss`. This |
| 27 | // will be lazily initalized in `initialize_impl` | 33 | // will be lazily initalized in `initialize_impl` |
| 28 | #[cfg(feature = "scheduler-deadline")] | 34 | #[cfg(feature = "scheduler-deadline")] |
| @@ -33,6 +39,14 @@ impl Metadata { | |||
| 33 | pub(crate) fn reset(&self) { | 39 | pub(crate) fn reset(&self) { |
| 34 | #[cfg(feature = "metadata-name")] | 40 | #[cfg(feature = "metadata-name")] |
| 35 | critical_section::with(|cs| self.name.borrow(cs).set(None)); | 41 | critical_section::with(|cs| self.name.borrow(cs).set(None)); |
| 42 | |||
| 43 | #[cfg(feature = "scheduler-priority")] | ||
| 44 | self.set_priority(0); | ||
| 45 | |||
| 46 | // By default, deadlines are set to the maximum value, so that any task WITH | ||
| 47 | // a set deadline will ALWAYS be scheduled BEFORE a task WITHOUT a set deadline | ||
| 48 | #[cfg(feature = "scheduler-deadline")] | ||
| 49 | self.unset_deadline(); | ||
| 36 | } | 50 | } |
| 37 | 51 | ||
| 38 | /// Get the metadata for the current task. | 52 | /// Get the metadata for the current task. |
| @@ -61,6 +75,18 @@ impl Metadata { | |||
| 61 | critical_section::with(|cs| self.name.borrow(cs).set(Some(name))) | 75 | critical_section::with(|cs| self.name.borrow(cs).set(Some(name))) |
| 62 | } | 76 | } |
| 63 | 77 | ||
| 78 | /// Get this task's priority. | ||
| 79 | #[cfg(feature = "scheduler-priority")] | ||
| 80 | pub fn priority(&self) -> u8 { | ||
| 81 | self.priority.load(Ordering::Relaxed) | ||
| 82 | } | ||
| 83 | |||
| 84 | /// Set this task's priority. | ||
| 85 | #[cfg(feature = "scheduler-priority")] | ||
| 86 | pub fn set_priority(&self, priority: u8) { | ||
| 87 | self.priority.store(priority, Ordering::Relaxed) | ||
| 88 | } | ||
| 89 | |||
| 64 | /// Get this task's deadline. | 90 | /// Get this task's deadline. |
| 65 | #[cfg(feature = "scheduler-deadline")] | 91 | #[cfg(feature = "scheduler-deadline")] |
| 66 | pub fn deadline(&self) -> u64 { | 92 | pub fn deadline(&self) -> u64 { |
