axstd/thread/mod.rs
1//! Native threads.
2
3#[cfg(feature = "multitask")]
4mod multi;
5#[cfg(feature = "multitask")]
6pub use multi::*;
7
8use arceos_api::task as api;
9use core::num::NonZero;
10
11/// Current thread gives up the CPU time voluntarily, and switches to another
12/// ready thread.
13///
14/// For single-threaded configuration (`multitask` feature is disabled), we just
15/// relax the CPU and wait for incoming interrupts.
16pub fn yield_now() {
17 api::ax_yield_now();
18}
19
20/// Exits the current thread.
21///
22/// For single-threaded configuration (`multitask` feature is disabled),
23/// it directly terminates the main thread and shutdown.
24pub fn exit(exit_code: i32) -> ! {
25 api::ax_exit(exit_code);
26}
27
28/// Current thread is going to sleep for the given duration.
29///
30/// If one of `multitask` or `irq` features is not enabled, it uses busy-wait
31/// instead.
32pub fn sleep(dur: core::time::Duration) {
33 sleep_until(arceos_api::time::ax_wall_time() + dur);
34}
35
36/// Current thread is going to sleep, it will be woken up at the given deadline.
37///
38/// If one of `multitask` or `irq` features is not enabled, it uses busy-wait
39/// instead.
40pub fn sleep_until(deadline: arceos_api::time::AxTimeValue) {
41 api::ax_sleep_until(deadline);
42}
43
44/// Returns an estimate of the default amount of parallelism a program should use.
45///
46/// Here we directly return the number of available logical CPUs, representing
47/// the theoretical maximum parallelism.
48pub fn available_parallelism() -> crate::io::Result<NonZero<usize>> {
49 NonZero::new(arceos_api::modules::axconfig::plat::CPU_NUM)
50 .ok_or_else(|| panic!("No available CPUs found, cannot determine parallelism"))
51}