use crate::config::ConfigError;
use core::fmt;
pub const DEFAULT_STACK_SIZE: usize = 4096;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpawnError {
NotInitialized,
ArenaExhausted,
Backend(ConfigError),
}
impl fmt::Display for SpawnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
SpawnError::NotInitialized => {
write!(f, "scheduler not initialized (call scheduler::init first)")
}
SpawnError::ArenaExhausted => write!(
f,
"kernel arena exhausted: increase the arena or reduce per-task stacks"
),
SpawnError::Backend(e) => write!(f, "backend could not create the task: {e}"),
}
}
}
pub fn spawn<F>(f: F)
where
F: FnOnce() + Send + 'static,
{
spawn_with_stack(f, crate::scheduler::config().stack_size)
}
pub fn spawn_with_stack<F>(f: F, stack_size: usize)
where
F: FnOnce() + Send + 'static,
{
if let Err(e) = try_spawn_with_stack(f, stack_size) {
panic!("rrkernel: thread::spawn failed: {}", e);
}
}
pub fn try_spawn<F>(f: F) -> Result<(), SpawnError>
where
F: FnOnce() + Send + 'static,
{
try_spawn_with_stack(f, crate::scheduler::config().stack_size)
}
pub fn try_spawn_with_stack<F>(f: F, stack_size: usize) -> Result<(), SpawnError>
where
F: FnOnce() + Send + 'static,
{
let stack_size = if stack_size == 0 {
DEFAULT_STACK_SIZE
} else {
stack_size
};
unsafe { crate::scheduler::spawn_internal(f, stack_size) }
}