@@ -258,31 +258,23 @@ impl Sleeper {
258
258
/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
259
259
pub fn wait ( & self ) {
260
260
let ( cv, notified_fut_mtx) = self . setup_wait ( ) ;
261
- let notified_fut = loop {
262
- let mut notified_fut_lck = cv. wait ( notified_fut_mtx. lock ( ) . unwrap ( ) ) . unwrap ( ) ;
263
- if let Some ( notified_fut) = notified_fut_lck. take ( ) {
264
- break notified_fut;
265
- }
266
- } ;
261
+ let notified_fut = cv. wait_while ( notified_fut_mtx. lock ( ) . unwrap ( ) , |fut_opt| fut_opt. is_none ( ) )
262
+ . unwrap ( ) . take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ;
267
263
notified_fut. lock ( ) . unwrap ( ) . callbacks_made = true ;
268
264
}
269
265
270
266
/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
271
267
/// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
272
268
/// elapsed.
273
269
pub fn wait_timeout ( & self , max_wait : Duration ) -> bool {
274
- let start_time = Instant :: now ( ) ;
275
270
let ( cv, notified_fut_mtx) = self . setup_wait ( ) ;
276
- let notified_fut = loop {
277
- let wait_duration = max_wait. saturating_sub ( start_time. elapsed ( ) ) ;
278
- if wait_duration == Duration :: from_secs ( 0 ) { return false ; }
279
- match cv. wait_timeout ( notified_fut_mtx. lock ( ) . unwrap ( ) , wait_duration) {
280
- Ok ( ( mut notified_fut, _) ) if notified_fut. is_some ( ) =>
281
- break notified_fut. take ( ) . unwrap ( ) ,
282
- Ok ( ( notified_fut_lck, _) ) => continue ,
271
+ let notified_fut =
272
+ match cv. wait_timeout_while ( notified_fut_mtx. lock ( ) . unwrap ( ) , max_wait, |fut_opt| fut_opt. is_none ( ) ) {
273
+ Ok ( ( _, e) ) if e. timed_out ( ) => return false ,
274
+ Ok ( ( mut notified_fut, _) ) =>
275
+ notified_fut. take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ,
283
276
Err ( _) => panic ! ( "Previous panic while a lock was held led to a lock panic" ) ,
284
277
} ;
285
- } ;
286
278
notified_fut. lock ( ) . unwrap ( ) . callbacks_made = true ;
287
279
true
288
280
}
0 commit comments