@@ -240,8 +240,12 @@ impl Sleeper {
240240 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
241241 pub fn wait ( & self ) {
242242 let ( cv, notified_fut_mtx) = self . setup_wait ( ) ;
243- let notified_fut = cv. wait_while ( notified_fut_mtx. lock ( ) . unwrap ( ) , |fut_opt| fut_opt. is_none ( ) )
244- . unwrap ( ) . take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ;
243+ let notified_fut = loop {
244+ let mut notified_fut_lck = cv. wait ( notified_fut_mtx. lock ( ) . unwrap ( ) ) . unwrap ( ) ;
245+ if let Some ( notified_fut) = notified_fut_lck. take ( ) {
246+ break notified_fut;
247+ }
248+ } ;
245249 notified_fut. lock ( ) . unwrap ( ) . callbacks_made = true ;
246250 }
247251
@@ -250,14 +254,18 @@ impl Sleeper {
250254 /// elapsed.
251255 #[ cfg( any( test, feature = "std" ) ) ]
252256 pub fn wait_timeout ( & self , max_wait : Duration ) -> bool {
257+ let start_time = Instant :: now ( ) ;
253258 let ( cv, notified_fut_mtx) = self . setup_wait ( ) ;
254- let notified_fut =
255- match cv. wait_timeout_while ( notified_fut_mtx. lock ( ) . unwrap ( ) , max_wait, |fut_opt| fut_opt. is_none ( ) ) {
256- Ok ( ( _, e) ) if e. timed_out ( ) => return false ,
257- Ok ( ( mut notified_fut, _) ) =>
258- notified_fut. take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ,
259+ let notified_fut = loop {
260+ let wait_duration = max_wait. saturating_sub ( start_time. elapsed ( ) ) ;
261+ if wait_duration == Duration :: from_secs ( 0 ) { return false ; }
262+ match cv. wait_timeout ( notified_fut_mtx. lock ( ) . unwrap ( ) , wait_duration) {
263+ Ok ( ( mut notified_fut, _) ) if notified_fut. is_some ( ) =>
264+ break notified_fut. take ( ) . unwrap ( ) ,
265+ Ok ( ( notified_fut_lck, _) ) => continue ,
259266 Err ( _) => panic ! ( "Previous panic while a lock was held led to a lock panic" ) ,
260267 } ;
268+ } ;
261269 notified_fut. lock ( ) . unwrap ( ) . callbacks_made = true ;
262270 true
263271 }
0 commit comments