Skip to content

Commit bd79cf8

Browse files
committed
time: rm the STATE_FIRING
1 parent d57c0cb commit bd79cf8

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

tokio/src/runtime/time/entry.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
//!
2222
//! Each timer has a state field associated with it. This field contains either
2323
//! the current scheduled time, or a special flag value indicating its state.
24-
//! This state can either indicate that the timer is firing (and thus will be fired
25-
//! with an `Ok(())` result soon) or that it has already been fired/deregistered.
24+
//! This state can either indicate that whether the timer has already been
25+
//! fired/deregistered.
2626
//!
2727
//! This single state field allows for code that is firing the timer to
2828
//! synchronize with any racing `reset` calls reliably.
@@ -48,10 +48,10 @@
4848
//! There is of course a race condition between timer reset and timer
4949
//! expiration. If the driver fails to observe the updated expiration time, it
5050
//! could trigger expiration of the timer too early. However, because
51-
//! [`mark_firing`][mark_firing] performs a compare-and-swap, it will identify this race and
52-
//! refuse to mark the timer as firing.
51+
//! [`mark_deregistered`][mark_deregistered] performs a compare-and-swap,
52+
//! it will identify this race and refuse to mark the timer as deregistered.
5353
//!
54-
//! [mark_firing]: TimerHandle::mark_firing
54+
//! [mark_deregistered]: TimerHandle::mark_deregistered
5555
5656
use crate::loom::cell::UnsafeCell;
5757
use crate::loom::sync::atomic::AtomicU64;
@@ -70,8 +70,7 @@ use std::{marker::PhantomPinned, pin::Pin, ptr::NonNull};
7070
type TimerResult = Result<(), crate::time::error::Error>;
7171

7272
const STATE_DEREGISTERED: u64 = u64::MAX;
73-
const STATE_FIRING: u64 = STATE_DEREGISTERED - 1;
74-
const STATE_MIN_VALUE: u64 = STATE_FIRING;
73+
const STATE_MIN_VALUE: u64 = STATE_DEREGISTERED;
7574
/// The largest safe integer to use for ticks.
7675
///
7776
/// This value should be updated if any other signal values are added above.
@@ -157,13 +156,13 @@ impl StateCell {
157156
}
158157
}
159158

160-
/// Marks this timer firing, if its scheduled time is not after `not_after`.
159+
/// Marks this timer deregistered, if its scheduled time is not after `not_after`.
161160
///
162161
/// If the timer is scheduled for a time after `not_after`, returns an Err
163162
/// containing the current scheduled time.
164163
///
165164
/// SAFETY: Must hold the driver lock.
166-
unsafe fn mark_firing(&self, not_after: u64) -> Result<(), u64> {
165+
unsafe fn mark_deregistered(&self, not_after: u64) -> Result<(), u64> {
167166
// Quick initial debug check to see if the timer is already fired. Since
168167
// firing the timer can only happen with the driver lock held, we know
169168
// we shouldn't be able to "miss" a transition to a fired state, even
@@ -184,7 +183,7 @@ impl StateCell {
184183

185184
match self.state.compare_exchange_weak(
186185
cur_state,
187-
STATE_FIRING,
186+
STATE_DEREGISTERED,
188187
Ordering::AcqRel,
189188
Ordering::Acquire,
190189
) {
@@ -204,15 +203,6 @@ impl StateCell {
204203
///
205204
/// SAFETY: The driver lock must be held.
206205
unsafe fn fire(&self, result: TimerResult) -> Option<Waker> {
207-
// Quick initial check to see if the timer is already fired. Since
208-
// firing the timer can only happen with the driver lock held, we know
209-
// we shouldn't be able to "miss" a transition to a fired state, even
210-
// with relaxed ordering.
211-
let cur_state = self.state.load(Ordering::Relaxed);
212-
if cur_state == STATE_DEREGISTERED {
213-
return None;
214-
}
215-
216206
// SAFETY: We assume the driver lock is held and the timer is not
217207
// fired, so only the driver is accessing this field.
218208
//
@@ -247,10 +237,9 @@ impl StateCell {
247237
fn extend_expiration(&self, new_timestamp: u64) -> Result<(), ()> {
248238
let mut prior = self.state.load(Ordering::Relaxed);
249239
loop {
250-
if new_timestamp < prior || prior >= STATE_MIN_VALUE {
240+
if new_timestamp < prior || prior == STATE_MIN_VALUE {
251241
return Err(());
252242
}
253-
254243
match self.state.compare_exchange_weak(
255244
prior,
256245
new_timestamp,
@@ -268,7 +257,7 @@ impl StateCell {
268257
/// ordering, but is conservative - if it returns false, the timer is
269258
/// definitely _not_ registered.
270259
pub(super) fn might_be_registered(&self) -> bool {
271-
self.state.load(Ordering::Relaxed) != u64::MAX
260+
self.state.load(Ordering::Relaxed) != STATE_DEREGISTERED
272261
}
273262
}
274263

@@ -639,8 +628,8 @@ impl TimerHandle {
639628
///
640629
/// SAFETY: The caller must ensure that the handle remains valid, the driver
641630
/// lock is held, and that the timer is not in any wheel linked lists.
642-
pub(super) unsafe fn mark_firing(&self, not_after: u64) -> Result<(), u64> {
643-
match self.inner.as_ref().state.mark_firing(not_after) {
631+
pub(super) unsafe fn mark_deregistered(&self, not_after: u64) -> Result<(), u64> {
632+
match self.inner.as_ref().state.mark_deregistered(not_after) {
644633
Ok(()) => {
645634
// mark this as being firing in cached_when
646635
self.inner.as_ref().set_cached_when(u64::MAX);

tokio/src/runtime/time/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl Handle {
340340
if let Some(entry) = lock.get_mut_entries(&expiration).pop_back() {
341341
// Try to expire the entry; this is cheap (doesn't synchronize) if
342342
// the timer is not expired, and updates cached_when.
343-
match unsafe { entry.mark_firing(expiration.deadline) } {
343+
match unsafe { entry.mark_deregistered(expiration.deadline) } {
344344
Ok(()) => {
345345
// Entry was expired.
346346
// SAFETY: We hold the driver lock, and just removed the entry from any linked lists.
@@ -390,9 +390,8 @@ impl Handle {
390390

391391
if entry.as_ref().might_be_registered() {
392392
lock.remove(entry);
393+
entry.as_ref().handle().fire(Ok(()));
393394
}
394-
395-
entry.as_ref().handle().fire(Ok(()));
396395
}
397396
}
398397

0 commit comments

Comments
 (0)