Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ members = [

"ulib/axstd",
"ulib/axlibc",
"ulib/axasync-std",

"examples/helloworld",
"examples/helloworld-myplat",
"examples/httpclient",
"examples/httpserver",
"examples/httpserver",
"examples/shell",
"examples/hybrid-task-schedule",
]

[workspace.package]
Expand All @@ -45,6 +47,7 @@ categories = ["os", "no-std"]

[workspace.dependencies]
axstd = { path = "ulib/axstd" }
axasync-std = { path = "ulib/axasync-std" }
axlibc = { path = "ulib/axlibc" }

arceos_api = { path = "api/arceos_api" }
Expand Down
70 changes: 70 additions & 0 deletions api/arceos_api/src/imp/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ pub fn ax_sleep_until(deadline: crate::time::AxTimeValue) {
axhal::time::busy_wait_until(deadline);
}

pub async fn ax_sleep_until_f(deadline: crate::time::AxTimeValue) {
#[cfg(feature = "multitask")]
axtask::sleep_until_f(deadline).await;
#[cfg(not(feature = "multitask"))]
axhal::time::busy_wait_until(deadline);
}

pub fn ax_yield_now() {
#[cfg(feature = "multitask")]
axtask::yield_now();
Expand All @@ -16,13 +23,31 @@ pub fn ax_yield_now() {
}
}

pub async fn ax_yield_now_f() {
#[cfg(feature = "multitask")]
axtask::yield_now_f().await;
#[cfg(not(feature = "multitask"))]
if cfg!(feature = "irq") {
axhal::arch::wait_for_irqs();
} else {
core::hint::spin_loop();
}
}

pub fn ax_exit(_exit_code: i32) -> ! {
#[cfg(feature = "multitask")]
axtask::exit(_exit_code);
#[cfg(not(feature = "multitask"))]
crate::sys::ax_terminate();
}

pub async fn ax_exit_f(_exit_code: i32) {
#[cfg(feature = "multitask")]
axtask::exit_f(_exit_code).await;
#[cfg(not(feature = "multitask"))]
axhal::misc::terminate();
}

cfg_task! {
use core::time::Duration;

Expand Down Expand Up @@ -70,10 +95,25 @@ cfg_task! {
}
}

pub fn ax_spawn_f<F>(f: F, name: alloc::string::String) -> AxTaskHandle
where
F: core::future::Future<Output = ()> + Send + 'static,
{
let inner = axtask::spawn_raw_f(f, name);
AxTaskHandle {
id: inner.id().as_u64(),
inner,
}
}

pub fn ax_wait_for_exit(task: AxTaskHandle) -> Option<i32> {
task.inner.join()
}

pub async fn ax_wait_for_exit_f(task: AxTaskHandle) -> Option<i32> {
task.inner.join_f().await
}

pub fn ax_set_current_priority(prio: isize) -> crate::AxResult {
if axtask::set_priority(prio) {
Ok(())
Expand Down Expand Up @@ -109,6 +149,19 @@ cfg_task! {
false
}

pub async fn ax_wait_queue_wait_f(wq: &AxWaitQueueHandle, timeout: Option<Duration>) -> bool {
#[cfg(feature = "irq")]
if let Some(dur) = timeout {
return wq.0.wait_timeout_f(dur).await;
}

if timeout.is_some() {
axlog::warn!("ax_wait_queue_wait: the `timeout` argument is ignored without the `irq` feature");
}
wq.0.wait_f().await;
false
}

pub fn ax_wait_queue_wait_until(
wq: &AxWaitQueueHandle,
until_condition: impl Fn() -> bool,
Expand All @@ -126,6 +179,23 @@ cfg_task! {
false
}

pub async fn ax_wait_queue_wait_until_f(
wq: &AxWaitQueueHandle,
until_condition: impl Fn() -> bool,
timeout: Option<Duration>,
) -> bool {
#[cfg(feature = "irq")]
if let Some(dur) = timeout {
return wq.0.wait_timeout_until_f(dur, until_condition).await;
}

if timeout.is_some() {
axlog::warn!("ax_wait_queue_wait_until: the `timeout` argument is ignored without the `irq` feature");
}
wq.0.wait_until_f(until_condition).await;
false
}

pub fn ax_wait_queue_wake(wq: &AxWaitQueueHandle, count: u32) {
if count == u32::MAX {
wq.0.notify_all(true);
Expand Down
41 changes: 41 additions & 0 deletions api/arceos_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,50 @@ pub mod task {
pub fn ax_exit(exit_code: i32) -> !;
}

define_api! {
/// Current coroutine task is going to sleep, it will be woken up at the given deadline.
///
/// If the feature `multitask` is not enabled, it uses busy-wait instead
pub async fn ax_sleep_until_f(deadline: crate::time::AxTimeValue);
/// Current coroutine task gives up the CPU time voluntarily, and switches to another
/// ready task.
///
/// If the feature `multitask` is not enabled, it does nothing.
pub async fn ax_yield_now_f();

/// Exits the current coroutine task with the given exit code.
pub async fn ax_exit_f(exit_code: i32);
}

define_api! {
@cfg "multitask";

/// Waits for the given task to exit, and returns its exit code (the
/// argument of [`ax_exit`]).
pub async fn ax_wait_for_exit_f(task: AxTaskHandle) -> Option<i32>;
/// Blocks the current task and put it into the wait queue, until
/// other tasks notify the wait queue, or the the given duration has
/// elapsed (if specified).
pub async fn ax_wait_queue_wait_f(wq: &AxWaitQueueHandle, timeout: Option<core::time::Duration>) -> bool;
/// Blocks the current task and put it into the wait queue, until the
/// given condition becomes true, or the the given duration has elapsed
/// (if specified).
pub async fn ax_wait_queue_wait_until_f(
wq: &AxWaitQueueHandle,
until_condition: impl Fn() -> bool,
timeout: Option<core::time::Duration>,
) -> bool;
}

define_api! {
@cfg "multitask";

/// Spawns a new task with the given entry point and other arguments.
pub fn ax_spawn_f(
f: impl core::future::Future<Output = ()> + Send + 'static,
name: alloc::string::String,
) -> AxTaskHandle;

/// Returns the current task's ID.
pub fn ax_current_task_id() -> u64;
/// Spawns a new task with the given entry point and other arguments.
Expand Down
27 changes: 27 additions & 0 deletions api/arceos_api/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ macro_rules! define_api_type {
}

macro_rules! define_api {
($( $(#[$attr:meta])* $vis:vis async fn $name:ident( $($arg:ident : $type:ty),* $(,)? ) $( -> $ret:ty )? ; )+) => {
$(
$(#[$attr])*
$vis async fn $name( $($arg : $type),* ) $( -> $ret )? {
$crate::imp::$name( $($arg),* ).await
}
)+
};
($( $(#[$attr:meta])* $vis:vis fn $name:ident( $($arg:ident : $type:ty),* $(,)? ) $( -> $ret:ty )? ; )+) => {
$(
$(#[$attr])*
Expand Down Expand Up @@ -55,6 +63,25 @@ macro_rules! define_api {
}
)+
};
(
@cfg $feature:literal;
$( $(#[$attr:meta])* $vis:vis async fn $name:ident( $($arg:ident : $type:ty),* $(,)? ) $( -> $ret:ty )? ; )+
) => {
$(
#[cfg(feature = $feature)]
$(#[$attr])*
$vis async fn $name( $($arg : $type),* ) $( -> $ret )? {
$crate::imp::$name( $($arg),* ).await
}

#[allow(unused_variables)]
#[cfg(all(feature = "dummy-if-not-enabled", not(feature = $feature)))]
$(#[$attr])*
$vis async fn $name( $($arg : $type),* ) $( -> $ret )? {
unimplemented!(stringify!($name))
}
)+
};
(
@cfg $feature:literal;
$( $(#[$attr:meta])* $vis:vis unsafe fn $name:ident( $($arg:ident : $type:ty),* $(,)? ) $( -> $ret:ty )? ; )+
Expand Down
21 changes: 21 additions & 0 deletions examples/hybrid-task-schedule/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "hybrid-task-schedule"
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
axstd = { workspace = true, features = [
"alloc",
"multitask",
"net",
"irq",
], optional = true }
axasync-std = { workspace = true }
rand = { version = "0.9.1", default-features = false, features = ["small_rng"] }
Loading