diff options
| author | The8472 <git@infinite-source.de> | 2021-10-15 23:55:23 +0200 |
|---|---|---|
| committer | The 8472 <git@infinite-source.de> | 2022-02-13 01:04:55 +0100 |
| commit | 9d8ef1160747a4d033f21803770641f2deb32b25 (patch) | |
| tree | 4e6f7872651d83ab3cfc0470e0bd3cb68a628bce /library/std/src/sys | |
| parent | 5d8767cb229b097fedb1dd4bd9420d463c37774f (diff) | |
| download | rust-9d8ef1160747a4d033f21803770641f2deb32b25.tar.gz rust-9d8ef1160747a4d033f21803770641f2deb32b25.zip | |
make Instant::{duration_since, elapsed, sub} saturating and remove workarounds
This removes all mutex/atomics based workarounds for non-monotonic clocks and makes the previously panicking methods saturating instead. Effectively this moves the monotonization from `Instant` construction to the comparisons. This has some observable effects, especially on platforms without monotonic clocks: * Incorrectly ordered Instant comparisons no longer panic. This may hide some programming errors until someone actually looks at the resulting `Duration` * `checked_duration_since` will now return `None` in more cases. Previously it only happened when one compared instants obtained in the wrong order or manually created ones. Now it also does on backslides. The upside is reduced complexity and lower overhead of `Instant::now`.
Diffstat (limited to 'library/std/src/sys')
| -rw-r--r-- | library/std/src/sys/hermit/time.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/itron/time.rs | 9 | ||||
| -rw-r--r-- | library/std/src/sys/sgx/time.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/unix/time.rs | 19 | ||||
| -rw-r--r-- | library/std/src/sys/unsupported/time.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/wasi/time.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/windows/time.rs | 8 |
7 files changed, 0 insertions, 68 deletions
diff --git a/library/std/src/sys/hermit/time.rs b/library/std/src/sys/hermit/time.rs index c02de17c1fc..27173de6307 100644 --- a/library/std/src/sys/hermit/time.rs +++ b/library/std/src/sys/hermit/time.rs @@ -115,14 +115,6 @@ impl Instant { Instant { t: time } } - pub const fn zero() -> Instant { - Instant { t: Timespec::zero() } - } - - pub fn actually_monotonic() -> bool { - true - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { self.t.sub_timespec(&other.t).ok() } diff --git a/library/std/src/sys/itron/time.rs b/library/std/src/sys/itron/time.rs index 6a992ad1d3c..25f13ee441a 100644 --- a/library/std/src/sys/itron/time.rs +++ b/library/std/src/sys/itron/time.rs @@ -14,15 +14,6 @@ impl Instant { } } - pub const fn zero() -> Instant { - Instant(0) - } - - pub fn actually_monotonic() -> bool { - // There are ways to change the system time - false - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { self.0.checked_sub(other.0).map(|ticks| { // `SYSTIM` is measured in microseconds diff --git a/library/std/src/sys/sgx/time.rs b/library/std/src/sys/sgx/time.rs index e2f6e6dba69..db4cf2804bf 100644 --- a/library/std/src/sys/sgx/time.rs +++ b/library/std/src/sys/sgx/time.rs @@ -25,14 +25,6 @@ impl Instant { pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { Some(Instant(self.0.checked_sub(*other)?)) } - - pub fn actually_monotonic() -> bool { - false - } - - pub const fn zero() -> Instant { - Instant(Duration::from_secs(0)) - } } impl SystemTime { diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 824283ef6c4..59ddd1aa92f 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -154,14 +154,6 @@ mod inner { Instant { t: unsafe { mach_absolute_time() } } } - pub const fn zero() -> Instant { - Instant { t: 0 } - } - - pub fn actually_monotonic() -> bool { - true - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { let diff = self.t.checked_sub(other.t)?; let info = info(); @@ -296,17 +288,6 @@ mod inner { Instant { t: now(libc::CLOCK_MONOTONIC) } } - pub const fn zero() -> Instant { - Instant { t: Timespec::zero() } - } - - pub fn actually_monotonic() -> bool { - (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64")) - || (cfg!(target_os = "linux") && cfg!(target_arch = "x86")) - || (cfg!(target_os = "linux") && cfg!(target_arch = "aarch64")) - || cfg!(target_os = "fuchsia") - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { self.t.sub_timespec(&other.t).ok() } diff --git a/library/std/src/sys/unsupported/time.rs b/library/std/src/sys/unsupported/time.rs index 8aaf1777f24..6d67b538a96 100644 --- a/library/std/src/sys/unsupported/time.rs +++ b/library/std/src/sys/unsupported/time.rs @@ -13,14 +13,6 @@ impl Instant { panic!("time not implemented on this platform") } - pub const fn zero() -> Instant { - Instant(Duration::from_secs(0)) - } - - pub fn actually_monotonic() -> bool { - false - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { self.0.checked_sub(other.0) } diff --git a/library/std/src/sys/wasi/time.rs b/library/std/src/sys/wasi/time.rs index db0ddecf0c6..088585654b9 100644 --- a/library/std/src/sys/wasi/time.rs +++ b/library/std/src/sys/wasi/time.rs @@ -25,14 +25,6 @@ impl Instant { Instant(current_time(wasi::CLOCKID_MONOTONIC)) } - pub const fn zero() -> Instant { - Instant(Duration::from_secs(0)) - } - - pub fn actually_monotonic() -> bool { - true - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { self.0.checked_sub(other.0) } diff --git a/library/std/src/sys/windows/time.rs b/library/std/src/sys/windows/time.rs index 91e4f765484..a04908b541c 100644 --- a/library/std/src/sys/windows/time.rs +++ b/library/std/src/sys/windows/time.rs @@ -41,14 +41,6 @@ impl Instant { perf_counter::PerformanceCounterInstant::now().into() } - pub fn actually_monotonic() -> bool { - false - } - - pub const fn zero() -> Instant { - Instant { t: Duration::from_secs(0) } - } - pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { // On windows there's a threshold below which we consider two timestamps // equivalent due to measurement error. For more details + doc link, |
