Skip to main content

osiris/uapi/
sched.rs

1use core::ffi::c_void;
2
3use crate::hal;
4use hal::stack::EntryFn;
5
6pub fn sleep(_until: u64) -> isize {
7    hal::asm::syscall!(1, (_until >> 32) as u32, _until as u32)
8}
9
10pub fn sleep_for(_duration: u64) -> isize {
11    hal::asm::syscall!(2, (_duration >> 32) as u32, _duration as u32)
12}
13
14pub fn yield_thread() -> isize {
15    let _until = u64::MAX;
16    hal::asm::syscall!(1, (_until >> 32) as u32, _until as u32)
17}
18
19#[repr(C)]
20#[derive(Clone, Copy)]
21pub struct RtAttrs {
22    pub deadline: u64,
23    pub period: u32,
24    pub budget: u32,
25}
26
27/// Spawn a thread. `ctx` is delivered to `func_ptr` as its sole argument;
28/// caller owns the lifetime of the pointee.
29pub fn spawn_thread(_func_ptr: EntryFn, _ctx: *mut c_void, attrs: Option<RtAttrs>) -> isize {
30    if let Some(attrs) = attrs {
31        if attrs.budget == 0 || attrs.period == 0 {
32            return -1; // Invalid attributes
33        }
34
35        if attrs.deadline == 0 || attrs.budget as u64 > attrs.deadline {
36            return -1; // Budget cannot exceed relative deadline
37        }
38
39        if attrs.deadline > attrs.period as u64 {
40            return -1; // Relative deadline cannot exceed period
41        }
42
43        if attrs.budget > u32::MAX / 2 || attrs.period > u32::MAX / 2 {
44            return -1; // Prevent potential overflow in calculations
45        }
46
47        hal::asm::syscall!(
48            3,
49            _func_ptr as u32,
50            _ctx as usize,
51            &attrs as *const RtAttrs as usize
52        )
53    } else {
54        -1
55    }
56}
57
58pub fn exit(_code: usize) -> ! {
59    hal::asm::syscall!(4, _code as u32);
60    loop {
61        hal::asm::nop!();
62    }
63}
64
65/// Raw `UId::as_usize()` of the calling thread, or `-1` if there is
66/// no current thread (kernel pre-init).
67pub fn current_id() -> isize {
68    hal::asm::syscall!(6)
69}