blob: fd8095629acb6f413ccaccef04769ec3894d7f7c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#[cfg(feature = "metadata-name")]
use core::cell::Cell;
use core::future::{poll_fn, Future};
use core::task::Poll;
#[cfg(feature = "metadata-name")]
use critical_section::Mutex;
use crate::raw;
/// Metadata associated with a task.
pub struct Metadata {
#[cfg(feature = "metadata-name")]
name: Mutex<Cell<Option<&'static str>>>,
#[cfg(feature = "metadata-deadline")]
deadline: raw::Deadline,
}
impl Metadata {
pub(crate) const fn new() -> Self {
Self {
#[cfg(feature = "metadata-name")]
name: Mutex::new(Cell::new(None)),
// NOTE: The deadline is set to zero to allow the initializer to reside in `.bss`. This
// will be lazily initalized in `initialize_impl`
#[cfg(feature = "metadata-deadline")]
deadline: raw::Deadline::new_unset(),
}
}
pub(crate) fn reset(&self) {
#[cfg(feature = "metadata-name")]
critical_section::with(|cs| self.name.borrow(cs).set(None));
}
/// Get the metadata for the current task.
///
/// You can use this to read or modify the current task's metadata.
///
/// This function is `async` just to get access to the current async
/// context. It returns instantly, it does not block/yield.
pub fn for_current_task() -> impl Future<Output = &'static Self> {
poll_fn(|cx| Poll::Ready(raw::task_from_waker(cx.waker()).metadata()))
}
/// Get this task's name
///
/// NOTE: this takes a critical section.
#[cfg(feature = "metadata-name")]
pub fn name(&self) -> Option<&'static str> {
critical_section::with(|cs| self.name.borrow(cs).get())
}
/// Set this task's name
///
/// NOTE: this takes a critical section.
#[cfg(feature = "metadata-name")]
pub fn set_name(&self, name: &'static str) {
critical_section::with(|cs| self.name.borrow(cs).set(Some(name)))
}
/// Earliest Deadline First scheduler Deadline. This field should not be accessed
/// outside the context of the task itself as it being polled by the executor.
#[cfg(feature = "metadata-deadline")]
pub fn deadline(&self) -> &raw::Deadline {
&self.deadline
}
}
|