Skip to content

Commit 1407c89

Browse files
committed
Add timeout sanity check in wait_for. Remove wait_until
1 parent ec1194e commit 1407c89

File tree

1 file changed

+9
-53
lines changed

1 file changed

+9
-53
lines changed

src/libstd/sys_common/parking_lot/condvar.rs

+9-53
Original file line numberDiff line numberDiff line change
@@ -219,34 +219,6 @@ impl Condvar {
219219
self.wait_until_internal(mutex, None);
220220
}
221221

222-
/// Waits on this condition variable for a notification, timing out after
223-
/// the specified time instant.
224-
///
225-
/// The semantics of this function are equivalent to `wait()` except that
226-
/// the thread will be blocked roughly until `timeout` is reached. This
227-
/// method should not be used for precise timing due to anomalies such as
228-
/// preemption or platform differences that may not cause the maximum
229-
/// amount of time waited to be precisely `timeout`.
230-
///
231-
/// Note that the best effort is made to ensure that the time waited is
232-
/// measured with a monotonic clock, and not affected by the changes made to
233-
/// the system time.
234-
///
235-
/// The returned `WaitTimeoutResult` value indicates if the timeout is
236-
/// known to have elapsed.
237-
///
238-
/// Like `wait`, the lock specified will be re-acquired when this function
239-
/// returns, regardless of whether the timeout elapsed or not.
240-
///
241-
/// # Panics
242-
///
243-
/// This function will panic if another thread is waiting on the `Condvar`
244-
/// with a different `Mutex` object.
245-
#[inline]
246-
pub fn wait_until(&self, mutex: &RawMutex, timeout: Instant) -> WaitTimeoutResult {
247-
self.wait_until_internal(mutex, Some(timeout))
248-
}
249-
250222
// This is a non-generic function to reduce the monomorphization cost of
251223
// using `wait_until`.
252224
fn wait_until_internal(&self, mutex: &RawMutex, timeout: Option<Instant>) -> WaitTimeoutResult {
@@ -333,7 +305,14 @@ impl Condvar {
333305
/// returns, regardless of whether the timeout elapsed or not.
334306
#[inline]
335307
pub fn wait_for(&self, mutex: &RawMutex, timeout: Duration) -> WaitTimeoutResult {
336-
self.wait_until(mutex, Instant::now() + timeout)
308+
/// FIXME: As soon as `Instant::checked_add` is merged, use that instead.
309+
const MAX_WAIT: Duration = Duration::from_nanos(u64::max_value());
310+
let timeout = if timeout > MAX_WAIT {
311+
None
312+
} else {
313+
Some(Instant::now() + timeout)
314+
};
315+
self.wait_until_internal(mutex, timeout)
337316
}
338317
}
339318

@@ -355,7 +334,7 @@ mod tests {
355334
use sync::mpsc::channel;
356335
use sync::Arc;
357336
use thread;
358-
use time::{Duration, Instant};
337+
use time::Duration;
359338
use super::{Condvar, RawMutex};
360339

361340
#[test]
@@ -431,29 +410,6 @@ mod tests {
431410
m.unlock();
432411
}
433412

434-
#[test]
435-
fn wait_until() {
436-
let m = Arc::new(RawMutex::INIT);
437-
let m2 = m.clone();
438-
let c = Arc::new(Condvar::new());
439-
let c2 = c.clone();
440-
441-
m.lock();
442-
let no_timeout = c.wait_until(&m, Instant::now() + Duration::from_millis(1));
443-
assert!(no_timeout.timed_out());
444-
let _t = thread::spawn(move || {
445-
m2.lock();
446-
c2.notify_one();
447-
m2.unlock();
448-
});
449-
let timeout_res = c.wait_until(
450-
&m,
451-
Instant::now() + Duration::from_millis(u32::max_value() as u64),
452-
);
453-
assert!(!timeout_res.timed_out());
454-
m.unlock();
455-
}
456-
457413
#[test]
458414
#[should_panic]
459415
fn two_mutexes() {

0 commit comments

Comments
 (0)