diff options
Diffstat (limited to 'embassy-executor/src/metadata.rs')
| -rw-r--r-- | embassy-executor/src/metadata.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/embassy-executor/src/metadata.rs b/embassy-executor/src/metadata.rs new file mode 100644 index 000000000..f92c9b37c --- /dev/null +++ b/embassy-executor/src/metadata.rs | |||
| @@ -0,0 +1,55 @@ | |||
| 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 | } | ||
