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