vsched_scheduler 0.2.1

Various scheduler algorithms in a unified interface
Documentation
use crate::BaseScheduler;
use core::fmt::Debug;
use core::ops::Deref;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicIsize, Ordering};
use utils::LockFreeDeque;
#[cfg(feature = "alloc")]
use {alloc::sync::Arc, core::mem::ManuallyDrop};

/// A task wrapper for the [`RRScheduler`].
///
/// It add a time slice counter to use in round-robin scheduling.
#[repr(C)]
pub struct RRTask<T, const MAX_TIME_SLICE: usize> {
    time_slice: AtomicIsize,
    inner: T,
}

impl<T, const S: usize> RRTask<T, S> {
    /// Creates a new [`RRTask`] from the inner task struct.
    pub const fn new(inner: T) -> Self {
        Self {
            inner,
            time_slice: AtomicIsize::new(S as isize),
        }
    }

    fn time_slice(&self) -> isize {
        self.time_slice.load(Ordering::Acquire)
    }

    fn reset_time_slice(&self) {
        self.time_slice.store(S as isize, Ordering::Release);
    }

    /// Returns a reference to the inner task struct.
    pub const fn inner(&self) -> &T {
        &self.inner
    }
}

impl<T, const S: usize> Deref for RRTask<T, S> {
    type Target = T;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

#[repr(transparent)]
pub struct RRTaskRef<T, const S: usize> {
    inner: NonNull<RRTask<T, S>>,
}

impl<T, const S: usize> Clone for RRTaskRef<T, S> {
    fn clone(&self) -> Self {
        Self::new(self.inner.as_ptr())
    }
}

unsafe impl<T, const S: usize> Send for RRTaskRef<T, S> {}
unsafe impl<T, const S: usize> Sync for RRTaskRef<T, S> {}

impl<T, const S: usize> RRTaskRef<T, S> {
    pub fn new(inner: *const RRTask<T, S>) -> Self {
        Self {
            inner: NonNull::new(inner as _).unwrap(),
        }
    }

    pub fn ptr_eq(&self, other: &Self) -> bool {
        self.inner.as_ptr() == other.inner.as_ptr()
    }

    pub fn is_empty(&self) -> bool {
        self.inner == NonNull::dangling()
    }

    #[cfg(feature = "alloc")]
    pub fn into_arc(&self) -> ManuallyDrop<Arc<RRTask<T, S>>> {
        unsafe { ManuallyDrop::new(Arc::from_raw(self.inner.as_ptr() as _)) }
    }
}

impl<T, const S: usize> Deref for RRTaskRef<T, S> {
    type Target = RRTask<T, S>;
    fn deref(&self) -> &Self::Target {
        unsafe { self.inner.as_ref() }
    }
}

impl<T: Debug, const S: usize> Debug for RRTask<T, S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RRTask")
            .field("inner", self.inner())
            .finish()
    }
}

impl<T: Debug, const S: usize> Debug for RRTaskRef<T, S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RRTaskRef")
            .field("RRTask", unsafe { self.inner.as_ref() })
            .finish()
    }
}

/// A simple [Round-Robin] (RR) preemptive scheduler.
///
/// It's very similar to the [`FifoScheduler`], but every task has a time slice
/// counter that is decremented each time a timer tick occurs. When the current
/// task's time slice counter reaches zero, the task is preempted and needs to
/// be rescheduled.
///
/// Unlike [`FifoScheduler`], it uses [`VecDeque`] as the ready queue. So it may
/// take O(n) time to remove a task from the ready queue.
///
/// [Round-Robin]: https://en.wikipedia.org/wiki/Round-robin_scheduling
/// [`FifoScheduler`]: crate::FifoScheduler
pub struct RRScheduler<T, const MAX_TIME_SLICE: usize, const CAPACITY: usize> {
    ready_queue: LockFreeDeque<RRTaskRef<T, MAX_TIME_SLICE>, CAPACITY>,
}

impl<T, const S: usize, const CAPACITY: usize> RRScheduler<T, S, CAPACITY> {
    /// Creates a new empty [`RRScheduler`].
    pub const fn new() -> Self {
        Self {
            ready_queue: LockFreeDeque::new(),
        }
    }
    /// get the name of scheduler
    pub fn scheduler_name() -> &'static str {
        "Round-robin"
    }
}

impl<T, const S: usize, const CAPACITY: usize> BaseScheduler for RRScheduler<T, S, CAPACITY> {
    type SchedItem = RRTaskRef<T, S>;

    fn init(&mut self) {}

    fn add_task(&self, task: Self::SchedItem) {
        let _ = self.ready_queue.push_back(task);
    }

    fn pick_next_task(&self) -> Option<Self::SchedItem> {
        self.ready_queue.pop_front()
    }

    fn put_prev_task(&self, prev: Self::SchedItem, preempt: bool) {
        if prev.time_slice() > 0 && preempt {
            let _ = self.ready_queue.push_front(prev);
        } else {
            prev.reset_time_slice();
            let _ = self.ready_queue.push_back(prev);
        }
    }

    fn task_tick(&self, current: &Self::SchedItem) -> bool {
        let old_slice = current.time_slice.fetch_sub(1, Ordering::Release);
        old_slice <= 1
    }

    fn set_priority(&self, _task: &Self::SchedItem, _prio: isize) -> bool {
        false
    }
}