osiris/syscalls/
sched.rs

1//! This module provides task management related syscalls.
2
3use core::ffi::c_int;
4
5use proc_macros::syscall_handler;
6
7use crate::{sched, time, uapi::sched::RtAttrs};
8
9#[syscall_handler(num = 1)]
10fn sleep(until_hi: u32, until_lo: u32) -> c_int {
11    let until = ((until_hi as u64) << 32) | (until_lo as u64);
12    sched::with(|sched| {
13        if sched.sleep_until(until, time::tick()).is_err() {
14            bug!("no current thread set.");
15        }
16    });
17    0
18}
19
20#[syscall_handler(num = 2)]
21fn sleep_for(duration_hi: u32, duration_lo: u32) -> c_int {
22    let duration = ((duration_hi as u64) << 32) | (duration_lo as u64);
23    sched::with(|sched| {
24        let now = time::tick();
25        if sched.sleep_until(now + duration, now).is_err() {
26            bug!("no current thread set.");
27        }
28    });
29    0
30}
31
32#[syscall_handler(num = 3)]
33fn spawn_thread(func_ptr: usize, attrs: *const RtAttrs) -> c_int {
34    sched::with(|sched| {
35        let attrs = if attrs.is_null() {
36            None
37        } else {
38            Some(unsafe { *attrs })
39        };
40
41        let attrs = sched::thread::Attributes {
42            entry: unsafe { core::mem::transmute(func_ptr) },
43            fin: None,
44            attrs,
45        };
46        match sched.create_thread(None, &attrs) {
47            Ok(uid) => {
48                if sched.enqueue(time::tick(), uid).is_err() {
49                    bug!("failed to enqueue thread.");
50                }
51                uid.as_usize() as c_int
52            }
53            Err(_) => -1,
54        }
55    })
56}
57
58#[syscall_handler(num = 4)]
59fn exit(_code: usize) -> c_int {
60    sched::with(|sched| {
61        if sched.kill_by_thread(None).is_err() {
62            bug!("failed to terminate thread.");
63        }
64    });
65    0
66}
67
68#[syscall_handler(num = 5)]
69fn kick_thread(_uid: usize) -> c_int {
70    // TODO: Implement a way to retrieve the thread UID from the usize uid.
71    0
72}