#[cfg(feature = "priority_boost")]
use core::sync::atomic::Ordering;
#[cfg(feature = "priority_boost")]
use super::{task, utils};
use super::{BadContextError, BoostPriorityError, Kernel};
pub(super) fn expect_task_context<System: Kernel>() -> Result<(), BadContextError> {
if !System::is_task_context() {
Err(BadContextError::BadContext)
} else {
Ok(())
}
}
pub(super) fn expect_waitable_context<System: Kernel>() -> Result<(), BadContextError> {
if !System::is_task_context() || System::is_priority_boost_active() {
Err(BadContextError::BadContext)
} else {
Ok(())
}
}
#[cfg(feature = "priority_boost")]
pub(super) fn boost_priority<System: Kernel>() -> Result<(), BoostPriorityError> {
if System::is_cpu_lock_active()
|| !System::is_task_context()
|| System::is_priority_boost_active()
{
Err(BoostPriorityError::BadContext)
} else {
System::state()
.priority_boost
.store(true, Ordering::Relaxed);
Ok(())
}
}
#[cfg(feature = "priority_boost")]
pub(super) fn unboost_priority<System: Kernel>() -> Result<(), BoostPriorityError> {
if !System::is_task_context() || !System::is_priority_boost_active() {
Err(BoostPriorityError::BadContext)
} else {
let lock = utils::lock_cpu()?;
System::state()
.priority_boost
.store(false, Ordering::Relaxed);
task::unlock_cpu_and_check_preemption::<System>(lock);
Ok(())
}
}
#[cfg(not(feature = "priority_boost"))]
pub(super) fn unboost_priority<System: Kernel>() -> Result<(), BoostPriorityError> {
Err(BoostPriorityError::BadContext)
}