21
21
//!
22
22
//! Each timer has a state field associated with it. This field contains either
23
23
//! 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.
26
26
//!
27
27
//! This single state field allows for code that is firing the timer to
28
28
//! synchronize with any racing `reset` calls reliably.
48
48
//! There is of course a race condition between timer reset and timer
49
49
//! expiration. If the driver fails to observe the updated expiration time, it
50
50
//! 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 .
53
53
//!
54
- //! [mark_firing ]: TimerHandle::mark_firing
54
+ //! [mark_deregistered ]: TimerHandle::mark_deregistered
55
55
56
56
use crate :: loom:: cell:: UnsafeCell ;
57
57
use crate :: loom:: sync:: atomic:: AtomicU64 ;
@@ -70,8 +70,7 @@ use std::{marker::PhantomPinned, pin::Pin, ptr::NonNull};
70
70
type TimerResult = Result < ( ) , crate :: time:: error:: Error > ;
71
71
72
72
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 ;
75
74
/// The largest safe integer to use for ticks.
76
75
///
77
76
/// This value should be updated if any other signal values are added above.
@@ -157,13 +156,13 @@ impl StateCell {
157
156
}
158
157
}
159
158
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`.
161
160
///
162
161
/// If the timer is scheduled for a time after `not_after`, returns an Err
163
162
/// containing the current scheduled time.
164
163
///
165
164
/// 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 > {
167
166
// Quick initial debug check to see if the timer is already fired. Since
168
167
// firing the timer can only happen with the driver lock held, we know
169
168
// we shouldn't be able to "miss" a transition to a fired state, even
@@ -184,7 +183,7 @@ impl StateCell {
184
183
185
184
match self . state . compare_exchange_weak (
186
185
cur_state,
187
- STATE_FIRING ,
186
+ STATE_DEREGISTERED ,
188
187
Ordering :: AcqRel ,
189
188
Ordering :: Acquire ,
190
189
) {
@@ -204,15 +203,6 @@ impl StateCell {
204
203
///
205
204
/// SAFETY: The driver lock must be held.
206
205
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
-
216
206
// SAFETY: We assume the driver lock is held and the timer is not
217
207
// fired, so only the driver is accessing this field.
218
208
//
@@ -247,10 +237,9 @@ impl StateCell {
247
237
fn extend_expiration ( & self , new_timestamp : u64 ) -> Result < ( ) , ( ) > {
248
238
let mut prior = self . state . load ( Ordering :: Relaxed ) ;
249
239
loop {
250
- if new_timestamp < prior || prior > = STATE_MIN_VALUE {
240
+ if new_timestamp < prior || prior = = STATE_MIN_VALUE {
251
241
return Err ( ( ) ) ;
252
242
}
253
-
254
243
match self . state . compare_exchange_weak (
255
244
prior,
256
245
new_timestamp,
@@ -268,7 +257,7 @@ impl StateCell {
268
257
/// ordering, but is conservative - if it returns false, the timer is
269
258
/// definitely _not_ registered.
270
259
pub ( super ) fn might_be_registered ( & self ) -> bool {
271
- self . state . load ( Ordering :: Relaxed ) != u64 :: MAX
260
+ self . state . load ( Ordering :: Relaxed ) != STATE_DEREGISTERED
272
261
}
273
262
}
274
263
@@ -639,8 +628,8 @@ impl TimerHandle {
639
628
///
640
629
/// SAFETY: The caller must ensure that the handle remains valid, the driver
641
630
/// 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) {
644
633
Ok ( ( ) ) => {
645
634
// mark this as being firing in cached_when
646
635
self . inner . as_ref ( ) . set_cached_when ( u64:: MAX ) ;
0 commit comments