use core::sync::atomic::{AtomicU32, Ordering};
use crate::task::Task;
pub struct TaskScheduleInfo {
pub cpu: AtomicCpuId,
}
pub struct AtomicCpuId(AtomicU32);
impl AtomicCpuId {
const NONE: u32 = u32::MAX;
fn new(cpu_id: u32) -> Self {
Self(AtomicU32::new(cpu_id))
}
pub fn set_if_is_none(&self, cpu_id: u32) -> core::result::Result<u32, u32> {
self.0
.compare_exchange(Self::NONE, cpu_id, Ordering::Relaxed, Ordering::Relaxed)
}
pub fn set_to_none(&self) {
self.0.store(Self::NONE, Ordering::Relaxed);
}
pub fn get(&self) -> Option<u32> {
let val = self.0.load(Ordering::Relaxed);
if val == Self::NONE {
None
} else {
Some(val)
}
}
}
impl Default for AtomicCpuId {
fn default() -> Self {
Self::new(Self::NONE)
}
}
impl CommonSchedInfo for Task {
fn cpu(&self) -> &AtomicCpuId {
&self.schedule_info().cpu
}
}
pub trait CommonSchedInfo {
fn cpu(&self) -> &AtomicCpuId;
}