use core::fmt;
use crate::{
kernel::{
raw, AdjustTimeError, BoostPriorityError, CpuLockError, ExitTaskError, ParkError,
ParkTimeoutError, SleepError, TimeError,
},
time::{Duration, Time},
};
#[doc = include_str!("../common.md")]
pub trait Kernel: private::Sealed {
type DebugPrinter: fmt::Debug + Send + Sync;
fn debug() -> <Self as Kernel>::DebugPrinter;
fn acquire_cpu_lock() -> Result<(), CpuLockError>;
unsafe fn release_cpu_lock() -> Result<(), CpuLockError>;
fn has_cpu_lock() -> bool;
fn boost_priority() -> Result<(), BoostPriorityError>
where
Self: raw::KernelBoostPriority;
unsafe fn unboost_priority() -> Result<(), BoostPriorityError>;
fn is_priority_boost_active() -> bool;
fn is_task_context() -> bool;
fn is_interrupt_context() -> bool;
fn is_boot_complete() -> bool;
fn set_time(time: Time) -> Result<(), TimeError>;
fn time() -> Result<Time, TimeError>
where
Self: raw::KernelTime;
fn time_user_headroom() -> Duration
where
Self: raw::KernelAdjustTime;
#[doc = svgbobdoc::transform!(
/// ```svgbob
/// system time
/// ----*------------------------
/// ^ frontier
/// ​
/// (b)
/// ​
/// --------*--------------------
/// system time ^
/// ----------*------------ ------------*----------------
/// ^ frontier ^
/// -----------------*-----------
/// (a) ^
/// ----------------------*------
/// ^
/// (c)
/// ```
)]
fn adjust_time(delta: Duration) -> Result<(), AdjustTimeError>
where
Self: raw::KernelAdjustTime;
unsafe fn exit_task() -> Result<!, ExitTaskError>;
fn park() -> Result<(), ParkError>;
fn park_timeout(timeout: Duration) -> Result<(), ParkTimeoutError>;
fn sleep(duration: Duration) -> Result<(), SleepError>;
}
mod private {
pub trait Sealed {}
impl<T: super::raw::KernelBase> Sealed for T {}
}
impl<T: raw::KernelBase> Kernel for T {
type DebugPrinter = <Self as raw::KernelBase>::RawDebugPrinter;
#[inline]
fn debug() -> <Self as Kernel>::DebugPrinter {
<T as raw::KernelBase>::raw_debug()
}
#[inline]
fn acquire_cpu_lock() -> Result<(), CpuLockError> {
<T as raw::KernelBase>::raw_acquire_cpu_lock()
}
#[inline]
unsafe fn release_cpu_lock() -> Result<(), CpuLockError> {
unsafe { <T as raw::KernelBase>::raw_release_cpu_lock() }
}
#[inline]
fn has_cpu_lock() -> bool {
<T as raw::KernelBase>::raw_has_cpu_lock()
}
#[inline]
fn boost_priority() -> Result<(), BoostPriorityError>
where
Self: raw::KernelBoostPriority,
{
<T as raw::KernelBoostPriority>::raw_boost_priority()
}
#[inline]
unsafe fn unboost_priority() -> Result<(), BoostPriorityError> {
unsafe { <T as raw::KernelBase>::raw_unboost_priority() }
}
#[inline]
fn is_priority_boost_active() -> bool {
<T as raw::KernelBase>::raw_is_priority_boost_active()
}
#[inline]
fn is_task_context() -> bool {
<T as raw::KernelBase>::raw_is_task_context()
}
#[inline]
fn is_interrupt_context() -> bool {
<T as raw::KernelBase>::raw_is_interrupt_context()
}
#[inline]
fn is_boot_complete() -> bool {
<T as raw::KernelBase>::raw_is_boot_complete()
}
#[inline]
fn set_time(time: Time) -> Result<(), TimeError> {
<T as raw::KernelBase>::raw_set_time(time)
}
#[inline]
fn time() -> Result<Time, TimeError>
where
Self: raw::KernelTime,
{
<T as raw::KernelTime>::raw_time()
}
#[inline]
fn time_user_headroom() -> Duration
where
Self: raw::KernelAdjustTime,
{
<T as raw::KernelAdjustTime>::RAW_TIME_USER_HEADROOM
}
#[inline]
fn adjust_time(delta: Duration) -> Result<(), AdjustTimeError>
where
Self: raw::KernelAdjustTime,
{
<T as raw::KernelAdjustTime>::raw_adjust_time(delta)
}
#[inline]
unsafe fn exit_task() -> Result<!, ExitTaskError> {
unsafe { <T as raw::KernelBase>::raw_exit_task() }
}
#[inline]
fn park() -> Result<(), ParkError> {
<T as raw::KernelBase>::raw_park()
}
#[inline]
fn park_timeout(timeout: Duration) -> Result<(), ParkTimeoutError> {
<T as raw::KernelBase>::raw_park_timeout(timeout)
}
#[inline]
fn sleep(duration: Duration) -> Result<(), SleepError> {
<T as raw::KernelBase>::raw_sleep(duration)
}
}