@@ -13,15 +13,17 @@ use entry::{EntryList, TimerHandle, TimerShared};
1313mod handle;
1414pub ( crate ) use self :: handle:: Handle ;
1515
16+ mod source;
17+ pub ( crate ) use source:: TimeSource ;
18+
1619mod wheel;
1720
1821use crate :: loom:: sync:: atomic:: { AtomicBool , Ordering } ;
1922use crate :: loom:: sync:: { Arc , Mutex } ;
2023use crate :: park:: { Park , Unpark } ;
2124use crate :: time:: error:: Error ;
22- use crate :: time:: { Clock , Duration , Instant } ;
25+ use crate :: time:: { Clock , Duration } ;
2326
24- use std:: convert:: TryInto ;
2527use std:: fmt;
2628use std:: { num:: NonZeroU64 , ptr:: NonNull , task:: Waker } ;
2729
@@ -83,7 +85,7 @@ use std::{num::NonZeroU64, ptr::NonNull, task::Waker};
8385#[ derive( Debug ) ]
8486pub ( crate ) struct Driver < P : Park + ' static > {
8587 /// Timing backend in use.
86- time_source : ClockTime ,
88+ time_source : TimeSource ,
8789
8890 /// Shared state.
8991 handle : Handle ,
@@ -101,58 +103,22 @@ pub(crate) struct Driver<P: Park + 'static> {
101103 did_wake : Arc < AtomicBool > ,
102104}
103105
104- /// A structure which handles conversion from Instants to u64 timestamps.
105- #[ derive( Debug , Clone ) ]
106- pub ( crate ) struct ClockTime {
107- clock : crate :: time:: Clock ,
108- start_time : Instant ,
109- }
110-
111- impl ClockTime {
112- pub ( self ) fn new ( clock : Clock ) -> Self {
113- Self {
114- start_time : clock. now ( ) ,
115- clock,
116- }
117- }
118-
119- pub ( crate ) fn deadline_to_tick ( & self , t : Instant ) -> u64 {
120- // Round up to the end of a ms
121- self . instant_to_tick ( t + Duration :: from_nanos ( 999_999 ) )
122- }
123-
124- pub ( self ) fn instant_to_tick ( & self , t : Instant ) -> u64 {
125- // round up
126- let dur: Duration = t
127- . checked_duration_since ( self . start_time )
128- . unwrap_or_else ( || Duration :: from_secs ( 0 ) ) ;
129- let ms = dur. as_millis ( ) ;
130-
131- ms. try_into ( ) . unwrap_or ( u64:: MAX )
132- }
133-
134- pub ( self ) fn tick_to_duration ( & self , t : u64 ) -> Duration {
135- Duration :: from_millis ( t)
136- }
137-
138- pub ( crate ) fn now ( & self ) -> u64 {
139- self . instant_to_tick ( self . clock . now ( ) )
140- }
141- }
142-
143106/// Timer state shared between `Driver`, `Handle`, and `Registration`.
144107struct Inner {
145108 // The state is split like this so `Handle` can access `is_shutdown` without locking the mutex
146109 pub ( super ) state : Mutex < InnerState > ,
147110
148111 /// True if the driver is being shutdown.
149112 pub ( super ) is_shutdown : AtomicBool ,
113+
114+ /// Unparker that can be used to wake the time driver.
115+ unpark : Box < dyn Unpark > ,
150116}
151117
152118/// Time state shared which must be protected by a `Mutex`
153119struct InnerState {
154120 /// Timing backend in use.
155- time_source : ClockTime ,
121+ time_source : TimeSource ,
156122
157123 /// The last published timer `elapsed` value.
158124 elapsed : u64 ,
@@ -162,9 +128,6 @@ struct InnerState {
162128
163129 /// Timer wheel.
164130 wheel : wheel:: Wheel ,
165-
166- /// Unparker that can be used to wake the time driver.
167- unpark : Box < dyn Unpark > ,
168131}
169132
170133// ===== impl Driver =====
@@ -178,7 +141,7 @@ where
178141 ///
179142 /// Specifying the source of time is useful when testing.
180143 pub ( crate ) fn new ( park : P , clock : Clock ) -> Driver < P > {
181- let time_source = ClockTime :: new ( clock) ;
144+ let time_source = TimeSource :: new ( clock) ;
182145
183146 let inner = Inner :: new ( time_source. clone ( ) , Box :: new ( park. unpark ( ) ) ) ;
184147
@@ -397,7 +360,7 @@ impl Handle {
397360 . map ( |next_wake| when < next_wake. get ( ) )
398361 . unwrap_or ( true )
399362 {
400- lock . unpark . unpark ( ) ;
363+ self . inner . unpark . unpark ( ) ;
401364 }
402365
403366 None
@@ -493,15 +456,15 @@ impl<P: Park + 'static> Unpark for TimerUnpark<P> {
493456// ===== impl Inner =====
494457
495458impl Inner {
496- pub ( self ) fn new ( time_source : ClockTime , unpark : Box < dyn Unpark > ) -> Self {
459+ pub ( self ) fn new ( time_source : TimeSource , unpark : Box < dyn Unpark > ) -> Self {
497460 Inner {
498461 state : Mutex :: new ( InnerState {
499462 time_source,
500463 elapsed : 0 ,
501464 next_wake : None ,
502- unpark,
503465 wheel : wheel:: Wheel :: new ( ) ,
504466 } ) ,
467+ unpark,
505468 is_shutdown : AtomicBool :: new ( false ) ,
506469 }
507470 }
0 commit comments