hazard3-executor 0.1.2

async/await executor designed for embedded usage
Documentation
#[cfg(feature = "metadata-name")]
use core::cell::Cell;
use core::future::{Future, poll_fn};
#[cfg(feature = "scheduler-priority")]
use core::sync::atomic::{AtomicU8, Ordering};
use core::task::Poll;

#[cfg(feature = "metadata-name")]
use critical_section::Mutex;

#[cfg(feature = "scheduler-deadline")]
use crate::raw::Deadline;
use crate::raw::{self, TaskRef};

/// A reference to metadata of a particular task
pub struct MetadataRef {
    #[allow(dead_code, reason = "only used when metadata is active")]
    task: TaskRef,
}

impl MetadataRef {
    /// Get a new reference to metadata of a task
    pub const fn new(task: TaskRef) -> Self {
        Self { task }
    }

    /// 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 = Self> {
        poll_fn(|cx| Poll::Ready(Self::new(raw::task_from_waker(cx.waker()))))
    }

    #[allow(dead_code, reason = "only used when metadata is active")]
    fn metadata(&self) -> &Metadata {
        &self.task.header().metadata
    }

    /// Get this task's name
    ///
    /// Tasks declared with `#[task]` default to their function name, or to the macro's
    /// `name = "..."` argument. [`set_name`](Self::set_name) replaces the default.
    ///
    /// `None` only for tasks spawned without the macros.
    ///
    /// NOTE: this takes a critical section.
    #[cfg(feature = "metadata-name")]
    pub fn name(&self) -> Option<&'static str> {
        self.metadata().name()
    }

    /// Set this task's name, replacing the default from `#[task]` or `#[main]`.
    ///
    /// NOTE: this takes a critical section.
    #[cfg(feature = "metadata-name")]
    pub fn set_name(&self, name: &'static str) {
        self.metadata().set_name(name);
        #[cfg(feature = "trace")]
        crate::raw::trace::TraceImpl::task_name_set(self.task, name);
    }

    /// Get this task's priority.
    #[cfg(feature = "scheduler-priority")]
    pub fn priority(&self) -> u8 {
        self.metadata().priority()
    }

    /// Set this task's priority.
    #[cfg(feature = "scheduler-priority")]
    pub fn set_priority(&self, priority: u8) {
        self.metadata().set_priority(priority);
        #[cfg(feature = "trace")]
        crate::raw::trace::TraceImpl::task_priority_set(self.task, priority);
    }

    /// Get this task's deadline.
    #[cfg(feature = "scheduler-deadline")]
    pub fn deadline(&self) -> u64 {
        self.metadata().deadline()
    }

    /// Set this task's deadline.
    ///
    /// This method does NOT check whether the deadline has already passed.
    #[cfg(feature = "scheduler-deadline")]
    pub fn set_deadline(&self, instant_ticks: u64) {
        self.metadata().set_deadline(instant_ticks);
        #[cfg(feature = "trace")]
        crate::raw::trace::TraceImpl::task_deadline_set(self.task, instant_ticks);
    }

    /// Remove this task's deadline.
    /// This brings it back to the default where it's not scheduled ahead of other tasks.
    #[cfg(feature = "scheduler-deadline")]
    pub fn unset_deadline(&self) {
        self.set_deadline(Deadline::UNSET_TICKS);
    }

    /// Set this task's deadline `duration_ticks` in the future from when
    /// this future is polled. This deadline is saturated to the max tick value.
    ///
    /// Analogous to `Timer::after`.
    #[cfg(all(feature = "scheduler-deadline", feature = "embassy-time-driver"))]
    pub fn set_deadline_after(&self, duration_ticks: u64) {
        let now = embassy_time_driver::now();

        // Since ticks is a u64, saturating add is PROBABLY overly cautious, leave
        // it for now, we can probably make this wrapping_add for performance
        // reasons later.
        let deadline = now.saturating_add(duration_ticks);

        self.set_deadline(deadline);
    }

    /// Set the this task's deadline `increment_ticks` from the previous deadline.
    ///
    /// This deadline is saturated to the max tick value.
    ///
    /// Note that by default (unless otherwise set), tasks start life with the deadline
    /// not set, which means this method will have no effect.
    ///
    /// Analogous to one increment of `Ticker::every().next()`.
    ///
    /// Returns the deadline that was set.
    #[cfg(feature = "scheduler-deadline")]
    pub fn increment_deadline(&self, duration_ticks: u64) {
        let last = self.deadline();

        // Since ticks is a u64, saturating add is PROBABLY overly cautious, leave
        // it for now, we can probably make this wrapping_add for performance
        // reasons later.
        let deadline = last.saturating_add(duration_ticks);

        self.set_deadline(deadline);
    }
}

/// Metadata associated with a task.
pub(crate) struct Metadata {
    /// The macro default or the name given to `set_name`, see [`MetadataRef::name`].
    #[cfg(feature = "metadata-name")]
    name: Mutex<Cell<Option<&'static str>>>,
    #[cfg(feature = "scheduler-priority")]
    priority: AtomicU8,
    #[cfg(feature = "scheduler-deadline")]
    deadline: raw::Deadline,
}

impl Metadata {
    pub(crate) const fn new() -> Self {
        Self {
            #[cfg(feature = "metadata-name")]
            name: Mutex::new(Cell::new(None)),
            #[cfg(feature = "scheduler-priority")]
            priority: AtomicU8::new(0),
            // NOTE: The deadline is set to zero to allow the initializer to reside in `.bss`. This
            // will be lazily initialized in `initialize_impl`
            #[cfg(feature = "scheduler-deadline")]
            deadline: raw::Deadline::new_unset(),
        }
    }

    /// Initialize the metadata for a freshly spawned task: `name` is its default name
    /// (see [`MetadataRef::name`]), priority and deadline start unset.
    pub(crate) fn init(&self, name: Option<&'static str>) {
        #[cfg(feature = "metadata-name")]
        critical_section::with(|cs| self.name.borrow(cs).set(name));
        #[cfg(not(feature = "metadata-name"))]
        let _ = name;

        #[cfg(feature = "scheduler-priority")]
        self.set_priority(0);

        // By default, deadlines are set to the maximum value, so that any task WITH
        // a set deadline will ALWAYS be scheduled BEFORE a task WITHOUT a set deadline
        #[cfg(feature = "scheduler-deadline")]
        self.set_deadline(Deadline::UNSET_TICKS);
    }

    /// Get this task's name
    ///
    /// NOTE: this takes a critical section.
    #[cfg(feature = "metadata-name")]
    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")]
    fn set_name(&self, name: &'static str) {
        critical_section::with(|cs| self.name.borrow(cs).set(Some(name)))
    }

    /// Get this task's priority.
    #[cfg(feature = "scheduler-priority")]
    pub fn priority(&self) -> u8 {
        self.priority.load(Ordering::Relaxed)
    }

    /// Set this task's priority.
    #[cfg(feature = "scheduler-priority")]
    fn set_priority(&self, priority: u8) {
        self.priority.store(priority, Ordering::Relaxed)
    }

    /// Get this task's deadline.
    #[cfg(feature = "scheduler-deadline")]
    pub fn deadline(&self) -> u64 {
        self.deadline.instant_ticks()
    }

    /// Set this task's deadline.
    ///
    /// This method does NOT check whether the deadline has already passed.
    #[cfg(feature = "scheduler-deadline")]
    fn set_deadline(&self, instant_ticks: u64) {
        self.deadline.set(instant_ticks);
    }
}

/// A unique ID for a task
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TaskId(pub(crate) usize);
impl TaskId {
    /// Get the id as a raw number
    pub fn get(&self) -> usize {
        self.0
    }
}

/// A unique ID for an executor
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ExecutorId(pub(crate) usize);
impl ExecutorId {
    /// Get the id as a raw number
    pub fn get(&self) -> usize {
        self.0
    }
}