Fix lowres timer restart

This commit is contained in:
Nikolay Kim 2021-09-01 22:24:13 +06:00
parent 4c5cd29fb7
commit a87c3855c8
3 changed files with 25 additions and 17 deletions

View file

@ -1,5 +1,9 @@
# Changes # Changes
## [0.4.0-b.10] - 2021-09-01
* Fix lowres timer restart
## [0.4.0-b.9] - 2021-09-01 ## [0.4.0-b.9] - 2021-09-01
* More timer wheel cleanups on driver drop * More timer wheel cleanups on driver drop

View file

@ -1,6 +1,6 @@
[package] [package]
name = "ntex" name = "ntex"
version = "0.4.0-b.9" version = "0.4.0-b.10"
authors = ["ntex contributors <team@ntex.rs>"] authors = ["ntex contributors <team@ntex.rs>"]
description = "Framework for composable network services" description = "Framework for composable network services"
readme = "README.md" readme = "README.md"

View file

@ -63,7 +63,7 @@ const ONE_MS: time::Duration = time::Duration::from_millis(1);
/// Resolution is ~1ms /// Resolution is ~1ms
#[inline] #[inline]
pub fn now() -> time::Instant { pub fn now() -> time::Instant {
TIMER.with(|t| t.0.borrow().now()) TIMER.with(|t| t.0.borrow_mut().now(&t.0))
} }
/// Returns the system time corresponding to “now”. /// Returns the system time corresponding to “now”.
@ -117,6 +117,7 @@ bitflags::bitflags! {
const NEEDS_RECALC = 0b0000_0010; const NEEDS_RECALC = 0b0000_0010;
const TIMER_ACTIVE = 0b0000_0100; const TIMER_ACTIVE = 0b0000_0100;
const LOWRES_TIMER = 0b0000_1000; const LOWRES_TIMER = 0b0000_1000;
const LOWRES_DRIVER = 0b0001_0000;
} }
} }
@ -143,9 +144,7 @@ struct TimerInner {
impl Timer { impl Timer {
fn new() -> Self { fn new() -> Self {
let inner = Rc::new(RefCell::new(TimerInner::new())); Timer(Rc::new(RefCell::new(TimerInner::new())))
LowresTimerDriver::start(&inner);
Timer(inner)
} }
fn with_entry<F, R>(no: usize, f: F) -> R fn with_entry<F, R>(no: usize, f: F) -> R
@ -196,14 +195,20 @@ impl TimerInner {
buckets buckets
} }
fn now(&self) -> time::Instant { fn now(&mut self, inner: &Rc<RefCell<TimerInner>>) -> time::Instant {
let cur = self.lowres_time.get(); let cur = self.lowres_time.get();
if let Some(cur) = cur { if let Some(cur) = cur {
cur cur
} else { } else {
let now = time::Instant::now(); let now = time::Instant::now();
self.lowres_driver.wake();
self.lowres_time.set(Some(now)); self.lowres_time.set(Some(now));
if self.flags.contains(Flags::LOWRES_DRIVER) {
self.lowres_driver.wake();
} else {
LowresTimerDriver::start(self, inner);
}
now now
} }
} }
@ -237,7 +242,7 @@ impl TimerInner {
} }
let delta = to_units( let delta = to_units(
(slf.now() + time::Duration::from_millis(millis) - slf.elapsed_instant) (slf.now(inner) + time::Duration::from_millis(millis) - slf.elapsed_instant)
.as_millis() as u64, .as_millis() as u64,
); );
@ -285,7 +290,7 @@ impl TimerInner {
} }
let delta = to_units( let delta = to_units(
(slf.now() + time::Duration::from_millis(millis) - slf.elapsed_instant) (slf.now(inner) + time::Duration::from_millis(millis) - slf.elapsed_instant)
.as_millis() as u64, .as_millis() as u64,
); );
@ -390,9 +395,9 @@ impl TimerInner {
} }
// Get instant of the next expiry // Get instant of the next expiry
fn next_expiry(&self) -> time::Instant { fn next_expiry(&mut self, inner: &Rc<RefCell<Self>>) -> time::Instant {
let millis = to_millis(self.next_expiry - self.elapsed); let millis = to_millis(self.next_expiry - self.elapsed);
self.now() + time::Duration::from_millis(millis) self.now(inner) + time::Duration::from_millis(millis)
} }
fn execute_expired_timers(&mut self, instant: time::Instant) { fn execute_expired_timers(&mut self, instant: time::Instant) {
@ -551,7 +556,7 @@ impl TimerDriver {
crate::rt::spawn(TimerDriver { crate::rt::spawn(TimerDriver {
inner: cell.clone(), inner: cell.clone(),
sleep: Box::pin(sleep_until(inner.next_expiry())), sleep: Box::pin(sleep_until(inner.next_expiry(cell))),
}); });
} }
} }
@ -572,7 +577,7 @@ impl Future for TimerDriver {
if inner.flags.contains(Flags::NEEDS_RECALC) { if inner.flags.contains(Flags::NEEDS_RECALC) {
inner.flags.remove(Flags::NEEDS_RECALC); inner.flags.remove(Flags::NEEDS_RECALC);
inner.flags.insert(Flags::TIMER_ACTIVE); inner.flags.insert(Flags::TIMER_ACTIVE);
let exp = inner.next_expiry(); let exp = inner.next_expiry(&self.inner);
drop(inner); drop(inner);
Pin::as_mut(&mut self.sleep).reset(exp); Pin::as_mut(&mut self.sleep).reset(exp);
return self.poll(cx); return self.poll(cx);
@ -587,7 +592,7 @@ impl Future for TimerDriver {
if let Some(next_expiry) = inner.next_pending_bucket() { if let Some(next_expiry) = inner.next_pending_bucket() {
inner.next_expiry = next_expiry; inner.next_expiry = next_expiry;
inner.flags.insert(Flags::TIMER_ACTIVE); inner.flags.insert(Flags::TIMER_ACTIVE);
let exp = inner.next_expiry(); let exp = inner.next_expiry(&self.inner);
drop(inner); drop(inner);
Pin::as_mut(&mut self.sleep).reset(exp); Pin::as_mut(&mut self.sleep).reset(exp);
return self.poll(cx); return self.poll(cx);
@ -607,9 +612,8 @@ struct LowresTimerDriver {
} }
impl LowresTimerDriver { impl LowresTimerDriver {
fn start(cell: &Rc<RefCell<TimerInner>>) { fn start(slf: &mut TimerInner, cell: &Rc<RefCell<TimerInner>>) {
let mut inner = cell.borrow_mut(); slf.flags.insert(Flags::LOWRES_DRIVER | Flags::LOWRES_TIMER);
inner.flags.insert(Flags::LOWRES_TIMER);
crate::rt::spawn(LowresTimerDriver { crate::rt::spawn(LowresTimerDriver {
inner: cell.clone(), inner: cell.clone(),