22
22
//! is only used in order to wake up when the earliest timer finishes, then restarted for the next
23
23
//! timer.
24
24
25
+ use crate :: bindings;
26
+
25
27
use core:: {
26
28
cmp:: { Eq , Ord , Ordering , PartialEq , PartialOrd } ,
27
29
future,
@@ -31,13 +33,8 @@ use core::{
31
33
} ;
32
34
use std:: { collections:: BinaryHeap , sync:: Mutex , time:: Instant } ;
33
35
34
- pub ( crate ) fn timer_finished ( timer_id : u32 ) {
35
- let callback = {
36
- let ptr = timer_id as * mut Box < dyn FnOnce ( ) + ' static > ;
37
- unsafe { Box :: from_raw ( ptr) }
38
- } ;
39
-
40
- callback ( ) ;
36
+ pub ( crate ) fn timer_finished ( ) {
37
+ process_timers ( ) ;
41
38
}
42
39
43
40
/// `Future` that automatically wakes up after a certain amount of time has elapsed.
@@ -81,9 +78,9 @@ impl Delay {
81
78
// If the timer that has just been inserted is the one that ends the soonest, then
82
79
// actually start the callback that will process timers.
83
80
// Ideally we would instead cancel or update the deadline of the previous call to
84
- // `start_timer_wrap `, but this isn't possible.
81
+ // `start_timer `, but this isn't possible.
85
82
if lock. timers_queue . peek ( ) . unwrap ( ) . timer_id == timer_id {
86
- super :: start_timer_wrap ( when - now, process_timers ) ;
83
+ start_timer ( when - now) ;
87
84
}
88
85
89
86
Delay {
@@ -245,10 +242,18 @@ fn process_timers() {
245
242
} ;
246
243
247
244
if let Some ( next_wakeup) = next_wakeup {
248
- super :: start_timer_wrap ( lock. time_zero + next_wakeup - now, process_timers ) ;
245
+ start_timer ( lock. time_zero + next_wakeup - now) ;
249
246
} else {
250
247
// Clean up memory a bit. Hopefully this doesn't impact performances too much.
251
248
lock. timers_queue . shrink_to_fit ( ) ;
252
249
lock. timers . shrink_to_fit ( ) ;
253
250
}
254
251
}
252
+
253
+ /// Instructs the environment to call [`process_timers`] after the given duration.
254
+ fn start_timer ( duration : Duration ) {
255
+ // Note that ideally `duration` should be rounded up in order to make sure that it is not
256
+ // truncated, but the precision of an `f64` is so high and the precision of the operating
257
+ // system generally so low that this is not worth dealing with.
258
+ unsafe { bindings:: start_timer ( duration. as_secs_f64 ( ) * 1000.0 ) }
259
+ }
0 commit comments