mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-23 00:00:10 +02:00
Rename Sleep to Delay (#270)
This patch renames `Sleep` from tokio-timer and the tokio facade to `Delay`. Given that the future does not actually put anything to sleep, the `Delay` name feels more appropriate. Fixes #263
This commit is contained in:
@@ -13,7 +13,7 @@ use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::time::Instant;
|
||||
use std::u64;
|
||||
|
||||
/// Internal state shared between a `Sleep` instance and the timer.
|
||||
/// Internal state shared between a `Delay` instance and the timer.
|
||||
///
|
||||
/// This struct is used as a node in two intrusive data structures:
|
||||
///
|
||||
@@ -27,7 +27,7 @@ use std::u64;
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Entry {
|
||||
/// Timer internals. Using a weak pointer allows the timer to shutdown
|
||||
/// without all `Sleep` instances having completed.
|
||||
/// without all `Delay` instances having completed.
|
||||
inner: Weak<Inner>,
|
||||
|
||||
/// Task to notify once the deadline is reached.
|
||||
@@ -49,7 +49,7 @@ pub(crate) struct Entry {
|
||||
/// counter.
|
||||
///
|
||||
/// One might think that it would be easier to just not create the `Entry`.
|
||||
/// The problem is that `Sleep` expects creating a `Registration` to always
|
||||
/// The problem is that `Delay` expects creating a `Registration` to always
|
||||
/// return a `Registration` instance. This simplifying factor allows it to
|
||||
/// improve the struct layout. To do this, we must always allocate the node.
|
||||
counted: bool,
|
||||
@@ -66,8 +66,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 `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
|
||||
/// 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
|
||||
/// 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.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use {Error, Sleep, Deadline, Interval};
|
||||
use {Error, Delay, Deadline, Interval};
|
||||
use timer::{Registration, Inner};
|
||||
|
||||
use tokio_executor::Enter;
|
||||
@@ -9,7 +9,7 @@ use std::time::{Duration, Instant};
|
||||
|
||||
/// Handle to timer instance.
|
||||
///
|
||||
/// The `Handle` allows creating `Sleep` instances that are driven by the
|
||||
/// The `Handle` allows creating `Delay` instances that are driven by the
|
||||
/// associated timer.
|
||||
///
|
||||
/// A `Handle` is obtained by calling [`Timer::handle`].
|
||||
@@ -25,14 +25,14 @@ thread_local!(static CURRENT_TIMER: RefCell<Option<Handle>> = RefCell::new(None)
|
||||
|
||||
/// Set the default timer for the duration of the closure.
|
||||
///
|
||||
/// From within the closure, [`Sleep`] instances that are created via
|
||||
/// [`Sleep::new`] can be used.
|
||||
/// From within the closure, [`Delay`] instances that are created via
|
||||
/// [`Delay::new`] can be used.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if there already is a default timer set.
|
||||
///
|
||||
/// [`Sleep`]: ../struct.Sleep.html
|
||||
/// [`Delay`]: ../struct.Delay.html
|
||||
pub fn with_default<F, R>(handle: &Handle, enter: &mut Enter, f: F) -> R
|
||||
where F: FnOnce(&mut Enter) -> R
|
||||
{
|
||||
@@ -77,7 +77,7 @@ impl Handle {
|
||||
///
|
||||
/// This function should only be called from within the context of
|
||||
/// [`with_default`]. Calling this function from outside of this context
|
||||
/// will return a `Handle` that does not reference a timer. `Sleep`
|
||||
/// will return a `Handle` that does not reference a timer. `Delay`
|
||||
/// instances created with this handle will error.
|
||||
///
|
||||
/// [`with_default`]: ../fn.with_default.html
|
||||
@@ -86,21 +86,21 @@ impl Handle {
|
||||
.unwrap_or(Handle { inner: Weak::new() })
|
||||
}
|
||||
|
||||
/// Create a `Sleep` driven by this handle's associated `Timer`.
|
||||
pub fn sleep(&self, deadline: Instant) -> Sleep {
|
||||
/// Create a `Delay` driven by this handle's associated `Timer`.
|
||||
pub fn delay(&self, deadline: Instant) -> Delay {
|
||||
let registration = Registration::new_with_handle(deadline, self.clone());
|
||||
Sleep::new_with_registration(deadline, registration)
|
||||
Delay::new_with_registration(deadline, registration)
|
||||
}
|
||||
|
||||
/// Create a `Deadline` driven by this handle's associated `Timer`.
|
||||
pub fn deadline<T>(&self, future: T, deadline: Instant) -> Deadline<T> {
|
||||
Deadline::new_with_sleep(future, self.sleep(deadline))
|
||||
Deadline::new_with_delay(future, self.delay(deadline))
|
||||
}
|
||||
|
||||
/// Create a new `Interval` that starts at `at` and yields every `duration`
|
||||
/// interval after that.
|
||||
pub fn interval(&self, at: Instant, duration: Duration) -> Interval {
|
||||
Interval::new_with_sleep(self.sleep(at), duration)
|
||||
Interval::new_with_delay(self.delay(at), duration)
|
||||
}
|
||||
|
||||
/// Try to get a handle to the current timer.
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
//! This module contains the types needed to run a timer.
|
||||
//!
|
||||
//! The [`Timer`] type runs the timer logic. It holds all the necessary state
|
||||
//! to track all associated [`Sleep`] instances and delivering notifications
|
||||
//! to track all associated [`Delay`] instances and delivering notifications
|
||||
//! once the deadlines are reached.
|
||||
//!
|
||||
//! The [`Handle`] type is a reference to a [`Timer`] instance. This type is
|
||||
//! `Clone`, `Send`, and `Sync`. This type is used to create instances of
|
||||
//! [`Sleep`].
|
||||
//! [`Delay`].
|
||||
//!
|
||||
//! The [`Now`] trait describes how to get an `Instance` representing the
|
||||
//! current moment in time. [`SystemNow`] is the default implementation, where
|
||||
@@ -23,7 +23,7 @@
|
||||
//!
|
||||
//! [`Timer`]: struct.Timer.html
|
||||
//! [`Handle`]: struct.Handle.html
|
||||
//! [`Sleep`]: ../struct.Sleep.html
|
||||
//! [`Delay`]: ../struct.Delay.html
|
||||
//! [`Now`]: trait.Now.html
|
||||
//! [`Now::now`]: trait.Now.html#method.now
|
||||
|
||||
@@ -52,16 +52,16 @@ use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::usize;
|
||||
|
||||
/// Timer implementation that drives [`Sleep`], [`Interval`], and [`Deadline`].
|
||||
/// Timer implementation that drives [`Delay`], [`Interval`], and [`Deadline`].
|
||||
///
|
||||
/// A `Timer` instance tracks the state necessary for managing time and
|
||||
/// notifying the [`Sleep`] instances once their deadlines are reached.
|
||||
/// notifying the [`Delay`] instances once their deadlines are reached.
|
||||
///
|
||||
/// It is expected that a single `Timer` instance manages many individual
|
||||
/// `Sleep` instances. The `Timer` implementation is thread-safe and, as such,
|
||||
/// `Delay` instances. The `Timer` implementation is thread-safe and, as such,
|
||||
/// is able to handle callers from across threads.
|
||||
///
|
||||
/// Callers do not use `Timer` directly to create `Sleep` instances. Instead,
|
||||
/// Callers do not use `Timer` directly to create `Delay` instances. Instead,
|
||||
/// [`Handle`] is used. A handle for the timer instance is obtained by calling
|
||||
/// [`handle`]. [`Handle`] is the type that implements `Clone` and is `Send +
|
||||
/// Sync`.
|
||||
@@ -73,9 +73,9 @@ use std::usize;
|
||||
/// The `Timer` has a resolution of one millisecond. Any unit of time that falls
|
||||
/// between milliseconds are rounded up to the next millisecond.
|
||||
///
|
||||
/// When the `Timer` instance is dropped, any outstanding `Sleep` instance that
|
||||
/// When the `Timer` instance is dropped, any outstanding `Delay` instance that
|
||||
/// has not elapsed will be notified with an error. At this point, calling
|
||||
/// `poll` on the sleep instance will result in `Err` being returned.
|
||||
/// `poll` on the `Delay` instance will result in `Err` being returned.
|
||||
///
|
||||
/// # Implementation
|
||||
///
|
||||
@@ -102,13 +102,13 @@ use std::usize;
|
||||
/// * Level 5: 64 x ~12 day slots.
|
||||
///
|
||||
/// When the timer processes entries at level zero, it will notify all the
|
||||
/// [`Sleep`] instances as their deadlines have been reached. For all higher
|
||||
/// [`Delay`] 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 will `Sleep` instances will
|
||||
/// down. Eventually, as time progresses, entries will `Delay` instances will
|
||||
/// either be canceled (dropped) or their associated entries will reach level
|
||||
/// zero and be notified.
|
||||
///
|
||||
/// [`Sleep`]: ../struct.Sleep.html
|
||||
/// [`Delay`]: ../struct.Delay.html
|
||||
/// [`Interval`]: ../struct.Interval.html
|
||||
/// [`Deadline`]: ../struct.Deadline.html
|
||||
/// [paper]: http://www.cs.columbia.edu/~nahum/w6998/papers/ton97-timing-wheels.pdf
|
||||
@@ -172,7 +172,7 @@ pub(crate) struct Inner {
|
||||
/// precision of 1 millisecond.
|
||||
const NUM_LEVELS: usize = 6;
|
||||
|
||||
/// The maximum duration of a sleep
|
||||
/// The maximum duration of a delay
|
||||
const MAX_DURATION: u64 = 1 << (6 * NUM_LEVELS);
|
||||
|
||||
/// Maximum number of timeouts the system can handle concurrently.
|
||||
@@ -187,7 +187,7 @@ where T: Park
|
||||
/// thread.
|
||||
///
|
||||
/// Once the timer has been created, a handle can be obtained using
|
||||
/// [`handle`]. The handle is used to create `Sleep` instances.
|
||||
/// [`handle`]. The handle is used to create `Delay` instances.
|
||||
///
|
||||
/// Use `default` when constructing a `Timer` using the default `park`
|
||||
/// instance.
|
||||
@@ -236,7 +236,7 @@ where T: Park,
|
||||
|
||||
/// Returns a handle to the timer.
|
||||
///
|
||||
/// The `Handle` is how `Sleep` instances are created. The `Sleep` instances
|
||||
/// The `Handle` is how `Delay` instances are created. The `Delay` 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.
|
||||
@@ -250,7 +250,7 @@ where T: Park,
|
||||
/// instance to make progress. This is where the work happens.
|
||||
///
|
||||
/// The `Timer` will use the `Park` instance that was specified in [`new`]
|
||||
/// to block the current thread until the next `Sleep` instance elapses. One
|
||||
/// to block the current thread until the next `Delay` instance elapses. One
|
||||
/// call to `turn` results in at most one call to `park.park()`.
|
||||
///
|
||||
/// # Return
|
||||
|
||||
@@ -8,7 +8,7 @@ use std::time::Instant;
|
||||
|
||||
/// Registration with a timer.
|
||||
///
|
||||
/// The association between a `Sleep` instance and a timer is done lazily in
|
||||
/// The association between a `Delay` instance and a timer is done lazily in
|
||||
/// `poll`
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Registration {
|
||||
|
||||
Reference in New Issue
Block a user