Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Yield instead of sleep in component-async-tests (#12567)

Alex Crichton112112d4ec4d
diff --git a/crates/misc/component-async-tests/src/lib.rs b/crates/misc/component-async-tests/src/lib.rs
index 86d4d6c3c3..76de164d0b 100644
--- a/crates/misc/component-async-tests/src/lib.rs
+++ b/crates/misc/component-async-tests/src/lib.rs
@@ -9,10 +9,10 @@ pub mod resource_stream;
pub mod round_trip;
pub mod round_trip_direct;
pub mod round_trip_many;
-pub mod sleep;
pub mod transmit;
pub mod util;
-pub mod yield_host;
+pub mod yield_;
+pub mod yield_runner;
/// Host implementation, usable primarily by tests
pub struct Ctx {
diff --git a/crates/misc/component-async-tests/src/round_trip.rs b/crates/misc/component-async-tests/src/round_trip.rs
index 7308abfece..95294e86aa 100644
--- a/crates/misc/component-async-tests/src/round_trip.rs
+++ b/crates/misc/component-async-tests/src/round_trip.rs
@@ -1,5 +1,5 @@
use super::Ctx;
-use std::time::Duration;
+use crate::util::yield_times;
use wasmtime::component::Accessor;
pub mod bindings {
@@ -20,7 +20,7 @@ pub mod non_concurrent_export_bindings {
impl bindings::local::local::baz::HostWithStore for Ctx {
async fn foo<T>(_: &Accessor<T, Self>, s: String) -> wasmtime::Result<String> {
- crate::util::sleep(Duration::from_millis(10)).await;
+ yield_times(10).await;
Ok(format!("{s} - entered host - exited host"))
}
}
diff --git a/crates/misc/component-async-tests/src/round_trip_direct.rs b/crates/misc/component-async-tests/src/round_trip_direct.rs
index 1cab5002f5..d149bb974d 100644
--- a/crates/misc/component-async-tests/src/round_trip_direct.rs
+++ b/crates/misc/component-async-tests/src/round_trip_direct.rs
@@ -1,5 +1,5 @@
use super::Ctx;
-use std::time::Duration;
+use crate::util::yield_times;
use wasmtime::component::Accessor;
pub mod bindings {
@@ -11,7 +11,7 @@ pub mod bindings {
impl bindings::RoundTripDirectImportsWithStore for Ctx {
async fn foo<T>(_: &Accessor<T, Self>, s: String) -> String {
- crate::util::sleep(Duration::from_millis(10)).await;
+ yield_times(5).await;
format!("{s} - entered host - exited host")
}
}
diff --git a/crates/misc/component-async-tests/src/round_trip_many.rs b/crates/misc/component-async-tests/src/round_trip_many.rs
index 6f9cf5f146..b213907df0 100644
--- a/crates/misc/component-async-tests/src/round_trip_many.rs
+++ b/crates/misc/component-async-tests/src/round_trip_many.rs
@@ -1,5 +1,5 @@
use super::Ctx;
-use std::time::Duration;
+use crate::util::yield_times;
use wasmtime::Result;
use wasmtime::component::Accessor;
@@ -41,7 +41,7 @@ impl bindings::local::local::many::HostWithStore for Ctx {
Option<Stuff>,
Result<Stuff, ()>,
) {
- crate::util::sleep(Duration::from_millis(10)).await;
+ yield_times(5).await;
(
format!("{a} - entered host - exited host"),
b,
diff --git a/crates/misc/component-async-tests/src/sleep.rs b/crates/misc/component-async-tests/src/sleep.rs
deleted file mode 100644
index 13e13950c7..0000000000
--- a/crates/misc/component-async-tests/src/sleep.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-use super::Ctx;
-use std::time::Duration;
-use wasmtime::component::Accessor;
-
-wasmtime::component::bindgen!({
- path: "wit",
- world: "sleep-host",
-});
-
-impl local::local::sleep::HostWithStore for Ctx {
- async fn sleep_millis<T>(_: &Accessor<T, Self>, time_in_millis: u64) {
- crate::util::sleep(Duration::from_millis(time_in_millis)).await;
- }
-}
-
-impl local::local::sleep::Host for Ctx {}
diff --git a/crates/misc/component-async-tests/src/util.rs b/crates/misc/component-async-tests/src/util.rs
index 2fb1dbf4f7..421e0473b3 100644
--- a/crates/misc/component-async-tests/src/util.rs
+++ b/crates/misc/component-async-tests/src/util.rs
@@ -3,7 +3,6 @@ use std::{
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
- thread,
};
use wasmtime::Result;
use wasmtime::{
@@ -14,24 +13,9 @@ use wasmtime::{
},
};
-pub async fn sleep(duration: std::time::Duration) {
- if cfg!(miri) {
- // TODO: We should be able to use `tokio::time::sleep` here, but as of
- // this writing the miri-compatible version of `wasmtime-fiber` uses
- // threads behind the scenes, which means thread-local storage is not
- // preserved when we switch fibers, and that confuses Tokio. If we ever
- // fix that we can stop using our own, special version of `sleep` and
- // switch back to the Tokio version.
-
- let (tx, rx) = oneshot::channel();
- let handle = thread::spawn(move || {
- thread::sleep(duration);
- _ = tx.send(());
- });
- _ = rx.await;
- _ = handle.join();
- } else {
- tokio::time::sleep(duration).await;
+pub async fn yield_times(n: usize) {
+ for _ in 0..n {
+ tokio::task::yield_now().await;
}
}
diff --git a/crates/misc/component-async-tests/src/yield_.rs b/crates/misc/component-async-tests/src/yield_.rs
new file mode 100644
index 0000000000..c974620588
--- /dev/null
+++ b/crates/misc/component-async-tests/src/yield_.rs
@@ -0,0 +1,15 @@
+use super::Ctx;
+use wasmtime::component::Accessor;
+
+wasmtime::component::bindgen!({
+ path: "wit",
+ world: "yield-host",
+});
+
+impl local::local::yield_::HostWithStore for Ctx {
+ async fn yield_times<T>(_: &Accessor<T, Self>, times: u64) {
+ crate::util::yield_times(times.try_into().unwrap()).await;
+ }
+}
+
+impl local::local::yield_::Host for Ctx {}
diff --git a/crates/misc/component-async-tests/src/yield_host.rs b/crates/misc/component-async-tests/src/yield_runner.rs
similarity index 98%
rename from crates/misc/component-async-tests/src/yield_host.rs
rename to crates/misc/component-async-tests/src/yield_runner.rs
index 2cb39990e5..859830e1de 100644
--- a/crates/misc/component-async-tests/src/yield_host.rs
+++ b/crates/misc/component-async-tests/src/yield_runner.rs
@@ -8,7 +8,7 @@ use wasmtime::component::{Accessor, Resource};
pub mod bindings {
wasmtime::component::bindgen!({
path: "wit",
- world: "yield-host",
+ world: "yield-runner",
imports: { default: trappable },
with: {
"local:local/ready.thing": super::Thing,
diff --git a/crates/misc/component-async-tests/tests/scenario/common.rs b/crates/misc/component-async-tests/tests/scenario/common.rs
deleted file mode 100644
index 13ec2d169c..0000000000
--- a/crates/misc/component-async-tests/tests/scenario/common.rs
+++ /dev/null
@@ -1,113 +0,0 @@
-use std::sync::{Arc, Mutex, Once};
-use std::time::Duration;
-
-use wasmtime::Result;
-use tokio::fs;
-use wasm_compose::composer::ComponentComposer;
-use wasmtime::component::{Component, Linker, PromisesUnordered, ResourceTable};
-use wasmtime::{Config, Engine, Store};
-use wasmtime_wasi::WasiCtxBuilder;
-
-use component_async_tests::Ctx;
-
-pub fn annotate<T, F>(val: F) -> F
-where
- F: Fn(&mut T) -> &mut T,
-{
- val
-}
-
-pub fn init_logger() {
- static ONCE: Once = Once::new();
- ONCE.call_once(pretty_env_logger::init);
-}
-
-/// Compose two components
-///
-/// a is the "root" component, and b is composed into it
-#[allow(unused)]
-pub async fn compose(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
- let dir = tempfile::tempdir()?;
-
- let a_file = dir.path().join("a.wasm");
- fs::write(&a_file, a).await?;
-
- let b_file = dir.path().join("b.wasm");
- fs::write(&b_file, b).await?;
-
- ComponentComposer::new(
- &a_file,
- &wasm_compose::config::Config {
- dir: dir.path().to_owned(),
- definitions: vec![b_file.to_owned()],
- ..Default::default()
- },
- )
- .compose()
-}
-
-#[allow(unused)]
-pub async fn test_run(component: &[u8]) -> Result<()> {
- let mut config = config();
- // As of this writing, miri/pulley/epochs is a problematic combination, so
- // we don't test it.
- if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
- config.epoch_interruption(true);
- }
-
- let engine = Engine::new(&config)?;
-
- let component = Component::new(&engine, component)?;
-
- let mut linker = Linker::new(&engine);
-
- wasmtime_wasi::add_to_linker_async(&mut linker)?;
- component_async_tests::yield_host::bindings::local::local::continue_::add_to_linker_get_host(
- &mut linker,
- annotate(|ctx| ctx),
- )?;
- component_async_tests::yield_host::bindings::local::local::ready::add_to_linker_get_host(
- &mut linker,
- annotate(|ctx| ctx),
- )?;
- component_async_tests::resource_stream::bindings::local::local::resource_stream::add_to_linker_get_host(
- &mut linker,
- annotate(|ctx| ctx),
- )?;
-
- let mut store = Store::new(
- &engine,
- Ctx {
- wasi: WasiCtxBuilder::new().inherit_stdio().build(),
- table: ResourceTable::default(),
- continue_: false,
- wakers: Arc::new(Mutex::new(None)),
- },
- );
-
- if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
- store.set_epoch_deadline(1);
-
- std::thread::spawn(move || {
- std::thread::sleep(Duration::from_secs(10));
- engine.increment_epoch();
- });
- }
-
- let yield_host = component_async_tests::yield_host::bindings::YieldHost::instantiate_async(
- &mut store, &component, &linker,
- )
- .await?;
-
- // Start three concurrent calls and then join them all:
- let mut promises = PromisesUnordered::new();
- for _ in 0..3 {
- promises.push(yield_host.local_local_run().call_run(&mut store).await?);
- }
-
- while let Some(()) = promises.next(&mut store).await? {
- // continue
- }
-
- Ok(())
-}
diff --git a/crates/misc/component-async-tests/tests/scenario/post_return.rs b/crates/misc/component-async-tests/tests/scenario/post_return.rs
index 464a97c4b4..d85d39d4ae 100644
--- a/crates/misc/component-async-tests/tests/scenario/post_return.rs
+++ b/crates/misc/component-async-tests/tests/scenario/post_return.rs
@@ -1,20 +1,19 @@
use super::util::test_run;
use crate::scenario::util::{config, make_component};
use component_async_tests::util;
-use component_async_tests::{Ctx, sleep};
+use component_async_tests::{Ctx, yield_};
use std::future;
use std::pin::pin;
use std::task::Poll;
-use std::time::Duration;
use wasmtime::Result;
use wasmtime::component::{Accessor, Linker, ResourceTable};
use wasmtime::{AsContextMut, Engine, Store, StoreContextMut};
use wasmtime_wasi::WasiCtxBuilder;
-mod sleep_post_return {
+mod yield_post_return {
wasmtime::component::bindgen!({
path: "wit",
- world: "sleep-post-return-callee",
+ world: "yield-post-return-callee",
exports: { default: task_exit },
});
}
@@ -36,21 +35,21 @@ pub async fn async_post_return_caller() -> Result<()> {
}
#[tokio::test]
-pub async fn async_sleep_post_return_caller() -> Result<()> {
- test_sleep_post_return(&[
- test_programs_artifacts::ASYNC_SLEEP_POST_RETURN_CALLER_COMPONENT,
- test_programs_artifacts::ASYNC_SLEEP_POST_RETURN_CALLEE_COMPONENT,
+pub async fn async_yield_post_return_caller() -> Result<()> {
+ test_yield_post_return(&[
+ test_programs_artifacts::ASYNC_YIELD_POST_RETURN_CALLER_COMPONENT,
+ test_programs_artifacts::ASYNC_YIELD_POST_RETURN_CALLEE_COMPONENT,
])
.await
}
#[tokio::test]
-pub async fn async_sleep_post_return_callee() -> Result<()> {
- test_sleep_post_return(&[test_programs_artifacts::ASYNC_SLEEP_POST_RETURN_CALLEE_COMPONENT])
+pub async fn async_yield_post_return_callee() -> Result<()> {
+ test_yield_post_return(&[test_programs_artifacts::ASYNC_YIELD_POST_RETURN_CALLEE_COMPONENT])
.await
}
-async fn test_sleep_post_return(components: &[&str]) -> Result<()> {
+async fn test_yield_post_return(components: &[&str]) -> Result<()> {
let engine = Engine::new(&config())?;
let component = make_component(&engine, components).await?;
@@ -58,7 +57,7 @@ async fn test_sleep_post_return(components: &[&str]) -> Result<()> {
let mut linker = Linker::new(&engine);
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
- sleep::local::local::sleep::add_to_linker::<_, Ctx>(&mut linker, |ctx| ctx)?;
+ yield_::local::local::yield_::add_to_linker::<_, Ctx>(&mut linker, |ctx| ctx)?;
let mut store = Store::new(
&engine,
@@ -69,19 +68,19 @@ async fn test_sleep_post_return(components: &[&str]) -> Result<()> {
},
);
- let guest = sleep_post_return::SleepPostReturnCallee::instantiate_async(
+ let guest = yield_post_return::YieldPostReturnCallee::instantiate_async(
&mut store, &component, &linker,
)
.await?;
async fn run_with(
accessor: &Accessor<Ctx>,
- guest: &sleep_post_return::SleepPostReturnCallee,
+ guest: &yield_post_return::YieldPostReturnCallee,
) -> Result<()> {
- // This function should return immediately, then sleep the specified
- // number of milliseconds after returning, and then finally exit.
+ // This function should return immediately, then yield the specified
+ // number of times after returning, and then finally exit.
let exit = guest
- .local_local_sleep_post_return()
+ .local_local_yield_post_return()
.call_run(accessor, 100)
.await?
.1;
@@ -93,7 +92,7 @@ async fn test_sleep_post_return(components: &[&str]) -> Result<()> {
async fn run(
store: StoreContextMut<'_, Ctx>,
- guest: &sleep_post_return::SleepPostReturnCallee,
+ guest: &yield_post_return::YieldPostReturnCallee,
) -> Result<()> {
store
.run_concurrent(async |accessor| {
@@ -104,7 +103,7 @@ async fn test_sleep_post_return(components: &[&str]) -> Result<()> {
// outstanding guest or host tasks to poll for a while, trusting
// that we'll resolve the future independently, with or without
// giving it more work to do.
- util::sleep(Duration::from_millis(100)).await;
+ util::yield_times(10).await;
run_with(accessor, guest).await?;
diff --git a/crates/misc/component-async-tests/tests/scenario/round_trip.rs b/crates/misc/component-async-tests/tests/scenario/round_trip.rs
index 21a52345b0..be701f86b6 100644
--- a/crates/misc/component-async-tests/tests/scenario/round_trip.rs
+++ b/crates/misc/component-async-tests/tests/scenario/round_trip.rs
@@ -1,15 +1,13 @@
-use std::sync::Arc;
-use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
-use std::time::Duration;
-
use super::util::{config, make_component};
use component_async_tests::Ctx;
-use component_async_tests::util::sleep;
+use component_async_tests::util::yield_times;
use futures::{
FutureExt,
channel::oneshot,
stream::{FuturesUnordered, TryStreamExt},
};
+use std::sync::Arc;
+use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
use wasmtime::component::{
Accessor, AccessorTask, HasData, HasSelf, Instance, Linker, ResourceTable, Val,
};
@@ -499,7 +497,7 @@ pub async fn test_round_trip(
.instance("local:local/baz")?
.func_new_concurrent("foo", |_, _, params, results| {
Box::pin(async move {
- sleep(Duration::from_millis(10)).await;
+ yield_times(5).await;
let Some(Val::String(s)) = params.into_iter().next() else {
unreachable!()
};
diff --git a/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs b/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
index 1726680903..3228fee768 100644
--- a/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
+++ b/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
@@ -1,8 +1,6 @@
-use std::time::Duration;
-
use super::util::{config, make_component};
use component_async_tests::Ctx;
-use component_async_tests::util::sleep;
+use component_async_tests::util::yield_times;
use futures::stream::{FuturesUnordered, TryStreamExt};
use wasmtime::component::{Linker, ResourceTable, Val};
use wasmtime::{Engine, Result, Store, format_err};
@@ -93,7 +91,7 @@ async fn test_round_trip_direct(
.root()
.func_new_concurrent("foo", |_, _, params, results| {
Box::pin(async move {
- sleep(Duration::from_millis(10)).await;
+ yield_times(10).await;
let Some(Val::String(s)) = params.into_iter().next() else {
unreachable!()
};
diff --git a/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs b/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs
index 1cd0999374..6ebd7bc8fe 100644
--- a/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs
+++ b/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs
@@ -1,10 +1,9 @@
use std::iter;
use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
-use std::time::Duration;
use super::util::{config, make_component};
use component_async_tests::Ctx;
-use component_async_tests::util::sleep;
+use component_async_tests::util::yield_times;
use futures::{
FutureExt,
stream::{FuturesUnordered, TryStreamExt},
@@ -338,7 +337,7 @@ async fn test_round_trip_many(
.instance("local:local/many")?
.func_new_concurrent("foo", |_, _, params, results| {
Box::pin(async move {
- sleep(Duration::from_millis(10)).await;
+ yield_times(5).await;
let mut params = params.into_iter();
let Some(Val::String(s)) = params.next() else {
unreachable!()
diff --git a/crates/misc/component-async-tests/tests/scenario/streams.rs b/crates/misc/component-async-tests/tests/scenario/streams.rs
index e025b9af22..446b91709c 100644
--- a/crates/misc/component-async-tests/tests/scenario/streams.rs
+++ b/crates/misc/component-async-tests/tests/scenario/streams.rs
@@ -2,7 +2,7 @@ use {
super::util::{config, make_component},
component_async_tests::{
Ctx, closed_streams,
- util::{OneshotConsumer, OneshotProducer, PipeConsumer, PipeProducer},
+ util::{OneshotConsumer, OneshotProducer, PipeConsumer, PipeProducer, yield_times},
},
futures::{
FutureExt, Sink, SinkExt, Stream, StreamExt,
@@ -15,7 +15,6 @@ use {
pin::Pin,
sync::{Arc, Mutex},
task::{self, Context, Poll},
- time::Duration,
},
wasmtime::{
Engine, Result, Store, StoreContextMut,
@@ -299,15 +298,15 @@ pub async fn async_closed_stream() -> Result<()> {
struct VecProducer<T> {
source: Vec<T>,
- sleep: Pin<Box<dyn Future<Output = ()> + Send>>,
+ maybe_yield: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl<T> VecProducer<T> {
fn new(source: Vec<T>, delay: bool) -> Self {
Self {
source,
- sleep: if delay {
- tokio::time::sleep(Duration::from_millis(10)).boxed()
+ maybe_yield: if delay {
+ yield_times(5).boxed()
} else {
async {}.boxed()
},
@@ -326,9 +325,9 @@ impl<D, T: Lift + Unpin + 'static> StreamProducer<D> for VecProducer<T> {
mut destination: Destination<Self::Item, Self::Buffer>,
_: bool,
) -> Poll<Result<StreamResult>> {
- let sleep = &mut self.as_mut().get_mut().sleep;
- task::ready!(sleep.as_mut().poll(cx));
- *sleep = async {}.boxed();
+ let maybe_yield = &mut self.as_mut().get_mut().maybe_yield;
+ task::ready!(maybe_yield.as_mut().poll(cx));
+ *maybe_yield = async {}.boxed();
destination.set_buffer(mem::take(&mut self.get_mut().source).into());
Poll::Ready(Ok(StreamResult::Dropped))
@@ -337,15 +336,15 @@ impl<D, T: Lift + Unpin + 'static> StreamProducer<D> for VecProducer<T> {
struct OneAtATime<T> {
destination: Arc<Mutex<Vec<T>>>,
- sleep: Pin<Box<dyn Future<Output = ()> + Send>>,
+ maybe_yield: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl<T> OneAtATime<T> {
fn new(destination: Arc<Mutex<Vec<T>>>, delay: bool) -> Self {
Self {
destination,
- sleep: if delay {
- tokio::time::sleep(Duration::from_millis(10)).boxed()
+ maybe_yield: if delay {
+ yield_times(5).boxed()
} else {
async {}.boxed()
},
@@ -363,9 +362,9 @@ impl<D, T: Lift + 'static> StreamConsumer<D> for OneAtATime<T> {
mut source: Source<Self::Item>,
_: bool,
) -> Poll<Result<StreamResult>> {
- let sleep = &mut self.as_mut().get_mut().sleep;
- task::ready!(sleep.as_mut().poll(cx));
- *sleep = async {}.boxed();
+ let maybe_yield = &mut self.as_mut().get_mut().maybe_yield;
+ task::ready!(maybe_yield.as_mut().poll(cx));
+ *maybe_yield = async {}.boxed();
let value = &mut None;
source.read(store, value)?;
diff --git a/crates/misc/component-async-tests/tests/scenario/transmit.rs b/crates/misc/component-async-tests/tests/scenario/transmit.rs
index 606dcc72ea..7f26200445 100644
--- a/crates/misc/component-async-tests/tests/scenario/transmit.rs
+++ b/crates/misc/component-async-tests/tests/scenario/transmit.rs
@@ -1,13 +1,12 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{self, Context, Poll};
-use std::time::Duration;
use super::util::{config, make_component, test_run, test_run_with_count};
use cancel::exports::local::local::cancel::Mode;
use component_async_tests::transmit::bindings::exports::local::local::transmit::Control;
use component_async_tests::util::{OneshotConsumer, OneshotProducer, PipeConsumer, PipeProducer};
-use component_async_tests::{Ctx, sleep, transmit};
+use component_async_tests::{Ctx, transmit, yield_};
use futures::{
FutureExt, SinkExt, StreamExt, TryStreamExt,
channel::{mpsc, oneshot},
@@ -120,7 +119,7 @@ impl<D> FutureConsumer<D> for ValueFutureConsumer {
struct DelayedStreamProducer<P> {
inner: P,
- sleep: Pin<Box<dyn Future<Output = ()> + Send>>,
+ maybe_yield: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl<D, P: StreamProducer<D>> StreamProducer<D> for DelayedStreamProducer<P> {
@@ -135,9 +134,9 @@ impl<D, P: StreamProducer<D>> StreamProducer<D> for DelayedStreamProducer<P> {
finish: bool,
) -> Poll<Result<StreamResult>> {
// SAFETY: We never move out of `self`.
- let sleep = unsafe { &mut self.as_mut().get_unchecked_mut().sleep };
- task::ready!(sleep.as_mut().poll(cx));
- *sleep = async {}.boxed();
+ let maybe_yield = unsafe { &mut self.as_mut().get_unchecked_mut().maybe_yield };
+ task::ready!(maybe_yield.as_mut().poll(cx));
+ *maybe_yield = async {}.boxed();
// SAFETY: This is a standard pin-projection, and we never move out
// of `self`.
@@ -148,7 +147,7 @@ impl<D, P: StreamProducer<D>> StreamProducer<D> for DelayedStreamProducer<P> {
struct DelayedStreamConsumer<C> {
inner: C,
- sleep: Pin<Box<dyn Future<Output = ()> + Send>>,
+ maybe_yield: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl<D, C: StreamConsumer<D>> StreamConsumer<D> for DelayedStreamConsumer<C> {
@@ -162,9 +161,9 @@ impl<D, C: StreamConsumer<D>> StreamConsumer<D> for DelayedStreamConsumer<C> {
finish: bool,
) -> Poll<Result<StreamResult>> {
// SAFETY: We never move out of `self`.
- let sleep = unsafe { &mut self.as_mut().get_unchecked_mut().sleep };
- task::ready!(sleep.as_mut().poll(cx));
- *sleep = async {}.boxed();
+ let maybe_yield = unsafe { &mut self.as_mut().get_unchecked_mut().maybe_yield };
+ task::ready!(maybe_yield.as_mut().poll(cx));
+ *maybe_yield = async {}.boxed();
// SAFETY: This is a standard pin-projection, and we never move out
// of `self`.
@@ -175,7 +174,7 @@ impl<D, C: StreamConsumer<D>> StreamConsumer<D> for DelayedStreamConsumer<C> {
struct DelayedFutureProducer<P> {
inner: P,
- sleep: Pin<Box<dyn Future<Output = ()> + Send>>,
+ maybe_yield: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl<D, P: FutureProducer<D>> FutureProducer<D> for DelayedFutureProducer<P> {
@@ -188,9 +187,9 @@ impl<D, P: FutureProducer<D>> FutureProducer<D> for DelayedFutureProducer<P> {
finish: bool,
) -> Poll<Result<Option<Self::Item>>> {
// SAFETY: We never move out of `self`.
- let sleep = unsafe { &mut self.as_mut().get_unchecked_mut().sleep };
- task::ready!(sleep.as_mut().poll(cx));
- *sleep = async {}.boxed();
+ let maybe_yield = unsafe { &mut self.as_mut().get_unchecked_mut().maybe_yield };
+ task::ready!(maybe_yield.as_mut().poll(cx));
+ *maybe_yield = async {}.boxed();
// SAFETY: This is a standard pin-projection, and we never move out
// of `self`.
@@ -201,7 +200,7 @@ impl<D, P: FutureProducer<D>> FutureProducer<D> for DelayedFutureProducer<P> {
struct DelayedFutureConsumer<C> {
inner: C,
- sleep: Pin<Box<dyn Future<Output = ()> + Send>>,
+ maybe_yield: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl<D, C: FutureConsumer<D>> FutureConsumer<D> for DelayedFutureConsumer<C> {
@@ -215,9 +214,9 @@ impl<D, C: FutureConsumer<D>> FutureConsumer<D> for DelayedFutureConsumer<C> {
finish: bool,
) -> Poll<Result<()>> {
// SAFETY: We never move out of `self`.
- let sleep = unsafe { &mut self.as_mut().get_unchecked_mut().sleep };
- task::ready!(sleep.as_mut().poll(cx));
- *sleep = async {}.boxed();
+ let maybe_yield = unsafe { &mut self.as_mut().get_unchecked_mut().maybe_yield };
+ task::ready!(maybe_yield.as_mut().poll(cx));
+ *maybe_yield = async {}.boxed();
// SAFETY: This is a standard pin-projection, and we never move out
// of `self`.
@@ -318,8 +317,10 @@ impl<D, C: FutureConsumer<D>> FutureConsumer<D> for ProcrastinatingFutureConsume
}
}
-fn sleep() -> Pin<Box<dyn Future<Output = ()> + Send>> {
- component_async_tests::util::sleep(Duration::from_millis(delay_millis())).boxed()
+async fn yield_times(n: usize) {
+ for _ in 0..n {
+ tokio::task::yield_now().await;
+ }
}
mod readiness {
@@ -360,7 +361,7 @@ pub async fn async_readiness() -> Result<()> {
inner: BufferStreamProducer {
buffer: expected.clone(),
},
- sleep: sleep(),
+ maybe_yield: yield_times(10).boxed(),
},
);
store
@@ -375,7 +376,7 @@ pub async fn async_readiness() -> Result<()> {
access,
DelayedStreamConsumer {
inner: BufferStreamConsumer { expected },
- sleep: sleep(),
+ maybe_yield: yield_times(10).boxed(),
},
)
});
@@ -447,16 +448,6 @@ pub async fn async_trap_cancel_host_after_return() -> Result<()> {
test_cancel_trap(Mode::TrapCancelHostAfterReturn).await
}
-fn delay_millis() -> u64 {
- // Miri-based builds are much slower to run, so we delay longer in that case
- // to ensure that async calls which the test expects to return `BLOCKED`
- // actually do so.
- //
- // TODO: Make this test (more) deterministic so that such tuning is not
- // necessary.
- if cfg!(miri) { 1000 } else { 10 }
-}
-
async fn test_cancel_trap(mode: Mode) -> Result<()> {
let message = "`subtask.cancel` called after terminal status delivered";
let trap = test_cancel(mode).await.unwrap_err();
@@ -482,7 +473,7 @@ async fn test_cancel(mode: Mode) -> Result<()> {
let mut linker = Linker::new(&engine);
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
- sleep::local::local::sleep::add_to_linker::<_, Ctx>(&mut linker, |ctx| ctx)?;
+ yield_::local::local::yield_::add_to_linker::<_, Ctx>(&mut linker, |ctx| ctx)?;
let mut store = Store::new(
&engine,
@@ -499,7 +490,7 @@ async fn test_cancel(mode: Mode) -> Result<()> {
.run_concurrent(async move |accessor| {
let ((), task) = cancel_host
.local_local_cancel()
- .call_run(accessor, mode, delay_millis())
+ .call_run(accessor, mode, 100)
.await?;
task.block(accessor).await;
Ok::<_, wasmtime::Error>(())
@@ -946,7 +937,7 @@ async fn test_synchronous_transmit(component: &str, procrastinate: bool) -> Resu
inner: BufferStreamProducer {
buffer: stream_expected.clone(),
},
- sleep: sleep(),
+ maybe_yield: yield_times(10).boxed(),
};
let stream = if procrastinate {
StreamReader::new(&mut store, ProcrastinatingStreamProducer(producer))
@@ -958,7 +949,7 @@ async fn test_synchronous_transmit(component: &str, procrastinate: bool) -> Resu
inner: ValueFutureProducer {
value: future_expected,
},
- sleep: sleep(),
+ maybe_yield: yield_times(10).boxed(),
};
let future = if procrastinate {
FutureReader::new(&mut store, ProcrastinatingFutureProducer(producer))
@@ -977,7 +968,7 @@ async fn test_synchronous_transmit(component: &str, procrastinate: bool) -> Resu
inner: BufferStreamConsumer {
expected: stream_expected,
},
- sleep: sleep(),
+ maybe_yield: yield_times(10).boxed(),
};
if procrastinate {
stream.pipe(&mut access, ProcrastinatingStreamConsumer(consumer));
@@ -988,7 +979,7 @@ async fn test_synchronous_transmit(component: &str, procrastinate: bool) -> Resu
inner: ValueFutureConsumer {
expected: future_expected,
},
- sleep: sleep(),
+ maybe_yield: yield_times(10).boxed(),
};
if procrastinate {
future.pipe(access, ProcrastinatingFutureConsumer(consumer));
diff --git a/crates/misc/component-async-tests/tests/scenario/util.rs b/crates/misc/component-async-tests/tests/scenario/util.rs
index 49af3a0fe7..817ba41323 100644
--- a/crates/misc/component-async-tests/tests/scenario/util.rs
+++ b/crates/misc/component-async-tests/tests/scenario/util.rs
@@ -5,7 +5,7 @@ use std::path::Path;
use std::sync::{Arc, LazyLock, Once};
use std::time::Duration;
-use component_async_tests::{Ctx, sleep};
+use component_async_tests::{Ctx, yield_};
use futures::stream::{FuturesUnordered, TryStreamExt};
use tokio::fs;
use tokio::sync::Mutex;
@@ -183,11 +183,11 @@ pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()
let mut linker = Linker::new(&engine);
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
- component_async_tests::yield_host::bindings::local::local::continue_::add_to_linker::<_, Ctx>(
+ component_async_tests::yield_runner::bindings::local::local::continue_::add_to_linker::<_, Ctx>(
&mut linker,
|ctx| ctx,
)?;
- component_async_tests::yield_host::bindings::local::local::ready::add_to_linker::<_, Ctx>(
+ component_async_tests::yield_runner::bindings::local::local::ready::add_to_linker::<_, Ctx>(
&mut linker,
|ctx| ctx,
)?;
@@ -195,7 +195,7 @@ pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()
_,
Ctx,
>(&mut linker, |ctx| ctx)?;
- sleep::local::local::sleep::add_to_linker::<_, Ctx>(&mut linker, |ctx| ctx)?;
+ yield_::local::local::yield_::add_to_linker::<_, Ctx>(&mut linker, |ctx| ctx)?;
let mut store = Store::new(
&engine,
@@ -215,17 +215,18 @@ pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()
});
}
- let yield_host = component_async_tests::yield_host::bindings::YieldHost::instantiate_async(
- &mut store, &component, &linker,
- )
- .await?;
+ let yield_runner =
+ component_async_tests::yield_runner::bindings::YieldRunner::instantiate_async(
+ &mut store, &component, &linker,
+ )
+ .await?;
// Start `count` concurrent calls and then join them all:
store
.run_concurrent(async |store| {
let mut futures = FuturesUnordered::new();
for _ in 0..count {
- futures.push(yield_host.local_local_run().call_run(store));
+ futures.push(yield_runner.local_local_run().call_run(store));
}
while let Some(()) = futures.try_next().await? {
diff --git a/crates/misc/component-async-tests/tests/test_all.rs b/crates/misc/component-async-tests/tests/test_all.rs
index 507f05148d..d1b3f9fe2f 100644
--- a/crates/misc/component-async-tests/tests/test_all.rs
+++ b/crates/misc/component-async-tests/tests/test_all.rs
@@ -17,8 +17,8 @@ use scenario::error_context::{
async_error_context, async_error_context_callee, async_error_context_caller,
};
use scenario::post_return::{
- async_post_return_callee, async_post_return_caller, async_sleep_post_return_callee,
- async_sleep_post_return_caller,
+ async_post_return_callee, async_post_return_caller, async_yield_post_return_callee,
+ async_yield_post_return_caller,
};
use scenario::read_resource_stream::async_read_resource_stream;
use scenario::round_trip::{
diff --git a/crates/misc/component-async-tests/wit/test.wit b/crates/misc/component-async-tests/wit/test.wit
index c43ce39251..7aee1b8ef6 100644
--- a/crates/misc/component-async-tests/wit/test.wit
+++ b/crates/misc/component-async-tests/wit/test.wit
@@ -134,11 +134,11 @@ interface closed {
read-future-post-return: async func(rx: future<u8>, expected: u8, rx-ignore: future<u8>);
}
-interface sleep {
- sleep-millis: async func(time-in-millis: u64);
+interface yield {
+ yield-times: async func(times: u64);
}
-interface sleep-with-options {
+interface yield-with-options {
use cancel.{mode};
variant on-cancel {
@@ -146,7 +146,7 @@ interface sleep-with-options {
task-cancel
}
- sleep-millis: async func(time-in-millis: u64, on-cancel: on-cancel, on-cancel-delay-millis: u64, synchronous-delay: bool, mode: mode);
+ yield-times: async func(times: u64, on-cancel: on-cancel, on-cancel-delay-times: u64, synchronous-delay: bool, mode: mode);
}
interface cancel {
@@ -160,7 +160,7 @@ interface cancel {
leak-task-after-cancel,
}
- run: async func(mode: mode, cancel-delay-millis: u64);
+ run: async func(mode: mode, cancel-delay-times: u64);
}
interface intertask {
@@ -175,8 +175,8 @@ interface synchronous-transmit {
start: async func(s: stream<u8>, s-expected: list<u8>, f: future<u8>, f-expected: u8) -> tuple<stream<u8>, list<u8>, future<u8>, u8>;
}
-interface sleep-post-return {
- run: async func(sleep-time-millis: u64);
+interface yield-post-return {
+ run: async func(times: u64);
}
interface closed-stream {
@@ -204,7 +204,7 @@ world yield-callee {
export run;
}
-world yield-host {
+world yield-runner {
import continue;
import ready;
export run;
@@ -311,22 +311,22 @@ world closed-streams {
export closed;
}
-world sleep-host {
- import sleep;
+world yield-host {
+ import yield;
}
world cancel-caller {
import backpressure;
- import sleep;
- import sleep-with-options;
+ import yield;
+ import yield-with-options;
export cancel;
}
world cancel-callee {
- import sleep;
+ import yield;
export backpressure;
- export sleep;
- export sleep-with-options;
+ export yield;
+ export yield-with-options;
}
world cancel-host {
@@ -346,15 +346,15 @@ world synchronous-transmit-guest {
export synchronous-transmit;
}
-world sleep-post-return-callee {
- import sleep;
- export sleep-post-return;
+world yield-post-return-callee {
+ import yield;
+ export yield-post-return;
}
-world sleep-post-return-caller {
- import sleep;
- import sleep-post-return;
- export sleep-post-return;
+world yield-post-return-caller {
+ import yield;
+ import yield-post-return;
+ export yield-post-return;
}
world closed-stream-guest {
diff --git a/crates/test-programs/src/bin/async_cancel_callee.rs b/crates/test-programs/src/bin/async_cancel_callee.rs
index f78d4c7b7a..b5bb723c16 100644
--- a/crates/test-programs/src/bin/async_cancel_callee.rs
+++ b/crates/test-programs/src/bin/async_cancel_callee.rs
@@ -12,35 +12,35 @@ use test_programs::async_::{
};
#[cfg(target_arch = "wasm32")]
-#[link(wasm_import_module = "[export]local:local/sleep-with-options")]
+#[link(wasm_import_module = "[export]local:local/yield-with-options")]
unsafe extern "C" {
- #[link_name = "[task-return]sleep-millis"]
- fn task_return_sleep_millis();
+ #[link_name = "[task-return]yield-times"]
+ fn task_return_yield_times();
}
#[cfg(not(target_arch = "wasm32"))]
-unsafe extern "C" fn task_return_sleep_millis() {
+unsafe extern "C" fn task_return_yield_times() {
unreachable!()
}
#[cfg(target_arch = "wasm32")]
-#[link(wasm_import_module = "local:local/sleep")]
+#[link(wasm_import_module = "local:local/yield")]
unsafe extern "C" {
- #[link_name = "sleep-millis"]
- fn sleep_millis(_: u64);
+ #[link_name = "yield-times"]
+ fn yield_times(_: u64);
}
#[cfg(not(target_arch = "wasm32"))]
-unsafe fn sleep_millis(_: u64) {
+unsafe fn yield_times(_: u64) {
unreachable!()
}
#[cfg(target_arch = "wasm32")]
-#[link(wasm_import_module = "local:local/sleep")]
+#[link(wasm_import_module = "local:local/yield")]
unsafe extern "C" {
- #[link_name = "[async-lower]sleep-millis"]
- fn sleep_millis_async(ms: u64) -> u32;
+ #[link_name = "[async-lower]yield-times"]
+ fn yield_times_async(ms: u64) -> u32;
}
#[cfg(not(target_arch = "wasm32"))]
-unsafe fn sleep_millis_async(_ms: u64) -> u32 {
+unsafe fn yield_times_async(_ms: u64) -> u32 {
unreachable!()
}
@@ -56,25 +56,25 @@ const MODE_TRAP_CANCEL_HOST_AFTER_RETURN: u8 = 5;
const MODE_LEAK_TASK_AFTER_CANCEL: u8 = 6;
#[derive(Clone, Copy)]
-struct SleepParams {
- time_in_millis: u64,
+struct YieldParams {
+ times: u64,
on_cancel: u8,
- on_cancel_delay_millis: u64,
+ on_cancel_delay_times: u64,
synchronous_delay: bool,
mode: u8,
}
enum State {
- S0(SleepParams),
+ S0(YieldParams),
S1 {
set: u32,
waitable: u32,
- params: SleepParams,
+ params: YieldParams,
},
S2 {
set: u32,
waitable: u32,
- params: SleepParams,
+ params: YieldParams,
},
}
@@ -97,38 +97,38 @@ unsafe extern "C" fn export_dec_backpressure() {
wit_bindgen::backpressure_dec();
}
-#[unsafe(export_name = "local:local/sleep#sleep-millis")]
-unsafe extern "C" fn export_sleep_sleep_millis(time_in_millis: u64) {
+#[unsafe(export_name = "local:local/yield#yield-times")]
+unsafe extern "C" fn export_yield_yield_times(times: u64) {
unsafe {
- sleep_millis(time_in_millis);
+ yield_times(times);
}
}
-#[unsafe(export_name = "[async-lift]local:local/sleep-with-options#sleep-millis")]
-unsafe extern "C" fn export_sleep_with_options_sleep_millis(
- time_in_millis: u64,
+#[unsafe(export_name = "[async-lift]local:local/yield-with-options#yield-times")]
+unsafe extern "C" fn export_yield_with_options_yield_times(
+ times: u64,
on_cancel: u8,
- on_cancel_delay_millis: u64,
+ on_cancel_delay_times: u64,
synchronous_delay: bool,
mode: u8,
) -> u32 {
unsafe {
context_set(
- u32::try_from(Box::into_raw(Box::new(State::S0(SleepParams {
- time_in_millis,
+ u32::try_from(Box::into_raw(Box::new(State::S0(YieldParams {
+ times,
on_cancel,
- on_cancel_delay_millis,
+ on_cancel_delay_times,
synchronous_delay,
mode,
}))) as usize)
.unwrap(),
);
- callback_sleep_with_options_sleep_millis(EVENT_NONE, 0, 0)
+ callback_yield_with_options_yield_times(EVENT_NONE, 0, 0)
}
}
-#[unsafe(export_name = "[callback][async-lift]local:local/sleep-with-options#sleep-millis")]
-unsafe extern "C" fn callback_sleep_with_options_sleep_millis(
+#[unsafe(export_name = "[callback][async-lift]local:local/yield-with-options#yield-times")]
+unsafe extern "C" fn callback_yield_with_options_yield_times(
event0: u32,
event1: u32,
event2: u32,
@@ -139,7 +139,7 @@ unsafe extern "C" fn callback_sleep_with_options_sleep_millis(
State::S0(params) => {
assert_eq!(event0, EVENT_NONE);
- let status = sleep_millis_async(params.time_in_millis);
+ let status = yield_times_async(params.times);
let waitable = status >> 4;
let status = status & 0xF;
@@ -182,26 +182,26 @@ unsafe extern "C" fn callback_sleep_with_options_sleep_millis(
subtask_drop(*waitable);
}
- if params.on_cancel_delay_millis == 0 {
+ if params.on_cancel_delay_times == 0 {
match params.on_cancel {
- ON_CANCEL_TASK_RETURN => task_return_sleep_millis(),
+ ON_CANCEL_TASK_RETURN => task_return_yield_times(),
ON_CANCEL_TASK_CANCEL => task_cancel(),
_ => unreachable!(),
}
CALLBACK_CODE_EXIT
} else if params.synchronous_delay {
- sleep_millis(params.on_cancel_delay_millis);
+ yield_times(params.on_cancel_delay_times);
match params.on_cancel {
- ON_CANCEL_TASK_RETURN => task_return_sleep_millis(),
+ ON_CANCEL_TASK_RETURN => task_return_yield_times(),
ON_CANCEL_TASK_CANCEL => task_cancel(),
_ => unreachable!(),
}
CALLBACK_CODE_EXIT
} else {
- let status = sleep_millis_async(params.on_cancel_delay_millis);
+ let status = yield_times_async(params.on_cancel_delay_times);
let waitable = status >> 4;
let status = status & 0xF;
@@ -242,7 +242,7 @@ unsafe extern "C" fn callback_sleep_with_options_sleep_millis(
waitable_set_drop(*set);
match params.on_cancel {
- ON_CANCEL_TASK_RETURN => task_return_sleep_millis(),
+ ON_CANCEL_TASK_RETURN => task_return_yield_times(),
ON_CANCEL_TASK_CANCEL => task_cancel(),
_ => unreachable!(),
}
diff --git a/crates/test-programs/src/bin/async_cancel_caller.rs b/crates/test-programs/src/bin/async_cancel_caller.rs
index cfaec732fc..e9b63bb1c2 100644
--- a/crates/test-programs/src/bin/async_cancel_caller.rs
+++ b/crates/test-programs/src/bin/async_cancel_caller.rs
@@ -40,28 +40,28 @@ unsafe fn dec_backpressure() {
unreachable!()
}
-mod sleep {
+mod yield_ {
#[cfg(target_arch = "wasm32")]
- #[link(wasm_import_module = "local:local/sleep")]
+ #[link(wasm_import_module = "local:local/yield")]
unsafe extern "C" {
- #[link_name = "[async-lower]sleep-millis"]
- pub fn sleep_millis(_: u64) -> u32;
+ #[link_name = "[async-lower]yield-times"]
+ pub fn yield_times(_: u64) -> u32;
}
#[cfg(not(target_arch = "wasm32"))]
- pub unsafe fn sleep_millis(_: u64) -> u32 {
+ pub unsafe fn yield_times(_: u64) -> u32 {
unreachable!()
}
}
-mod sleep_with_options {
+mod yield_with_options {
#[cfg(target_arch = "wasm32")]
- #[link(wasm_import_module = "local:local/sleep-with-options")]
+ #[link(wasm_import_module = "local:local/yield-with-options")]
unsafe extern "C" {
- #[link_name = "[async-lower]sleep-millis"]
- pub fn sleep_millis(_: *mut u8) -> u32;
+ #[link_name = "[async-lower]yield-times"]
+ pub fn yield_times(_: *mut u8) -> u32;
}
#[cfg(not(target_arch = "wasm32"))]
- pub unsafe fn sleep_millis(_: *mut u8) -> u32 {
+ pub unsafe fn yield_times(_: *mut u8) -> u32 {
unreachable!()
}
}
@@ -77,10 +77,10 @@ const _MODE_TRAP_CANCEL_HOST_AFTER_RETURN_CANCELLED: u8 = 4;
const _MODE_TRAP_CANCEL_HOST_AFTER_RETURN: u8 = 5;
#[repr(C)]
-struct SleepParams {
- time_in_millis: u64,
+struct YieldParams {
+ times: u64,
on_cancel: u8,
- on_cancel_delay_millis: u64,
+ on_cancel_delay_times: u64,
synchronous_delay: bool,
mode: u8,
}
@@ -88,39 +88,39 @@ struct SleepParams {
enum State {
S0 {
mode: u8,
- cancel_delay_millis: u64,
+ cancel_delay_times: u64,
},
S1 {
mode: u8,
set: u32,
waitable: u32,
- params: *mut SleepParams,
+ params: *mut YieldParams,
},
S2 {
mode: u8,
set: u32,
waitable: u32,
- params: *mut SleepParams,
+ params: *mut YieldParams,
},
S3 {
set: u32,
waitable: u32,
- params: *mut SleepParams,
+ params: *mut YieldParams,
},
S4 {
set: u32,
waitable: u32,
- params: *mut SleepParams,
+ params: *mut YieldParams,
},
}
#[unsafe(export_name = "[async-lift]local:local/cancel#run")]
-unsafe extern "C" fn export_run(mode: u8, cancel_delay_millis: u64) -> u32 {
+unsafe extern "C" fn export_run(mode: u8, cancel_delay_times: u64) -> u32 {
unsafe {
context_set(
u32::try_from(Box::into_raw(Box::new(State::S0 {
mode,
- cancel_delay_millis,
+ cancel_delay_times,
})) as usize)
.unwrap(),
);
@@ -135,25 +135,25 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
match state {
State::S0 {
mode,
- cancel_delay_millis,
+ cancel_delay_times,
} => {
assert_eq!(event0, EVENT_NONE);
- // First, call and cancel `sleep_with_options::sleep_millis`
+ // First, call and cancel `yield_with_options::yield_tiems`
// with backpressure enabled. Cancelling should not block since
// the call will not even have started.
inc_backpressure();
- let params = Box::into_raw(Box::new(SleepParams {
- time_in_millis: 60 * 60 * 1000,
+ let params = Box::into_raw(Box::new(YieldParams {
+ times: 60 * 60 * 1000,
on_cancel: ON_CANCEL_TASK_CANCEL,
- on_cancel_delay_millis: 0,
+ on_cancel_delay_times: 0,
synchronous_delay: false,
mode: *mode,
}));
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
@@ -173,13 +173,13 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
subtask_drop(waitable);
- // Next, call and cancel `sleep_with_options::sleep_millis` with
+ // Next, call and cancel `yield_with_options::yield_times` with
// backpressure disabled. Cancelling should not block since we
// specified zero cancel delay to the callee.
dec_backpressure();
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
@@ -199,13 +199,13 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
subtask_drop(waitable);
- // Next, call and cancel `sleep_with_options::sleep_millis` with
+ // Next, call and cancel `yield_with_options::yieldtimes` with
// a non-zero cancel delay. Cancelling _should_ block this
// time.
- (*params).on_cancel_delay_millis = *cancel_delay_millis;
+ (*params).on_cancel_delay_times = *cancel_delay_times;
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
@@ -242,14 +242,14 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
waitable_join(*waitable, 0);
subtask_drop(*waitable);
- // Next, call and cancel `sleep_with_options::sleep_millis` with
+ // Next, call and cancel `yield_with_options::yield_times` with
// a non-zero cancel delay, but this time specifying that the
// callee should call `task.return` instead of `task.cancel`.
// Cancelling _should_ block this time.
(**params).on_cancel = ON_CANCEL_TASK_RETURN;
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
@@ -293,14 +293,14 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
waitable_join(*waitable, 0);
subtask_drop(*waitable);
- // Next, call and cancel `sleep_with_options::sleep_millis` with
+ // Next, call and cancel `yield_with_options::yield_times` with
// a non-zero cancel delay, and specify that the callee should
// delay the cancel by making a synchronous call.
(**params).on_cancel = ON_CANCEL_TASK_CANCEL;
(**params).synchronous_delay = true;
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
@@ -343,13 +343,13 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
waitable_join(*waitable, 0);
subtask_drop(*waitable);
- // Next, call and cancel `sleep::sleep_millis`, which the callee
+ // Next, call and cancel `yield_::yield_times`, which the callee
// implements using both an synchronous lift and asynchronous
// lower. This should block asynchronously and yield a
// `STATUS_RETURNED` when complete since the callee cannot
// actually be cancelled.
- let status = sleep::sleep_millis(10);
+ let status = yield_::yield_times(10);
let waitable = status >> 4;
let status = status & 0xF;
@@ -386,7 +386,7 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
subtask_drop(*waitable);
waitable_set_drop(*set);
- // Next, call and cancel `sleep_with_options::sleep_millis` with
+ // Next, call and cancel `yield_with_options::yield_times` with
// a non-zero cancel delay, and specify that the callee should
// delay the cancel by making a synchronous call. Here we make
// synchronous call to `subtask.cancel`, which should block
@@ -394,7 +394,7 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
(**params).synchronous_delay = true;
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
@@ -413,7 +413,7 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
(**params).synchronous_delay = false;
- let status = sleep_with_options::sleep_millis(params.cast());
+ let status = yield_with_options::yield_times(params.cast());
let waitable = status >> 4;
let status = status & 0xF;
diff --git a/crates/test-programs/src/bin/async_sleep_post_return_callee.rs b/crates/test-programs/src/bin/async_yield_post_return_callee.rs
similarity index 62%
rename from crates/test-programs/src/bin/async_sleep_post_return_callee.rs
rename to crates/test-programs/src/bin/async_yield_post_return_callee.rs
index 82c0d60bb6..12379c54f7 100644
--- a/crates/test-programs/src/bin/async_sleep_post_return_callee.rs
+++ b/crates/test-programs/src/bin/async_yield_post_return_callee.rs
@@ -1,7 +1,7 @@
mod bindings {
wit_bindgen::generate!({
path: "../misc/component-async-tests/wit",
- world: "sleep-post-return-caller",
+ world: "yield-post-return-caller",
});
use super::Component;
@@ -9,18 +9,18 @@ mod bindings {
}
use {
- bindings::{exports::local::local::sleep_post_return::Guest, local::local::sleep},
+ bindings::{exports::local::local::yield_post_return::Guest, local::local::yield_},
wit_bindgen::rt::async_support,
};
struct Component;
impl Guest for Component {
- async fn run(sleep_time_millis: u64) {
+ async fn run(times: u64) {
// Spawn a task to run post-return and otherwise return immediately.
async_support::spawn(async move {
- // Sleep for as long as requested:
- sleep::sleep_millis(sleep_time_millis).await;
+ // Yield for as long as requested:
+ yield_::yield_times(times).await;
});
}
}
diff --git a/crates/test-programs/src/bin/async_sleep_post_return_caller.rs b/crates/test-programs/src/bin/async_yield_post_return_caller.rs
similarity index 56%
rename from crates/test-programs/src/bin/async_sleep_post_return_caller.rs
rename to crates/test-programs/src/bin/async_yield_post_return_caller.rs
index 2866212c05..c21d5fd28e 100644
--- a/crates/test-programs/src/bin/async_sleep_post_return_caller.rs
+++ b/crates/test-programs/src/bin/async_yield_post_return_caller.rs
@@ -1,7 +1,7 @@
mod bindings {
wit_bindgen::generate!({
path: "../misc/component-async-tests/wit",
- world: "sleep-post-return-caller",
+ world: "yield-post-return-caller",
});
use super::Component;
@@ -10,8 +10,8 @@ mod bindings {
use {
bindings::{
- exports::local::local::sleep_post_return::Guest,
- local::local::{sleep, sleep_post_return},
+ exports::local::local::yield_post_return::Guest,
+ local::local::{yield_, yield_post_return},
},
wit_bindgen::rt::async_support,
};
@@ -19,16 +19,16 @@ use {
struct Component;
impl Guest for Component {
- async fn run(sleep_time_millis: u64) {
+ async fn run(times: u64) {
// Spawn a task to run post-return and otherwise return immediately.
async_support::spawn(async move {
// Create a couple of subtasks which will also return immediately
- // and sleep post-return. These will not have completed once we
+ // and yield post-return. These will not have completed once we
// exit and thus will be reparented to the caller.
- sleep_post_return::run(sleep_time_millis * 2).await;
- sleep_post_return::run(sleep_time_millis * 2).await;
- // Sleep for as long as requested:
- sleep::sleep_millis(sleep_time_millis).await;
+ yield_post_return::run(times * 2).await;
+ yield_post_return::run(times * 2).await;
+ // Yield for as long as requested:
+ yield_::yield_times(times).await;
});
}
}