diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-07-09 01:18:04 +0200 |
|---|---|---|
| committer | diondokter <[email protected]> | 2025-08-29 13:22:59 +0200 |
| commit | da9cdf0c536ec4fa7bdfb649750c44f70ef1cd55 (patch) | |
| tree | a080b8663037d8d4af8fc3998360faa80c45fb02 /embassy-executor/src/metadata.rs | |
| parent | 2ba34ce2178d576f339f0b0dac70ac125f81cc5b (diff) | |
executor: add "task metadata" concept, make name a task metadata.
Diffstat (limited to 'embassy-executor/src/metadata.rs')
| -rw-r--r-- | embassy-executor/src/metadata.rs | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/embassy-executor/src/metadata.rs b/embassy-executor/src/metadata.rs index 957417f6b..f92c9b37c 100644 --- a/embassy-executor/src/metadata.rs +++ b/embassy-executor/src/metadata.rs | |||
| @@ -1 +1,55 @@ | |||
| 1 | pub struct Metadata {} | 1 | #[cfg(feature = "metadata-name")] |
| 2 | use core::cell::Cell; | ||
| 3 | use core::future::{poll_fn, Future}; | ||
| 4 | use core::task::Poll; | ||
| 5 | |||
| 6 | #[cfg(feature = "metadata-name")] | ||
| 7 | use critical_section::Mutex; | ||
| 8 | |||
| 9 | use crate::raw; | ||
| 10 | |||
| 11 | /// Metadata associated with a task. | ||
| 12 | pub struct Metadata { | ||
| 13 | #[cfg(feature = "metadata-name")] | ||
| 14 | name: Mutex<Cell<Option<&'static str>>>, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl Metadata { | ||
| 18 | pub(crate) const fn new() -> Self { | ||
| 19 | Self { | ||
| 20 | #[cfg(feature = "metadata-name")] | ||
| 21 | name: Mutex::new(Cell::new(None)), | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | pub(crate) fn reset(&self) { | ||
| 26 | #[cfg(feature = "metadata-name")] | ||
| 27 | critical_section::with(|cs| self.name.borrow(cs).set(None)); | ||
| 28 | } | ||
| 29 | |||
| 30 | /// Get the metadata for the current task. | ||
| 31 | /// | ||
| 32 | /// You can use this to read or modify the current task's metadata. | ||
| 33 | /// | ||
| 34 | /// This function is `async` just to get access to the current async | ||
| 35 | /// context. It returns instantly, it does not block/yield. | ||
| 36 | pub fn for_current_task() -> impl Future<Output = &'static Self> { | ||
| 37 | poll_fn(|cx| Poll::Ready(raw::task_from_waker(cx.waker()).metadata())) | ||
| 38 | } | ||
| 39 | |||
| 40 | /// Get this task's name | ||
| 41 | /// | ||
| 42 | /// NOTE: this takes a critical section. | ||
| 43 | #[cfg(feature = "metadata-name")] | ||
| 44 | pub fn name(&self) -> Option<&'static str> { | ||
| 45 | critical_section::with(|cs| self.name.borrow(cs).get()) | ||
| 46 | } | ||
| 47 | |||
| 48 | /// Set this task's name | ||
| 49 | /// | ||
| 50 | /// NOTE: this takes a critical section. | ||
| 51 | #[cfg(feature = "metadata-name")] | ||
| 52 | pub fn set_name(&self, name: &'static str) { | ||
| 53 | critical_section::with(|cs| self.name.borrow(cs).set(Some(name))) | ||
| 54 | } | ||
| 55 | } | ||
