Skip to main content

ax_std/thread/
mod.rs

1//! Native threads.
2
3mod multi;
4use core::num::NonZero;
5
6use ax_api::task as api;
7pub use multi::*;
8
9/// Current thread gives up the CPU time voluntarily, and switches to another
10/// ready thread.
11#[track_caller]
12pub fn yield_now() {
13    api::ax_yield_now();
14}
15
16/// Exits the current thread.
17#[track_caller]
18pub fn exit(exit_code: i32) -> ! {
19    api::ax_exit(exit_code);
20}
21
22/// Current thread is going to sleep for the given duration.
23#[track_caller]
24pub fn sleep(dur: core::time::Duration) {
25    sleep_until(ax_api::time::ax_monotonic_time() + dur);
26}
27
28/// Current thread is going to sleep, it will be woken up at the given deadline.
29/// The deadline is measured against the monotonic clock.
30#[track_caller]
31pub fn sleep_until(deadline: ax_api::time::AxTimeValue) {
32    api::ax_sleep_until(deadline);
33}
34
35/// Returns an estimate of the default amount of parallelism a program should use.
36///
37/// Here we directly return the number of available logical CPUs, representing
38/// the theoretical maximum parallelism.
39pub fn available_parallelism() -> crate::StdResult<NonZero<usize>> {
40    NonZero::new(ax_api::sys::ax_get_cpu_num()).ok_or(crate::StdError::NoAvailableCpu)
41}