aboutsummaryrefslogtreecommitdiff
path: root/src/time/clock.rs
diff options
context:
space:
mode:
authorJoel Galenson <jgalenson@google.com>2020-10-23 09:39:31 -0700
committerJoel Galenson <jgalenson@google.com>2020-10-23 09:52:09 -0700
commitd5495b03381a3ebe0805db353d198b285b535b5c (patch)
tree778b8524d15fca8b73db0253ee0e1919d0848bb6 /src/time/clock.rs
parentba45c5bedf31df8562364c61d3dfb5262f10642e (diff)
downloadtokio-d5495b03381a3ebe0805db353d198b285b535b5c.tar.gz
Update to tokio-0.3.1 and add new features
Test: Build Change-Id: I5b5b9b386a21982a019653d0cf0bd3afc505cfac
Diffstat (limited to 'src/time/clock.rs')
-rw-r--r--src/time/clock.rs40
1 files changed, 33 insertions, 7 deletions
diff --git a/src/time/clock.rs b/src/time/clock.rs
index bd67d7a..fab7eca 100644
--- a/src/time/clock.rs
+++ b/src/time/clock.rs
@@ -1,3 +1,5 @@
+#![cfg_attr(not(feature = "rt"), allow(dead_code))]
+
//! Source of time abstraction.
//!
//! By default, `std::time::Instant::now()` is used. However, when the
@@ -36,7 +38,18 @@ cfg_not_test_util! {
cfg_test_util! {
use crate::time::{Duration, Instant};
use std::sync::{Arc, Mutex};
- use crate::runtime::context;
+
+ cfg_rt! {
+ fn clock() -> Option<Clock> {
+ crate::runtime::context::clock()
+ }
+ }
+
+ cfg_not_rt! {
+ fn clock() -> Option<Clock> {
+ None
+ }
+ }
/// A handle to a source of time.
#[derive(Debug, Clone)]
@@ -58,14 +71,14 @@ cfg_test_util! {
/// The current value of `Instant::now()` is saved and all subsequent calls
/// to `Instant::now()` until the timer wheel is checked again will return the saved value.
/// Once the timer wheel is checked, time will immediately advance to the next registered
- /// `Delay`. This is useful for running tests that depend on time.
+ /// `Sleep`. This is useful for running tests that depend on time.
///
/// # Panics
///
/// Panics if time is already frozen or if called from outside of the Tokio
/// runtime.
pub fn pause() {
- let clock = context::clock().expect("time cannot be frozen from outside the Tokio runtime");
+ let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
clock.pause();
}
@@ -79,7 +92,7 @@ cfg_test_util! {
/// Panics if time is not frozen or if called from outside of the Tokio
/// runtime.
pub fn resume() {
- let clock = context::clock().expect("time cannot be frozen from outside the Tokio runtime");
+ let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
let mut inner = clock.inner.lock().unwrap();
if inner.unfrozen.is_some() {
@@ -99,14 +112,27 @@ cfg_test_util! {
/// Panics if time is not frozen or if called from outside of the Tokio
/// runtime.
pub async fn advance(duration: Duration) {
- let clock = context::clock().expect("time cannot be frozen from outside the Tokio runtime");
+ use crate::future::poll_fn;
+ use std::task::Poll;
+
+ let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
clock.advance(duration);
- crate::task::yield_now().await;
+
+ let mut yielded = false;
+ poll_fn(|cx| {
+ if yielded {
+ Poll::Ready(())
+ } else {
+ yielded = true;
+ cx.waker().wake_by_ref();
+ Poll::Pending
+ }
+ }).await;
}
/// Return the current instant, factoring in frozen time.
pub(crate) fn now() -> Instant {
- if let Some(clock) = context::clock() {
+ if let Some(clock) = clock() {
clock.now()
} else {
Instant::from_std(std::time::Instant::now())