Skip to content

Commit dbcd1f9

Browse files
authored
time: Remove HandlePriv (#1896)
## Motivation #1800 removed the lazy binding of `Delay`s to timers. With the removal of the logic required for that, `HandlePriv` is no longer needed. This PR removes the use of `HandlePriv`. A `TODO` was also removed that would panic if when registering a new `Delay` the current timer handle was full. That has been fixed to now immediately transition that `Delay` to an error state that can be handled in a similar way to other error states. Signed-off-by: Kevin Leimkuhler <[email protected]>
1 parent 0e729aa commit dbcd1f9

File tree

3 files changed

+38
-63
lines changed

3 files changed

+38
-63
lines changed

tokio/src/time/driver/entry.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::loom::sync::atomic::AtomicU64;
22
use crate::sync::AtomicWaker;
3-
use crate::time::driver::{HandlePriv, Inner};
3+
use crate::time::driver::{Handle, Inner};
44
use crate::time::{Duration, Error, Instant};
55

66
use std::cell::UnsafeCell;
@@ -105,36 +105,24 @@ const ERROR: u64 = u64::MAX;
105105

106106
impl Entry {
107107
pub(crate) fn new(deadline: Instant, duration: Duration) -> Arc<Entry> {
108-
let handle_priv = HandlePriv::current();
109-
let handle = handle_priv.inner().unwrap();
108+
let inner = Handle::current().inner().unwrap();
109+
let entry: Entry;
110110

111111
// Increment the number of active timeouts
112-
if handle.increment().is_err() {
113-
// TODO(kleimkuhler): Transition to error state instead of
114-
// panicking?
115-
panic!("failed to add entry; timer at capacity");
116-
};
117-
118-
let when = handle.normalize_deadline(deadline);
119-
let state = if when <= handle.elapsed() {
120-
ELAPSED
112+
if inner.increment().is_err() {
113+
entry = Entry::new2(deadline, duration, Weak::new(), ERROR)
121114
} else {
122-
when
123-
};
124-
125-
let entry = Arc::new(Entry {
126-
time: CachePadded(UnsafeCell::new(Time { deadline, duration })),
127-
inner: handle_priv.into_inner(),
128-
waker: AtomicWaker::new(),
129-
state: AtomicU64::new(state),
130-
queued: AtomicBool::new(false),
131-
next_atomic: UnsafeCell::new(ptr::null_mut()),
132-
when: UnsafeCell::new(None),
133-
next_stack: UnsafeCell::new(None),
134-
prev_stack: UnsafeCell::new(ptr::null_mut()),
135-
});
115+
let when = inner.normalize_deadline(deadline);
116+
let state = if when <= inner.elapsed() {
117+
ELAPSED
118+
} else {
119+
when
120+
};
121+
entry = Entry::new2(deadline, duration, Arc::downgrade(&inner), state);
122+
}
136123

137-
if handle.queue(&entry).is_err() {
124+
let entry = Arc::new(entry);
125+
if inner.queue(&entry).is_err() {
138126
entry.error();
139127
}
140128

@@ -314,6 +302,20 @@ impl Entry {
314302
}
315303
}
316304

305+
fn new2(deadline: Instant, duration: Duration, inner: Weak<Inner>, state: u64) -> Self {
306+
Self {
307+
time: CachePadded(UnsafeCell::new(Time { deadline, duration })),
308+
inner,
309+
waker: AtomicWaker::new(),
310+
state: AtomicU64::new(state),
311+
queued: AtomicBool::new(false),
312+
next_atomic: UnsafeCell::new(ptr::null_mut()),
313+
when: UnsafeCell::new(None),
314+
next_stack: UnsafeCell::new(None),
315+
prev_stack: UnsafeCell::new(ptr::null_mut()),
316+
}
317+
}
318+
317319
fn upgrade_inner(&self) -> Option<Arc<Inner>> {
318320
self.inner.upgrade()
319321
}

tokio/src/time/driver/handle.rs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@ use std::marker::PhantomData;
55
use std::sync::{Arc, Weak};
66

77
/// Handle to time driver instance.
8-
#[derive(Debug, Clone)]
9-
pub(crate) struct Handle {
10-
inner: Option<HandlePriv>,
11-
}
12-
13-
/// Like `Handle` but never `None`.
148
#[derive(Clone)]
15-
pub(crate) struct HandlePriv {
9+
pub(crate) struct Handle {
1610
inner: Weak<Inner>,
1711
}
1812

1913
thread_local! {
2014
/// Tracks the timer for the current execution context.
21-
static CURRENT_TIMER: RefCell<Option<HandlePriv>> = RefCell::new(None)
15+
static CURRENT_TIMER: RefCell<Option<Handle>> = RefCell::new(None)
2216
}
2317

2418
#[derive(Debug)]
2519
/// Guard that unsets the current default timer on drop.
2620
pub(crate) struct DefaultGuard<'a> {
27-
prev: Option<HandlePriv>,
21+
prev: Option<Handle>,
2822
_lifetime: PhantomData<&'a u8>,
2923
}
3024

@@ -47,10 +41,6 @@ pub(crate) fn set_default(handle: &Handle) -> DefaultGuard<'_> {
4741
let mut current = current.borrow_mut();
4842
let prev = current.take();
4943

50-
let handle = handle
51-
.as_priv()
52-
.unwrap_or_else(|| panic!("`handle` does not reference a timer"));
53-
5444
*current = Some(handle.clone());
5545

5646
DefaultGuard {
@@ -61,23 +51,11 @@ pub(crate) fn set_default(handle: &Handle) -> DefaultGuard<'_> {
6151
}
6252

6353
impl Handle {
64-
pub(crate) fn new(inner: Weak<Inner>) -> Handle {
65-
let inner = HandlePriv { inner };
66-
Handle { inner: Some(inner) }
54+
/// Create a new timer `Handle` from a shared `Inner` timer state.
55+
pub(crate) fn new(inner: Weak<Inner>) -> Self {
56+
Handle { inner }
6757
}
6858

69-
fn as_priv(&self) -> Option<&HandlePriv> {
70-
self.inner.as_ref()
71-
}
72-
}
73-
74-
impl Default for Handle {
75-
fn default() -> Handle {
76-
Handle { inner: None }
77-
}
78-
}
79-
80-
impl HandlePriv {
8159
/// Try to get a handle to the current timer.
8260
///
8361
/// # Panics
@@ -94,15 +72,10 @@ impl HandlePriv {
9472
pub(crate) fn inner(&self) -> Option<Arc<Inner>> {
9573
self.inner.upgrade()
9674
}
97-
98-
/// Consume the handle, returning the weak Inner ref.
99-
pub(crate) fn into_inner(self) -> Weak<Inner> {
100-
self.inner
101-
}
10275
}
10376

104-
impl fmt::Debug for HandlePriv {
77+
impl fmt::Debug for Handle {
10578
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106-
write!(f, "HandlePriv")
79+
write!(f, "Handle")
10780
}
10881
}

tokio/src/time/driver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod entry;
77
use self::entry::Entry;
88

99
mod handle;
10-
pub(crate) use self::handle::{set_default, Handle, HandlePriv};
10+
pub(crate) use self::handle::{set_default, Handle};
1111

1212
mod registration;
1313
pub(crate) use self::registration::Registration;

0 commit comments

Comments
 (0)