Moves when to Timeout from TimeoutToken

This commit is contained in:
Paul Colomiets
2016-10-06 21:11:46 +03:00
parent a99b2529e0
commit 411caa786d
3 changed files with 9 additions and 18 deletions
+2 -2
View File
@@ -451,7 +451,7 @@ impl Inner {
} }
} }
fn add_timeout(&mut self, at: Instant) -> io::Result<(usize, Instant)> { fn add_timeout(&mut self, at: Instant) -> usize {
if self.timeouts.vacant_entry().is_none() { if self.timeouts.vacant_entry().is_none() {
let len = self.timeouts.len(); let len = self.timeouts.len();
self.timeouts.reserve_exact(len); self.timeouts.reserve_exact(len);
@@ -460,7 +460,7 @@ impl Inner {
let slot = self.timer_heap.push((at, entry.index())); let slot = self.timer_heap.push((at, entry.index()));
let entry = entry.insert((Some(slot), TimeoutState::NotFired)); let entry = entry.insert((Some(slot), TimeoutState::NotFired));
debug!("added a timeout: {}", entry.index()); debug!("added a timeout: {}", entry.index());
Ok((entry.index(), at)) return entry.index();
} }
fn update_timeout(&mut self, token: usize, handle: Task) -> Option<Task> { fn update_timeout(&mut self, token: usize, handle: Task) -> Option<Task> {
+5 -3
View File
@@ -13,13 +13,14 @@ use reactor::timeout_token::TimeoutToken;
/// A future representing the notification that a timeout has occurred. /// A future representing the notification that a timeout has occurred.
/// ///
/// Timeouts are created through the `LoopHandle::timeout` or /// Timeouts are created through the `Timeout::new` or
/// `LoopHandle::timeout_at` methods indicating when a timeout should fire at. /// `Timeout::new_at` methods indicating when a timeout should fire at.
/// Note that timeouts are not intended for high resolution timers, but rather /// Note that timeouts are not intended for high resolution timers, but rather
/// they will likely fire some granularity after the exact instant that they're /// they will likely fire some granularity after the exact instant that they're
/// otherwise indicated to fire at. /// otherwise indicated to fire at.
pub struct Timeout { pub struct Timeout {
token: TimeoutToken, token: TimeoutToken,
when: Instant,
handle: Remote, handle: Remote,
} }
@@ -41,6 +42,7 @@ impl Timeout {
pub fn new_at(at: Instant, handle: &Handle) -> io::Result<Timeout> { pub fn new_at(at: Instant, handle: &Handle) -> io::Result<Timeout> {
Ok(Timeout { Ok(Timeout {
token: try!(TimeoutToken::new(at, &handle)), token: try!(TimeoutToken::new(at, &handle)),
when: at,
handle: handle.remote().clone(), handle: handle.remote().clone(),
}) })
} }
@@ -53,7 +55,7 @@ impl Future for Timeout {
fn poll(&mut self) -> Poll<(), io::Error> { fn poll(&mut self) -> Poll<(), io::Error> {
// TODO: is this fast enough? // TODO: is this fast enough?
let now = Instant::now(); let now = Instant::now();
if *self.token.when() <= now { if self.when <= now {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} else { } else {
self.token.update_timeout(&self.handle); self.token.update_timeout(&self.handle);
+2 -13
View File
@@ -8,7 +8,6 @@ use reactor::{Message, Handle, Remote};
/// A token that identifies an active timeout. /// A token that identifies an active timeout.
pub struct TimeoutToken { pub struct TimeoutToken {
token: usize, token: usize,
when: Instant,
} }
impl TimeoutToken { impl TimeoutToken {
@@ -17,23 +16,13 @@ impl TimeoutToken {
pub fn new(at: Instant, handle: &Handle) -> io::Result<TimeoutToken> { pub fn new(at: Instant, handle: &Handle) -> io::Result<TimeoutToken> {
match handle.inner.upgrade() { match handle.inner.upgrade() {
Some(inner) => { Some(inner) => {
let (token, when) = try!(inner.borrow_mut().add_timeout(at)); let token = inner.borrow_mut().add_timeout(at);
Ok(TimeoutToken { token: token, when: when }) Ok(TimeoutToken { token: token })
} }
None => Err(io::Error::new(io::ErrorKind::Other, "event loop gone")), None => Err(io::Error::new(io::ErrorKind::Other, "event loop gone")),
} }
} }
/// Returns the instant in time when this timeout token will "fire".
///
/// Note that this instant may *not* be the instant that was passed in when
/// the timeout was created. The event loop does not support high resolution
/// timers, so the exact resolution of when a timeout may fire may be
/// slightly fudged.
pub fn when(&self) -> &Instant {
&self.when
}
/// Updates a previously added timeout to notify a new task instead. /// Updates a previously added timeout to notify a new task instead.
/// ///
/// # Panics /// # Panics