From c6eb8527e09bed702f30ffdc8d6e54acf9b867ca Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 16 Nov 2015 17:36:14 -0800 Subject: std: Add Instant and SystemTime to std::time This commit is an implementation of [RFC 1288][rfc] which adds two new unstable types to the `std::time` module. The `Instant` type is used to represent measurements of a monotonically increasing clock suitable for measuring time withing a process for operations such as benchmarks or just the elapsed time to do something. An `Instant` favors panicking when bugs are found as the bugs are programmer errors rather than typical errors that can be encountered. [rfc]: https://github.com/rust-lang/rfcs/pull/1288 The `SystemTime` type is used to represent a system timestamp and is not monotonic. Very few guarantees are provided about this measurement of the system clock, but a fixed point in time (`UNIX_EPOCH`) is provided to learn about the relative distance from this point for any particular time stamp. This PR takes the same implementation strategy as the `time` crate on crates.io, namely: | Platform | Instant | SystemTime | |------------|--------------------------|--------------------------| | Windows | QueryPerformanceCounter | GetSystemTimeAsFileTime | | OSX | mach_absolute_time | gettimeofday | | Unix | CLOCK_MONOTONIC | CLOCK_REALTIME | These implementations can perhaps be refined over time, but they currently satisfy the requirements of the `Instant` and `SystemTime` types while also being portable across implementations and revisions of each platform. --- src/libstd/sync/condvar.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/libstd/sync') diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index a18370dc68e..389c9c4a066 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -12,11 +12,10 @@ use prelude::v1::*; use sync::atomic::{AtomicUsize, Ordering}; use sync::{mutex, MutexGuard, PoisonError}; -use sys::time::SteadyTime; use sys_common::condvar as sys; use sys_common::mutex as sys_mutex; use sys_common::poison::{self, LockResult}; -use time::Duration; +use time::{Instant, Duration}; /// A type indicating whether a timed wait on a condition variable returned /// due to a time out or not. @@ -345,14 +344,13 @@ impl StaticCondvar { where F: FnMut(LockResult<&mut T>) -> bool { // This could be made more efficient by pushing the implementation into // sys::condvar - let start = SteadyTime::now(); + let start = Instant::now(); let mut guard_result: LockResult> = Ok(guard); while !f(guard_result .as_mut() .map(|g| &mut **g) .map_err(|e| PoisonError::new(&mut **e.get_mut()))) { - let now = SteadyTime::now(); - let consumed = &now - &start; + let consumed = start.elapsed(); let guard = guard_result.unwrap_or_else(|e| e.into_inner()); let (new_guard_result, timed_out) = if consumed > dur { (Ok(guard), WaitTimeoutResult(true)) -- cgit 1.4.1-3-g733a5