Skip to main content

ax_api/imp/
task.rs

1#[track_caller]
2pub fn ax_sleep_until(deadline: crate::time::AxTimeValue) {
3    ax_task::sleep_until(deadline);
4}
5
6#[track_caller]
7pub fn ax_yield_now() {
8    ax_task::yield_now();
9}
10
11#[track_caller]
12pub fn ax_exit(exit_code: i32) -> ! {
13    ax_task::exit(exit_code);
14}
15
16cfg_task! {
17    use core::time::Duration;
18
19    /// A handle to a task.
20    pub struct AxTaskHandle {
21        inner: ax_task::AxTaskRef,
22        id: u64,
23    }
24
25    impl AxTaskHandle {
26        /// Returns the task ID.
27        pub fn id(&self) -> u64 {
28            self.id
29        }
30    }
31
32    /// A mask to specify the CPU affinity.
33    pub use ax_task::AxCpuMask;
34
35    pub use ax_runtime::sync::RawMutex as AxRawMutex;
36
37    /// A handle to a wait queue.
38    ///
39    /// A wait queue is used to store sleeping tasks waiting for a certain event
40    /// to happen.
41    pub struct AxWaitQueueHandle(ax_task::WaitQueue);
42
43    impl AxWaitQueueHandle {
44        /// Creates a new empty wait queue.
45        pub const fn new() -> Self {
46            Self(ax_task::WaitQueue::new())
47        }
48    }
49
50    impl Default for AxWaitQueueHandle {
51        fn default() -> Self {
52            Self::new()
53        }
54    }
55
56    pub fn ax_current_task_id() -> u64 {
57        ax_task::current().id().as_u64()
58    }
59
60    pub fn ax_spawn<F>(f: F, name: alloc::string::String, stack_size: usize) -> AxTaskHandle
61    where
62        F: FnOnce() + Send + 'static,
63    {
64        let inner = ax_task::spawn_raw(f, name, stack_size);
65        AxTaskHandle {
66            id: inner.id().as_u64(),
67            inner,
68        }
69    }
70
71    #[track_caller]
72    pub fn ax_wait_for_exit(task: AxTaskHandle) -> i32 {
73        task.inner.join()
74    }
75
76    pub fn ax_set_current_priority(prio: isize) -> crate::ApiResult {
77        if ax_task::set_priority(prio) {
78            Ok(())
79        } else {
80            Err(crate::ApiError::PriorityUpdateFailed)
81        }
82    }
83
84    #[track_caller]
85    pub fn ax_set_current_affinity(cpumask: AxCpuMask) -> crate::ApiResult {
86        if ax_task::set_current_affinity(cpumask) {
87            Ok(())
88        } else {
89            Err(crate::ApiError::AffinityUpdateFailed)
90        }
91    }
92
93    #[track_caller]
94    pub fn ax_wait_queue_wait(wq: &AxWaitQueueHandle, timeout: Option<Duration>) -> bool {
95        if let Some(dur) = timeout {
96            return wq.0.wait_timeout(dur);
97        }
98        wq.0.wait();
99        false
100    }
101
102    #[track_caller]
103    pub fn ax_wait_queue_wait_until(
104        wq: &AxWaitQueueHandle,
105        until_condition: impl Fn() -> bool,
106        timeout: Option<Duration>,
107    ) -> bool {
108        if let Some(dur) = timeout {
109            return wq.0.wait_timeout_until(dur, until_condition);
110        }
111        wq.0.wait_until(until_condition);
112        false
113    }
114
115    pub fn ax_wait_queue_wake(wq: &AxWaitQueueHandle, count: u32) {
116        if count == u32::MAX {
117            wq.0.notify_all(true);
118        } else {
119            for _ in 0..count {
120                if !wq.0.notify_one(true) {
121                    break;
122                }
123            }
124        }
125    }
126
127    pub fn ax_wait_queue_wake_one_with<F>(wq: &AxWaitQueueHandle, func: F)
128    where
129        F: Fn(u64),
130    {
131        wq.0.notify_one_with(true, func);
132    }
133
134}