Skip to content

Commit 98d054a

Browse files
committed
Rename wait_until/wait_timeout_until to wait_while/white_timeout_while
1 parent 207e60a commit 98d054a

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/libstd/sync/condvar.rs

+32-36
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ impl Condvar {
204204
}
205205

206206
/// 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.
210208
///
211209
/// This function will atomically unlock the mutex specified (represented by
212210
/// `guard`) and block the current thread. This means that any calls
@@ -231,32 +229,32 @@ impl Condvar {
231229
/// use std::sync::{Arc, Mutex, Condvar};
232230
/// use std::thread;
233231
///
234-
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
232+
/// let pair = Arc::new((Mutex::new(true), Condvar::new()));
235233
/// let pair2 = pair.clone();
236234
///
237235
/// thread::spawn(move|| {
238236
/// let (lock, cvar) = &*pair2;
239-
/// let mut started = lock.lock().unwrap();
240-
/// *started = true;
237+
/// let mut pending = lock.lock().unwrap();
238+
/// *pending = false;
241239
/// // We notify the condvar that the value has changed.
242240
/// cvar.notify_one();
243241
/// });
244242
///
245243
/// // Wait for the thread to start up.
246244
/// 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();
249247
/// ```
250248
#[stable(feature = "wait_until", since = "1.42.0")]
251-
pub fn wait_until<'a, T, F>(
249+
pub fn wait_while<'a, T, F>(
252250
&self,
253251
mut guard: MutexGuard<'a, T>,
254252
mut condition: F,
255253
) -> LockResult<MutexGuard<'a, T>>
256254
where
257255
F: FnMut(&mut T) -> bool,
258256
{
259-
while !condition(&mut *guard) {
257+
while condition(&mut *guard) {
260258
guard = self.wait(guard)?;
261259
}
262260
Ok(guard)
@@ -341,11 +339,10 @@ impl Condvar {
341339
/// Condition variables normally have a boolean predicate associated with
342340
/// them, and the predicate must always be checked each time this function
343341
/// 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
345343
/// 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.
349346
///
350347
/// The returned [`WaitTimeoutResult`] value indicates if the timeout is
351348
/// known to have elapsed.
@@ -354,7 +351,7 @@ impl Condvar {
354351
/// returns, regardless of whether the timeout elapsed or not.
355352
///
356353
/// [`wait`]: #method.wait
357-
/// [`wait_timeout_until`]: #method.wait_timeout_until
354+
/// [`wait_timeout_while`]: #method.wait_timeout_while
358355
/// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
359356
///
360357
/// # Examples
@@ -405,10 +402,9 @@ impl Condvar {
405402
}
406403

407404
/// 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.
410406
///
411-
/// The semantics of this function are equivalent to [`wait_until`] except
407+
/// The semantics of this function are equivalent to [`wait_while`] except
412408
/// that the thread will be blocked for roughly no longer than `dur`. This
413409
/// method should not be used for precise timing due to anomalies such as
414410
/// preemption or platform differences that may not cause the maximum
@@ -421,10 +417,10 @@ impl Condvar {
421417
/// The returned [`WaitTimeoutResult`] value indicates if the timeout is
422418
/// known to have elapsed without the condition being met.
423419
///
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
425421
/// function returns, regardless of whether the timeout elapsed or not.
426422
///
427-
/// [`wait_until`]: #method.wait_until
423+
/// [`wait_while`]: #method.wait_while
428424
/// [`wait_timeout`]: #method.wait_timeout
429425
/// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
430426
///
@@ -435,31 +431,31 @@ impl Condvar {
435431
/// use std::thread;
436432
/// use std::time::Duration;
437433
///
438-
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
434+
/// let pair = Arc::new((Mutex::new(true), Condvar::new()));
439435
/// let pair2 = pair.clone();
440436
///
441437
/// thread::spawn(move|| {
442438
/// let (lock, cvar) = &*pair2;
443-
/// let mut started = lock.lock().unwrap();
444-
/// *started = true;
439+
/// let mut pending = lock.lock().unwrap();
440+
/// *pending = false;
445441
/// // We notify the condvar that the value has changed.
446442
/// cvar.notify_one();
447443
/// });
448444
///
449445
/// // wait for the thread to start up
450446
/// let (lock, cvar) = &*pair;
451-
/// let result = cvar.wait_timeout_until(
447+
/// let result = cvar.wait_timeout_while(
452448
/// lock.lock().unwrap(),
453449
/// Duration::from_millis(100),
454-
/// |&mut started| started,
450+
/// |&mut pending| pending,
455451
/// ).unwrap();
456452
/// 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.
458454
/// }
459455
/// // access the locked mutex via result.0
460456
/// ```
461457
#[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>(
463459
&self,
464460
mut guard: MutexGuard<'a, T>,
465461
dur: Duration,
@@ -470,7 +466,7 @@ impl Condvar {
470466
{
471467
let start = Instant::now();
472468
loop {
473-
if condition(&mut *guard) {
469+
if !condition(&mut *guard) {
474470
return Ok((guard, WaitTimeoutResult(false)));
475471
}
476472
let timeout = match dur.checked_sub(start.elapsed()) {
@@ -678,7 +674,7 @@ mod tests {
678674

679675
#[test]
680676
#[cfg_attr(target_os = "emscripten", ignore)]
681-
fn wait_until() {
677+
fn wait_while() {
682678
let pair = Arc::new((Mutex::new(false), Condvar::new()));
683679
let pair2 = pair.clone();
684680

@@ -693,7 +689,7 @@ mod tests {
693689

694690
// Wait for the thread to start up.
695691
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);
697693
assert!(*guard.unwrap());
698694
}
699695

@@ -720,32 +716,32 @@ mod tests {
720716
#[test]
721717
#[cfg_attr(target_os = "emscripten", ignore)]
722718
#[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() {
724720
let m = Arc::new(Mutex::new(()));
725721
let c = Arc::new(Condvar::new());
726722

727723
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();
729725
// no spurious wakeups. ensure it timed-out
730726
assert!(wait.timed_out());
731727
}
732728

733729
#[test]
734730
#[cfg_attr(target_os = "emscripten", ignore)]
735-
fn wait_timeout_until_instant_satisfy() {
731+
fn wait_timeout_while_instant_satisfy() {
736732
let m = Arc::new(Mutex::new(()));
737733
let c = Arc::new(Condvar::new());
738734

739735
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();
741737
// ensure it didn't time-out even if we were not given any time.
742738
assert!(!wait.timed_out());
743739
}
744740

745741
#[test]
746742
#[cfg_attr(target_os = "emscripten", ignore)]
747743
#[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() {
749745
let pair = Arc::new((Mutex::new(false), Condvar::new()));
750746
let pair_copy = pair.clone();
751747

@@ -759,7 +755,7 @@ mod tests {
759755
cvar.notify_one();
760756
});
761757
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)
763759
.unwrap();
764760
// ensure it didn't time-out even if we were not given any time.
765761
assert!(!wait.timed_out());

0 commit comments

Comments
 (0)