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};
#[repr(C)]
pub struct RRTask<T, const MAX_TIME_SLICE: usize> {
time_slice: AtomicIsize,
inner: T,
}
impl<T, const S: usize> RRTask<T, S> {
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);
}
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()
}
}
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> {
pub const fn new() -> Self {
Self {
ready_queue: LockFreeDeque::new(),
}
}
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
}
}