arceos_posix_api/imp/
task.rs

1use core::ffi::c_int;
2
3/// Relinquish the CPU, and switches to another task.
4///
5/// For single-threaded configuration (`multitask` feature is disabled), we just
6/// relax the CPU and wait for incoming interrupts.
7pub fn sys_sched_yield() -> c_int {
8    #[cfg(feature = "multitask")]
9    axtask::yield_now();
10    #[cfg(not(feature = "multitask"))]
11    if cfg!(feature = "irq") {
12        axhal::arch::wait_for_irqs();
13    } else {
14        core::hint::spin_loop();
15    }
16    0
17}
18
19/// Get current thread ID.
20pub fn sys_getpid() -> c_int {
21    syscall_body!(sys_getpid,
22        #[cfg(feature = "multitask")]
23        {
24            Ok(axtask::current().id().as_u64() as c_int)
25        }
26        #[cfg(not(feature = "multitask"))]
27        {
28            Ok(2) // `main` task ID
29        }
30    )
31}
32
33/// Exit current task
34pub fn sys_exit(exit_code: c_int) -> ! {
35    debug!("sys_exit <= {}", exit_code);
36    #[cfg(feature = "multitask")]
37    axtask::exit(exit_code);
38    #[cfg(not(feature = "multitask"))]
39    axhal::misc::terminate();
40}