Skip to content

Commit 5c9c034

Browse files
committed
add yeild and now apis
1 parent 9f58cec commit 5c9c034

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

crates/runtime/src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,22 @@ impl Handle {
346346
}
347347
}
348348

349+
pub async fn yield_now(&self) {
350+
match self {
351+
Self::Tokio(_) => tokio::task::yield_now().await,
352+
#[cfg(feature = "simulation")]
353+
Self::Simulation(handle) => handle.yield_now().await,
354+
}
355+
}
356+
357+
pub fn now(&self) -> Duration {
358+
match self {
359+
Self::Tokio(_) => panic!("Handle::now is only supported by the simulation runtime"),
360+
#[cfg(feature = "simulation")]
361+
Self::Simulation(handle) => handle.now(),
362+
}
363+
}
364+
349365
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
350366
match self {
351367
Self::Tokio(handle) => tokio::task::block_in_place(|| handle.block_on(future)),
@@ -381,10 +397,7 @@ mod tests {
381397
drop(jh);
382398

383399
// Yield so the spawned task gets polled.
384-
handle
385-
.timeout(std::time::Duration::from_millis(50), async {})
386-
.await
387-
.ok();
400+
handle.yield_now().await;
388401
});
389402

390403
assert!(flag.load(Ordering::Acquire));

crates/runtime/src/sim/executor/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ impl Handle {
352352
self.executor.time.timeout(duration, future).await
353353
}
354354

355+
/// Yield this task back to the simulation scheduler once.
356+
pub async fn yield_now(&self) {
357+
yield_now().await
358+
}
359+
355360
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
356361
self.executor.block_on(future)
357362
}

0 commit comments

Comments
 (0)