@@ -204,9 +204,7 @@ impl Condvar {
204
204
}
205
205
206
206
/// Blocks the current thread until this condition variable receives a
207
- /// notification and the required condition is met. Spurious wakeups are
208
- /// ignored and this function will only return once the condition has been
209
- /// met.
207
+ /// notification and the provided condition is false.
210
208
///
211
209
/// This function will atomically unlock the mutex specified (represented by
212
210
/// `guard`) and block the current thread. This means that any calls
@@ -231,32 +229,32 @@ impl Condvar {
231
229
/// use std::sync::{Arc, Mutex, Condvar};
232
230
/// use std::thread;
233
231
///
234
- /// let pair = Arc::new((Mutex::new(false ), Condvar::new()));
232
+ /// let pair = Arc::new((Mutex::new(true ), Condvar::new()));
235
233
/// let pair2 = pair.clone();
236
234
///
237
235
/// thread::spawn(move|| {
238
236
/// let (lock, cvar) = &*pair2;
239
- /// let mut started = lock.lock().unwrap();
240
- /// *started = true ;
237
+ /// let mut pending = lock.lock().unwrap();
238
+ /// *pending = false ;
241
239
/// // We notify the condvar that the value has changed.
242
240
/// cvar.notify_one();
243
241
/// });
244
242
///
245
243
/// // Wait for the thread to start up.
246
244
/// let (lock, cvar) = &*pair;
247
- /// // As long as the value inside the `Mutex<bool>` is `false `, we wait.
248
- /// let _guard = cvar.wait_until (lock.lock().unwrap(), |started | { *started }).unwrap();
245
+ /// // As long as the value inside the `Mutex<bool>` is `true `, we wait.
246
+ /// let _guard = cvar.wait_while (lock.lock().unwrap(), |pending | { *pending }).unwrap();
249
247
/// ```
250
248
#[ stable( feature = "wait_until" , since = "1.42.0" ) ]
251
- pub fn wait_until < ' a , T , F > (
249
+ pub fn wait_while < ' a , T , F > (
252
250
& self ,
253
251
mut guard : MutexGuard < ' a , T > ,
254
252
mut condition : F ,
255
253
) -> LockResult < MutexGuard < ' a , T > >
256
254
where
257
255
F : FnMut ( & mut T ) -> bool ,
258
256
{
259
- while ! condition ( & mut * guard) {
257
+ while condition ( & mut * guard) {
260
258
guard = self . wait ( guard) ?;
261
259
}
262
260
Ok ( guard)
@@ -341,11 +339,10 @@ impl Condvar {
341
339
/// Condition variables normally have a boolean predicate associated with
342
340
/// them, and the predicate must always be checked each time this function
343
341
/// returns to protect against spurious wakeups. Additionally, it is
344
- /// typically desirable for the time-out to not exceed some duration in
342
+ /// typically desirable for the timeout to not exceed some duration in
345
343
/// spite of spurious wakes, thus the sleep-duration is decremented by the
346
- /// amount slept. Alternatively, use the `wait_timeout_until` method
347
- /// to wait until a condition is met with a total time-out regardless
348
- /// of spurious wakes.
344
+ /// amount slept. Alternatively, use the `wait_timeout_while` method
345
+ /// to wait with a timeout while a predicate is true.
349
346
///
350
347
/// The returned [`WaitTimeoutResult`] value indicates if the timeout is
351
348
/// known to have elapsed.
@@ -354,7 +351,7 @@ impl Condvar {
354
351
/// returns, regardless of whether the timeout elapsed or not.
355
352
///
356
353
/// [`wait`]: #method.wait
357
- /// [`wait_timeout_until `]: #method.wait_timeout_until
354
+ /// [`wait_timeout_while `]: #method.wait_timeout_while
358
355
/// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
359
356
///
360
357
/// # Examples
@@ -405,10 +402,9 @@ impl Condvar {
405
402
}
406
403
407
404
/// Waits on this condition variable for a notification, timing out after a
408
- /// specified duration. Spurious wakes will not cause this function to
409
- /// return.
405
+ /// specified duration.
410
406
///
411
- /// The semantics of this function are equivalent to [`wait_until `] except
407
+ /// The semantics of this function are equivalent to [`wait_while `] except
412
408
/// that the thread will be blocked for roughly no longer than `dur`. This
413
409
/// method should not be used for precise timing due to anomalies such as
414
410
/// preemption or platform differences that may not cause the maximum
@@ -421,10 +417,10 @@ impl Condvar {
421
417
/// The returned [`WaitTimeoutResult`] value indicates if the timeout is
422
418
/// known to have elapsed without the condition being met.
423
419
///
424
- /// Like [`wait_until `], the lock specified will be re-acquired when this
420
+ /// Like [`wait_while `], the lock specified will be re-acquired when this
425
421
/// function returns, regardless of whether the timeout elapsed or not.
426
422
///
427
- /// [`wait_until `]: #method.wait_until
423
+ /// [`wait_while `]: #method.wait_while
428
424
/// [`wait_timeout`]: #method.wait_timeout
429
425
/// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
430
426
///
@@ -435,31 +431,31 @@ impl Condvar {
435
431
/// use std::thread;
436
432
/// use std::time::Duration;
437
433
///
438
- /// let pair = Arc::new((Mutex::new(false ), Condvar::new()));
434
+ /// let pair = Arc::new((Mutex::new(true ), Condvar::new()));
439
435
/// let pair2 = pair.clone();
440
436
///
441
437
/// thread::spawn(move|| {
442
438
/// let (lock, cvar) = &*pair2;
443
- /// let mut started = lock.lock().unwrap();
444
- /// *started = true ;
439
+ /// let mut pending = lock.lock().unwrap();
440
+ /// *pending = false ;
445
441
/// // We notify the condvar that the value has changed.
446
442
/// cvar.notify_one();
447
443
/// });
448
444
///
449
445
/// // wait for the thread to start up
450
446
/// let (lock, cvar) = &*pair;
451
- /// let result = cvar.wait_timeout_until (
447
+ /// let result = cvar.wait_timeout_while (
452
448
/// lock.lock().unwrap(),
453
449
/// Duration::from_millis(100),
454
- /// |&mut started| started ,
450
+ /// |&mut pending| pending ,
455
451
/// ).unwrap();
456
452
/// if result.1.timed_out() {
457
- /// // timed-out without the condition ever evaluating to true .
453
+ /// // timed-out without the condition ever evaluating to false .
458
454
/// }
459
455
/// // access the locked mutex via result.0
460
456
/// ```
461
457
#[ stable( feature = "wait_timeout_until" , since = "1.42.0" ) ]
462
- pub fn wait_timeout_until < ' a , T , F > (
458
+ pub fn wait_timeout_while < ' a , T , F > (
463
459
& self ,
464
460
mut guard : MutexGuard < ' a , T > ,
465
461
dur : Duration ,
@@ -470,7 +466,7 @@ impl Condvar {
470
466
{
471
467
let start = Instant :: now ( ) ;
472
468
loop {
473
- if condition ( & mut * guard) {
469
+ if ! condition ( & mut * guard) {
474
470
return Ok ( ( guard, WaitTimeoutResult ( false ) ) ) ;
475
471
}
476
472
let timeout = match dur. checked_sub ( start. elapsed ( ) ) {
@@ -678,7 +674,7 @@ mod tests {
678
674
679
675
#[ test]
680
676
#[ cfg_attr( target_os = "emscripten" , ignore) ]
681
- fn wait_until ( ) {
677
+ fn wait_while ( ) {
682
678
let pair = Arc :: new ( ( Mutex :: new ( false ) , Condvar :: new ( ) ) ) ;
683
679
let pair2 = pair. clone ( ) ;
684
680
@@ -693,7 +689,7 @@ mod tests {
693
689
694
690
// Wait for the thread to start up.
695
691
let & ( ref lock, ref cvar) = & * pair;
696
- let guard = cvar. wait_until ( lock. lock ( ) . unwrap ( ) , |started| * started) ;
692
+ let guard = cvar. wait_while ( lock. lock ( ) . unwrap ( ) , |started| ! * started) ;
697
693
assert ! ( * guard. unwrap( ) ) ;
698
694
}
699
695
@@ -720,32 +716,32 @@ mod tests {
720
716
#[ test]
721
717
#[ cfg_attr( target_os = "emscripten" , ignore) ]
722
718
#[ cfg_attr( target_env = "sgx" , ignore) ] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
723
- fn wait_timeout_until_wait ( ) {
719
+ fn wait_timeout_while_wait ( ) {
724
720
let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
725
721
let c = Arc :: new ( Condvar :: new ( ) ) ;
726
722
727
723
let g = m. lock ( ) . unwrap ( ) ;
728
- let ( _g, wait) = c. wait_timeout_until ( g, Duration :: from_millis ( 1 ) , |_| false ) . unwrap ( ) ;
724
+ let ( _g, wait) = c. wait_timeout_while ( g, Duration :: from_millis ( 1 ) , |_| true ) . unwrap ( ) ;
729
725
// no spurious wakeups. ensure it timed-out
730
726
assert ! ( wait. timed_out( ) ) ;
731
727
}
732
728
733
729
#[ test]
734
730
#[ cfg_attr( target_os = "emscripten" , ignore) ]
735
- fn wait_timeout_until_instant_satisfy ( ) {
731
+ fn wait_timeout_while_instant_satisfy ( ) {
736
732
let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
737
733
let c = Arc :: new ( Condvar :: new ( ) ) ;
738
734
739
735
let g = m. lock ( ) . unwrap ( ) ;
740
- let ( _g, wait) = c. wait_timeout_until ( g, Duration :: from_millis ( 0 ) , |_| true ) . unwrap ( ) ;
736
+ let ( _g, wait) = c. wait_timeout_while ( g, Duration :: from_millis ( 0 ) , |_| false ) . unwrap ( ) ;
741
737
// ensure it didn't time-out even if we were not given any time.
742
738
assert ! ( !wait. timed_out( ) ) ;
743
739
}
744
740
745
741
#[ test]
746
742
#[ cfg_attr( target_os = "emscripten" , ignore) ]
747
743
#[ cfg_attr( target_env = "sgx" , ignore) ] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
748
- fn wait_timeout_until_wake ( ) {
744
+ fn wait_timeout_while_wake ( ) {
749
745
let pair = Arc :: new ( ( Mutex :: new ( false ) , Condvar :: new ( ) ) ) ;
750
746
let pair_copy = pair. clone ( ) ;
751
747
@@ -759,7 +755,7 @@ mod tests {
759
755
cvar. notify_one ( ) ;
760
756
} ) ;
761
757
let ( g2, wait) = c
762
- . wait_timeout_until ( g, Duration :: from_millis ( u64:: MAX ) , |& mut notified| notified)
758
+ . wait_timeout_while ( g, Duration :: from_millis ( u64:: MAX ) , |& mut notified| ! notified)
763
759
. unwrap ( ) ;
764
760
// ensure it didn't time-out even if we were not given any time.
765
761
assert ! ( !wait. timed_out( ) ) ;
0 commit comments