Skip to content

Commit 371277f

Browse files
committed
Stabilize basic timeout functionality
This commit renames and stabilizes: * `Condvar::wait_timeout_ms` (renamed from `wait_timeout`) * `thread::park_timeout_ms` (renamed from `park_timeout`) * `thread::sleep_ms` (renamed from `sleep`) In each case, the timeout is taken as a `u32` number of milliseconds, rather than a `Duration`. These functions are likely to be deprecated once a stable form of `Duration` is available, but there is little cost to having these named variants around, and it's crucial functionality for 1.0. [breaking-change]
1 parent d528aa9 commit 371277f

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

src/libstd/sync/condvar.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -140,33 +140,43 @@ impl Condvar {
140140
/// Wait on this condition variable for a notification, timing out after a
141141
/// specified duration.
142142
///
143-
/// The semantics of this function are equivalent to `wait()` except that
144-
/// the thread will be blocked for roughly no longer than `dur`. This method
145-
/// should not be used for precise timing due to anomalies such as
146-
/// preemption or platform differences that may not cause the maximum amount
147-
/// of time waited to be precisely `dur`.
143+
/// The semantics of this function are equivalent to `wait()`
144+
/// except that the thread will be blocked for roughly no longer
145+
/// than `ms` milliseconds. This method should not be used for
146+
/// precise timing due to anomalies such as preemption or platform
147+
/// differences that may not cause the maximum amount of time
148+
/// waited to be precisely `ms`.
148149
///
149-
/// If the wait timed out, then `false` will be returned. Otherwise if a
150-
/// notification was received then `true` will be returned.
150+
/// The returned boolean is `false` only if the timeout is known
151+
/// to have elapsed.
151152
///
152153
/// Like `wait`, the lock specified will be re-acquired when this function
153154
/// returns, regardless of whether the timeout elapsed or not.
154-
#[unstable(feature = "std_misc")]
155-
pub fn wait_timeout<'a, T>(&self, guard: MutexGuard<'a, T>, dur: Duration)
156-
-> LockResult<(MutexGuard<'a, T>, bool)> {
155+
#[stable(feature = "rust1", since = "1.0.0")]
156+
pub fn wait_timeout_ms<'a, T>(&self, guard: MutexGuard<'a, T>, ms: u32)
157+
-> LockResult<(MutexGuard<'a, T>, bool)> {
157158
unsafe {
158159
let me: &'static Condvar = &*(self as *const _);
159-
me.inner.wait_timeout(guard, dur)
160+
me.inner.wait_timeout_ms(guard, ms)
160161
}
161162
}
162163

164+
/// Deprecated: use `wait_timeout_ms` instead.
165+
#[unstable(feature = "std_misc")]
166+
#[deprecated(since = "1.0.0", reason = "use wait_timeout_ms instead")]
167+
pub fn wait_timeout<'a, T>(&self, guard: MutexGuard<'a, T>, dur: Duration)
168+
-> LockResult<(MutexGuard<'a, T>, bool)> {
169+
self.wait_timeout_ms(guard, dur.num_milliseconds() as u32)
170+
}
171+
163172
/// Wait on this condition variable for a notification, timing out after a
164173
/// specified duration.
165174
///
166175
/// The semantics of this function are equivalent to `wait_timeout` except
167176
/// that the implementation will repeatedly wait while the duration has not
168177
/// passed and the provided function returns `false`.
169-
#[unstable(feature = "std_misc")]
178+
#[unstable(feature = "wait_timeout_with",
179+
reason = "unsure if this API is broadly needed or what form it should take")]
170180
pub fn wait_timeout_with<'a, T, F>(&self,
171181
guard: MutexGuard<'a, T>,
172182
dur: Duration,
@@ -235,12 +245,12 @@ impl StaticCondvar {
235245
/// See `Condvar::wait_timeout`.
236246
#[unstable(feature = "std_misc",
237247
reason = "may be merged with Condvar in the future")]
238-
pub fn wait_timeout<'a, T>(&'static self, guard: MutexGuard<'a, T>, dur: Duration)
239-
-> LockResult<(MutexGuard<'a, T>, bool)> {
248+
pub fn wait_timeout_ms<'a, T>(&'static self, guard: MutexGuard<'a, T>, ms: u32)
249+
-> LockResult<(MutexGuard<'a, T>, bool)> {
240250
let (poisoned, success) = unsafe {
241251
let lock = mutex::guard_lock(&guard);
242252
self.verify(lock);
243-
let success = self.inner.wait_timeout(lock, dur);
253+
let success = self.inner.wait_timeout(lock, Duration::milliseconds(ms as i64));
244254
(mutex::guard_poison(&guard).get(), success)
245255
};
246256
if poisoned {
@@ -275,7 +285,8 @@ impl StaticCondvar {
275285
let now = SteadyTime::now();
276286
let consumed = &now - &start;
277287
let guard = guard_result.unwrap_or_else(|e| e.into_inner());
278-
let (new_guard_result, no_timeout) = match self.wait_timeout(guard, dur - consumed) {
288+
let res = self.wait_timeout_ms(guard, (dur - consumed).num_milliseconds() as u32);
289+
let (new_guard_result, no_timeout) = match res {
279290
Ok((new_guard, no_timeout)) => (Ok(new_guard), no_timeout),
280291
Err(err) => {
281292
let (new_guard, no_timeout) = err.into_inner();
@@ -350,6 +361,7 @@ mod tests {
350361
use sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
351362
use thread;
352363
use time::Duration;
364+
use u32;
353365

354366
#[test]
355367
fn smoke() {
@@ -418,19 +430,19 @@ mod tests {
418430
}
419431

420432
#[test]
421-
fn wait_timeout() {
433+
fn wait_timeout_ms() {
422434
static C: StaticCondvar = CONDVAR_INIT;
423435
static M: StaticMutex = MUTEX_INIT;
424436

425437
let g = M.lock().unwrap();
426-
let (g, _no_timeout) = C.wait_timeout(g, Duration::nanoseconds(1000)).unwrap();
438+
let (g, _no_timeout) = C.wait_timeout_ms(g, 1).unwrap();
427439
// spurious wakeups mean this isn't necessarily true
428440
// assert!(!no_timeout);
429441
let _t = thread::spawn(move || {
430442
let _g = M.lock().unwrap();
431443
C.notify_one();
432444
});
433-
let (g, no_timeout) = C.wait_timeout(g, Duration::days(1)).unwrap();
445+
let (g, no_timeout) = C.wait_timeout_ms(g, u32::MAX).unwrap();
434446
assert!(no_timeout);
435447
drop(g);
436448
unsafe { C.destroy(); M.destroy(); }

src/libstd/thread/mod.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,16 @@ pub fn catch_panic<F, R>(f: F) -> Result<R>
465465
/// specifics or platform-dependent functionality. Note that on unix platforms
466466
/// this function will not return early due to a signal being received or a
467467
/// spurious wakeup.
468+
#[stable(feature = "rust1", since = "1.0.0")]
469+
pub fn sleep_ms(ms: u32) {
470+
imp::sleep(Duration::milliseconds(ms as i64))
471+
}
472+
473+
/// Deprecated: use `sleep_ms` instead.
468474
#[unstable(feature = "thread_sleep",
469475
reason = "recently added, needs an RFC, and `Duration` itself is \
470476
unstable")]
477+
#[deprecated(since = "1.0.0", reason = "use sleep_ms instead")]
471478
pub fn sleep(dur: Duration) {
472479
imp::sleep(dur)
473480
}
@@ -501,17 +508,24 @@ pub fn park() {
501508
/// amount of time waited to be precisely *duration* long.
502509
///
503510
/// See the module doc for more detail.
504-
#[unstable(feature = "std_misc", reason = "recently introduced, depends on Duration")]
505-
pub fn park_timeout(duration: Duration) {
511+
#[stable(feature = "rust1", since = "1.0.0")]
512+
pub fn park_timeout_ms(ms: u32) {
506513
let thread = current();
507514
let mut guard = thread.inner.lock.lock().unwrap();
508515
if !*guard {
509-
let (g, _) = thread.inner.cvar.wait_timeout(guard, duration).unwrap();
516+
let (g, _) = thread.inner.cvar.wait_timeout_ms(guard, ms).unwrap();
510517
guard = g;
511518
}
512519
*guard = false;
513520
}
514521

522+
/// Deprecated: use `park_timeout_ms`
523+
#[unstable(feature = "std_misc", reason = "recently introduced, depends on Duration")]
524+
#[deprecated(since = "1.0.0", reason = "use park_timeout_ms instead")]
525+
pub fn park_timeout(duration: Duration) {
526+
park_timeout_ms(duration.num_milliseconds() as u32)
527+
}
528+
515529
////////////////////////////////////////////////////////////////////////////////
516530
// Thread
517531
////////////////////////////////////////////////////////////////////////////////
@@ -716,6 +730,7 @@ mod test {
716730
use thread;
717731
use thunk::Thunk;
718732
use time::Duration;
733+
use u32;
719734

720735
// !!! These tests are dangerous. If something is buggy, they will hang, !!!
721736
// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
@@ -936,14 +951,14 @@ mod test {
936951
fn test_park_timeout_unpark_before() {
937952
for _ in 0..10 {
938953
thread::current().unpark();
939-
thread::park_timeout(Duration::seconds(10_000_000));
954+
thread::park_timeout_ms(u32::MAX);
940955
}
941956
}
942957

943958
#[test]
944959
fn test_park_timeout_unpark_not_called() {
945960
for _ in 0..10 {
946-
thread::park_timeout(Duration::milliseconds(10));
961+
thread::park_timeout_ms(10);
947962
}
948963
}
949964

@@ -959,14 +974,13 @@ mod test {
959974
th.unpark();
960975
});
961976

962-
thread::park_timeout(Duration::seconds(10_000_000));
977+
thread::park_timeout_ms(u32::MAX);
963978
}
964979
}
965980

966981
#[test]
967-
fn sleep_smoke() {
968-
thread::sleep(Duration::milliseconds(2));
969-
thread::sleep(Duration::milliseconds(-2));
982+
fn sleep_ms_smoke() {
983+
thread::sleep_ms(2);
970984
}
971985

972986
// NOTE: the corresponding test for stderr is in run-pass/task-stderr, due

0 commit comments

Comments
 (0)