Skip to content

Commit

Permalink
time: rename Delay future to Sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
alce committed Oct 9, 2020
1 parent b704c53 commit a307f1e
Show file tree
Hide file tree
Showing 25 changed files with 186 additions and 186 deletions.
4 changes: 2 additions & 2 deletions tokio-test/src/io.rs
Expand Up @@ -20,7 +20,7 @@

use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::sync::mpsc;
use tokio::time::{self, Delay, Duration, Instant};
use tokio::time::{self, Duration, Instant, Sleep};

use futures_core::ready;
use std::collections::VecDeque;
Expand Down Expand Up @@ -67,7 +67,7 @@ enum Action {
struct Inner {
actions: VecDeque<Action>,
waiting: Option<Instant>,
sleep: Option<Delay>,
sleep: Option<Sleep>,
read_wait: Option<Waker>,
rx: mpsc::UnboundedReceiver<Action>,
}
Expand Down
2 changes: 1 addition & 1 deletion tokio-test/tests/block_on.rs
Expand Up @@ -18,7 +18,7 @@ fn async_fn() {
}

#[test]
fn test_delay() {
fn test_sleep() {
let deadline = Instant::now() + Duration::from_millis(100);

block_on(async {
Expand Down
4 changes: 2 additions & 2 deletions tokio-util/src/time/delay_queue.rs
Expand Up @@ -7,7 +7,7 @@
use crate::time::wheel::{self, Wheel};

use futures_core::ready;
use tokio::time::{sleep_until, Delay, Duration, Error, Instant};
use tokio::time::{sleep_until, Duration, Error, Instant, Sleep};

use slab::Slab;
use std::cmp;
Expand Down Expand Up @@ -138,7 +138,7 @@ pub struct DelayQueue<T> {
expired: Stack<T>,

/// Delay expiring when the *first* item in the queue expires
delay: Option<Delay>,
delay: Option<Sleep>,

/// Wheel polling state
wheel_now: u64,
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/lib.rs
Expand Up @@ -25,7 +25,7 @@
//! provides a few major components:
//!
//! * Tools for [working with asynchronous tasks][tasks], including
//! [synchronization primitives and channels][sync] and [timeouts, delays, and
//! [synchronization primitives and channels][sync] and [timeouts, sleeps, and
//! intervals][time].
//! * APIs for [performing asynchronous I/O][io], including [TCP and UDP][net] sockets,
//! [filesystem][fs] operations, and [process] and [signal] management.
Expand Down Expand Up @@ -183,13 +183,13 @@
//!
//! The [`tokio::time`] module provides utilities for tracking time and
//! scheduling work. This includes functions for setting [timeouts][timeout] for
//! tasks, [delaying][delay] work to run in the future, or [repeating an operation at an
//! tasks, [sleeping][sleep] work to run in the future, or [repeating an operation at an
//! interval][interval].
//!
//! In order to use `tokio::time`, the "time" feature flag must be enabled.
//!
//! [`tokio::time`]: crate::time
//! [delay]: crate::time::sleep()
//! [sleep]: crate::time::sleep()
//! [interval]: crate::time::interval()
//! [timeout]: crate::time::timeout()
//!
Expand Down
26 changes: 13 additions & 13 deletions tokio/src/macros/select.rs
Expand Up @@ -63,9 +63,9 @@
/// Given that `if` preconditions are used to disable `select!` branches, some
/// caution must be used to avoid missing values.
///
/// For example, here is **incorrect** usage of `delay` with `if`. The objective
/// For example, here is **incorrect** usage of `sleep` with `if`. The objective
/// is to repeatedly run an asynchronous task for up to 50 milliseconds.
/// However, there is a potential for the `delay` completion to be missed.
/// However, there is a potential for the `sleep` completion to be missed.
///
/// ```no_run
/// use tokio::time::{self, Duration};
Expand All @@ -76,11 +76,11 @@
///
/// #[tokio::main]
/// async fn main() {
/// let mut delay = time::sleep(Duration::from_millis(50));
/// let mut sleep = time::sleep(Duration::from_millis(50));
///
/// while !delay.is_elapsed() {
/// while !sleep.is_elapsed() {
/// tokio::select! {
/// _ = &mut delay, if !delay.is_elapsed() => {
/// _ = &mut sleep, if !sleep.is_elapsed() => {
/// println!("operation timed out");
/// }
/// _ = some_async_work() => {
Expand All @@ -91,11 +91,11 @@
/// }
/// ```
///
/// In the above example, `delay.is_elapsed()` may return `true` even if
/// `delay.poll()` never returned `Ready`. This opens up a potential race
/// condition where `delay` expires between the `while !delay.is_elapsed()`
/// In the above example, `sleep.is_elapsed()` may return `true` even if
/// `sleep.poll()` never returned `Ready`. This opens up a potential race
/// condition where `sleep` expires between the `while !sleep.is_elapsed()`
/// check and the call to `select!` resulting in the `some_async_work()` call to
/// run uninterrupted despite the delay having elapsed.
/// run uninterrupted despite the sleep having elapsed.
///
/// One way to write the above example without the race would be:
///
Expand All @@ -109,11 +109,11 @@
///
/// #[tokio::main]
/// async fn main() {
/// let mut delay = time::sleep(Duration::from_millis(50));
/// let mut sleep = time::sleep(Duration::from_millis(50));
///
/// loop {
/// tokio::select! {
/// _ = &mut delay => {
/// _ = &mut sleep => {
/// println!("operation timed out");
/// break;
/// }
Expand Down Expand Up @@ -226,7 +226,7 @@
/// #[tokio::main]
/// async fn main() {
/// let mut stream = stream::iter(vec![1, 2, 3]);
/// let mut delay = time::sleep(Duration::from_secs(1));
/// let mut sleep = time::sleep(Duration::from_secs(1));
///
/// loop {
/// tokio::select! {
Expand All @@ -237,7 +237,7 @@
/// break;
/// }
/// }
/// _ = &mut delay => {
/// _ = &mut sleep => {
/// println!("timeout");
/// break;
/// }
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/handle.rs
Expand Up @@ -28,7 +28,7 @@ pub(crate) struct Handle {

impl Handle {
/// Enter the runtime context. This allows you to construct types that must
/// have an executor available on creation such as [`Delay`] or [`TcpStream`].
/// have an executor available on creation such as [`Sleep`] or [`TcpStream`].
/// It will also allow you to call methods such as [`tokio::spawn`].
pub(crate) fn enter<F, R>(&self, f: F) -> R
where
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/mod.rs
Expand Up @@ -450,10 +450,10 @@ impl Runtime {
}

/// Enter the runtime context. This allows you to construct types that must
/// have an executor available on creation such as [`Delay`] or [`TcpStream`].
/// have an executor available on creation such as [`Sleep`] or [`TcpStream`].
/// It will also allow you to call methods such as [`tokio::spawn`].
///
/// [`Delay`]: struct@crate::time::Delay
/// [`Sleep`]: struct@crate::time::Sleep
/// [`TcpStream`]: struct@crate::net::TcpStream
/// [`tokio::spawn`]: fn@crate::spawn
///
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/stream/throttle.rs
@@ -1,7 +1,7 @@
//! Slow down a stream by enforcing a delay between items.

use crate::stream::Stream;
use crate::time::{Delay, Duration, Instant};
use crate::time::{Duration, Instant, Sleep};

use std::future::Future;
use std::marker::Unpin;
Expand All @@ -17,7 +17,7 @@ where
let delay = if duration == Duration::from_millis(0) {
None
} else {
Some(Delay::new_timeout(Instant::now() + duration, duration))
Some(Sleep::new_timeout(Instant::now() + duration, duration))
};

Throttle {
Expand All @@ -34,7 +34,7 @@ pin_project! {
#[must_use = "streams do nothing unless polled"]
pub struct Throttle<T> {
// `None` when duration is zero.
delay: Option<Delay>,
delay: Option<Sleep>,
duration: Duration,

// Set to true when `delay` has returned ready, but `stream` hasn't.
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/stream/timeout.rs
@@ -1,5 +1,5 @@
use crate::stream::{Fuse, Stream};
use crate::time::{Delay, Elapsed, Instant};
use crate::time::{Elapsed, Instant, Sleep};

use core::future::Future;
use core::pin::Pin;
Expand All @@ -14,7 +14,7 @@ pin_project! {
pub struct Timeout<S> {
#[pin]
stream: Fuse<S>,
deadline: Delay,
deadline: Sleep,
duration: Duration,
poll_deadline: bool,
}
Expand All @@ -23,7 +23,7 @@ pin_project! {
impl<S: Stream> Timeout<S> {
pub(super) fn new(stream: S, duration: Duration) -> Self {
let next = Instant::now() + duration;
let deadline = Delay::new_timeout(next, duration);
let deadline = Sleep::new_timeout(next, duration);

Timeout {
stream: Fuse::new(stream),
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/sync/mod.rs
Expand Up @@ -359,26 +359,26 @@
//! let mut conf = rx.borrow().clone();
//!
//! let mut op_start = Instant::now();
//! let mut delay = time::sleep_until(op_start + conf.timeout);
//! let mut sleep = time::sleep_until(op_start + conf.timeout);
//!
//! loop {
//! tokio::select! {
//! _ = &mut delay => {
//! _ = &mut sleep => {
//! // The operation elapsed. Restart it
//! op.set(my_async_operation());
//!
//! // Track the new start time
//! op_start = Instant::now();
//!
//! // Restart the timeout
//! delay = time::sleep_until(op_start + conf.timeout);
//! sleep = time::sleep_until(op_start + conf.timeout);
//! }
//! _ = rx.changed() => {
//! conf = rx.borrow().clone();
//!
//! // The configuration has been updated. Update the
//! // `delay` using the new `timeout` value.
//! delay.reset(op_start + conf.timeout);
//! // `sleep` using the new `timeout` value.
//! sleep.reset(op_start + conf.timeout);
//! }
//! _ = &mut op => {
//! // The operation completed!
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/clock.rs
Expand Up @@ -58,7 +58,7 @@ 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
///
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/time/driver/entry.rs
Expand Up @@ -11,7 +11,7 @@ use std::sync::{Arc, Weak};
use std::task::{self, Poll};
use std::u64;

/// Internal state shared between a `Delay` instance and the timer.
/// Internal state shared between a `Sleep` instance and the timer.
///
/// This struct is used as a node in two intrusive data structures:
///
Expand All @@ -28,7 +28,7 @@ pub(crate) struct Entry {
time: CachePadded<UnsafeCell<Time>>,

/// Timer internals. Using a weak pointer allows the timer to shutdown
/// without all `Delay` instances having completed.
/// without all `Sleep` instances having completed.
///
/// When empty, it means that the entry has not yet been linked with a
/// timer instance.
Expand Down Expand Up @@ -69,8 +69,8 @@ pub(crate) struct Entry {
/// When the entry expires, relative to the `start` of the timer
/// (Inner::start). This is only used by the timer.
///
/// A `Delay` instance can be reset to a different deadline by the thread
/// that owns the `Delay` instance. In this case, the timer thread will not
/// A `Sleep` instance can be reset to a different deadline by the thread
/// that owns the `Sleep` instance. In this case, the timer thread will not
/// immediately know that this has happened. The timer thread must know the
/// last deadline that it saw as it uses this value to locate the entry in
/// its wheel.
Expand All @@ -94,7 +94,7 @@ pub(crate) struct Entry {
pub(crate) prev_stack: UnsafeCell<*const Entry>,
}

/// Stores the info for `Delay`.
/// Stores the info for `Sleep`.
#[derive(Debug)]
pub(crate) struct Time {
pub(crate) deadline: Instant,
Expand Down
18 changes: 9 additions & 9 deletions tokio/src/time/driver/mod.rs
Expand Up @@ -20,12 +20,12 @@ use std::sync::Arc;
use std::usize;
use std::{cmp, fmt};

/// Time implementation that drives [`Delay`][delay], [`Interval`][interval], and [`Timeout`][timeout].
/// Time implementation that drives [`Sleep`][sleep], [`Interval`][interval], and [`Timeout`][timeout].
///
/// A `Driver` instance tracks the state necessary for managing time and
/// notifying the [`Delay`][delay] instances once their deadlines are reached.
/// notifying the [`Sleep`][sleep] instances once their deadlines are reached.
///
/// It is expected that a single instance manages many individual [`Delay`][delay]
/// It is expected that a single instance manages many individual [`Sleep`][sleep]
/// instances. The `Driver` implementation is thread-safe and, as such, is able
/// to handle callers from across threads.
///
Expand All @@ -36,9 +36,9 @@ use std::{cmp, fmt};
/// The driver has a resolution of one millisecond. Any unit of time that falls
/// between milliseconds are rounded up to the next millisecond.
///
/// When an instance is dropped, any outstanding [`Delay`][delay] instance that has not
/// When an instance is dropped, any outstanding [`Sleep`][sleep] instance that has not
/// elapsed will be notified with an error. At this point, calling `poll` on the
/// [`Delay`][delay] instance will result in panic.
/// [`Sleep`][sleep] instance will result in panic.
///
/// # Implementation
///
Expand All @@ -65,14 +65,14 @@ use std::{cmp, fmt};
/// * Level 5: 64 x ~12 day slots.
///
/// When the timer processes entries at level zero, it will notify all the
/// `Delay` instances as their deadlines have been reached. For all higher
/// `Sleep` instances as their deadlines have been reached. For all higher
/// levels, all entries will be redistributed across the wheel at the next level
/// down. Eventually, as time progresses, entries with [`Delay`][delay] instances will
/// down. Eventually, as time progresses, entries with [`Sleep`][sleep] instances will
/// either be canceled (dropped) or their associated entries will reach level
/// zero and be notified.
///
/// [paper]: http://www.cs.columbia.edu/~nahum/w6998/papers/ton97-timing-wheels.pdf
/// [delay]: crate::time::Delay
/// [sleep]: crate::time::Sleep
/// [timeout]: crate::time::Timeout
/// [interval]: crate::time::Interval
#[derive(Debug)]
Expand Down Expand Up @@ -138,7 +138,7 @@ where

/// Returns a handle to the timer.
///
/// The `Handle` is how `Delay` instances are created. The `Delay` instances
/// The `Handle` is how `Sleep` instances are created. The `Sleep` instances
/// can either be created directly or the `Handle` instance can be passed to
/// `with_default`, setting the timer as the default timer for the execution
/// context.
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/error.rs
Expand Up @@ -13,7 +13,7 @@ use std::fmt;
/// succeed in the future.
///
/// * `at_capacity` occurs when a timer operation is attempted, but the timer
/// instance is currently handling its maximum number of outstanding delays.
/// instance is currently handling its maximum number of outstanding sleep instances.
/// In this case, the operation is not able to be performed at the current
/// moment, and `at_capacity` is returned. This is a transient error, i.e., at
/// some point in the future, if the operation is attempted again, it might
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/time/interval.rs
@@ -1,5 +1,5 @@
use crate::future::poll_fn;
use crate::time::{sleep_until, Delay, Duration, Instant};
use crate::time::{sleep_until, Duration, Instant, Sleep};

use std::future::Future;
use std::pin::Pin;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn interval_at(start: Instant, period: Duration) -> Interval {
#[derive(Debug)]
pub struct Interval {
/// Future that completes the next time the `Interval` yields a value.
delay: Delay,
delay: Sleep,

/// The duration between values yielded by `Interval`.
period: Duration,
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/time/mod.rs
Expand Up @@ -3,7 +3,7 @@
//! This module provides a number of types for executing code after a set period
//! of time.
//!
//! * `Delay` is a future that does no work and completes at a specific `Instant`
//! * `Sleep` is a future that does no work and completes at a specific `Instant`
//! in time.
//!
//! * `Interval` is a stream yielding a value at a fixed period. It is
Expand Down Expand Up @@ -93,8 +93,8 @@ pub(crate) use self::clock::Clock;
#[cfg(feature = "test-util")]
pub use clock::{advance, pause, resume};

mod delay;
pub use delay::{sleep, sleep_until, Delay};
mod sleep;
pub use sleep::{sleep, sleep_until, Sleep};

pub(crate) mod driver;

Expand Down

0 comments on commit a307f1e

Please sign in to comment.