use core::cell::UnsafeCell;
use core::ptr;
pub const MAX_HELD_LOCKS: usize = 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum TaskState {
Ready = 0,
Running = 1,
Dead = 2,
Blocked = 3,
}
pub const TCB_FLAG_USE_MSP: u8 = 1 << 0;
pub const TCB_FLAG_WOKEN: u8 = 1 << 1;
#[repr(C)]
pub struct TaskControlBlock {
pub sp: *mut u8,
pub stack_base: *mut u8,
pub stack_size: usize,
pub closure_block: *mut u8,
pub state: TaskState,
pub flags: u8,
pub next: *mut TaskControlBlock,
pub prev: *mut TaskControlBlock,
pub id: u32,
pub slice_cycles: u32,
pub slices_run: u32,
pub switches: u32,
pub backend: *mut u8,
pub blocked_on: u32,
pub block_deadline: u64,
pub held_locks: [u32; MAX_HELD_LOCKS],
pub held_count: u8,
}
pub const TCB_SP_OFFSET: usize = core::mem::offset_of!(TaskControlBlock, sp);
pub const TCB_FLAGS_OFFSET: usize = core::mem::offset_of!(TaskControlBlock, flags);
pub const TCB_STATE_OFFSET: usize = core::mem::offset_of!(TaskControlBlock, state);
const _: () = assert!(
TCB_SP_OFFSET == 0,
"TaskControlBlock::sp must stay at offset 0 (Cortex-M PendSV reads it with a fixed offset)"
);
impl TaskControlBlock {
#[inline]
pub fn is_linked(&self) -> bool {
!self.next.is_null()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IdlePolicy {
Wait,
Spin,
ExitWhenAllDead,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Measure {
Cycles,
Nanos,
Off,
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct KernelConfig {
pub stack_size: usize,
pub idle: IdlePolicy,
pub measure: Measure,
pub slice_cycles: u32,
pub timer_hz: u32,
pub active_cores: usize,
}
impl KernelConfig {
pub const fn default_const() -> Self {
KernelConfig {
stack_size: crate::thread::DEFAULT_STACK_SIZE,
idle: IdlePolicy::Wait,
measure: Measure::Off,
slice_cycles: 0,
timer_hz: 0,
active_cores: 1,
}
}
}
#[repr(C)]
pub struct Kernel {
pub current_tcb: UnsafeCell<*mut TaskControlBlock>,
pub ring_head: UnsafeCell<*mut TaskControlBlock>,
pub total_threads: UnsafeCell<usize>,
pub active_threads: UnsafeCell<usize>,
pub ticks: UnsafeCell<u64>,
pub switches: UnsafeCell<u64>,
pub ticks_deferred: UnsafeCell<u64>,
pub worst_latency: UnsafeCell<u32>,
pub last_latency: UnsafeCell<u32>,
pub worst_period_error_ns: UnsafeCell<u32>,
pub last_period_error_ns: UnsafeCell<u32>,
pub next_id: UnsafeCell<u32>,
pub running: UnsafeCell<bool>,
pub shutdown: UnsafeCell<bool>,
pub pending_free: UnsafeCell<*mut TaskControlBlock>,
pub reclaimed: UnsafeCell<u64>,
pub blocks_without_current: UnsafeCell<u64>,
pub idle_waits: UnsafeCell<u64>,
pub config: UnsafeCell<KernelConfig>,
}
unsafe impl Sync for Kernel {}
impl Kernel {
const fn new() -> Self {
Kernel {
current_tcb: UnsafeCell::new(ptr::null_mut()),
ring_head: UnsafeCell::new(ptr::null_mut()),
total_threads: UnsafeCell::new(0),
active_threads: UnsafeCell::new(0),
ticks: UnsafeCell::new(0),
switches: UnsafeCell::new(0),
ticks_deferred: UnsafeCell::new(0),
worst_latency: UnsafeCell::new(0),
last_latency: UnsafeCell::new(0),
worst_period_error_ns: UnsafeCell::new(0),
last_period_error_ns: UnsafeCell::new(0),
next_id: UnsafeCell::new(1),
running: UnsafeCell::new(false),
shutdown: UnsafeCell::new(false),
pending_free: UnsafeCell::new(ptr::null_mut()),
reclaimed: UnsafeCell::new(0),
blocks_without_current: UnsafeCell::new(0),
idle_waits: UnsafeCell::new(0),
config: UnsafeCell::new(KernelConfig::default_const()),
}
}
#[inline]
pub unsafe fn current(&self) -> *mut TaskControlBlock {
*self.current_tcb.get()
}
#[inline]
pub unsafe fn set_current(&self, tcb: *mut TaskControlBlock) {
*self.current_tcb.get() = tcb;
}
#[inline]
pub unsafe fn active(&self) -> usize {
*self.active_threads.get()
}
#[inline]
pub unsafe fn total(&self) -> usize {
*self.total_threads.get()
}
#[inline]
pub unsafe fn config(&self) -> KernelConfig {
*self.config.get()
}
}
#[no_mangle]
pub static KERNEL: Kernel = Kernel::new();
const _: () = assert!(core::mem::offset_of!(Kernel, current_tcb) == 0);