aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/metadata.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-08-29 12:04:29 +0000
committerGitHub <[email protected]>2025-08-29 12:04:29 +0000
commitf86cf87f2f20f723e2ba2fe7d83908a2b3bac2d1 (patch)
tree57f9200ed729746ef5f077af6c79863c37e824ae /embassy-executor/src/metadata.rs
parentdf10e8a6bc407544d29c234ed00bdec3e9caf837 (diff)
parente2c34ac735888d25d57d3ea07e8915c2e112048c (diff)
Merge pull request #4606 from diondokter/taskmeta-update-2
Taskmeta update
Diffstat (limited to 'embassy-executor/src/metadata.rs')
-rw-r--r--embassy-executor/src/metadata.rs55
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")]
2use core::cell::Cell;
3use core::future::{poll_fn, Future};
4use core::task::Poll;
5
6#[cfg(feature = "metadata-name")]
7use critical_section::Mutex;
8
9use crate::raw;
10
11/// Metadata associated with a task.
12pub struct Metadata {
13 #[cfg(feature = "metadata-name")]
14 name: Mutex<Cell<Option<&'static str>>>,
15}
16
17impl 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}