2 use crate::time::Duration;
4 pub use self::inner::Instant;
6 const NSEC_PER_SEC: u64 = 1_000_000_000;
7 pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
9 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10 pub struct SystemTime {
11 pub(in crate::sys::unix) t: Timespec,
14 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15 pub(in crate::sys::unix) struct Timespec {
21 #[cfg_attr(target_os = "horizon", allow(unused))]
22 pub fn new(tv_sec: i64, tv_nsec: i64) -> SystemTime {
23 SystemTime { t: Timespec::new(tv_sec, tv_nsec) }
26 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
27 self.t.sub_timespec(&other.t)
30 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
31 Some(SystemTime { t: self.t.checked_add_duration(other)? })
34 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
35 Some(SystemTime { t: self.t.checked_sub_duration(other)? })
39 impl From<libc::timespec> for SystemTime {
40 fn from(t: libc::timespec) -> SystemTime {
41 SystemTime { t: Timespec::from(t) }
45 impl fmt::Debug for SystemTime {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 f.debug_struct("SystemTime")
48 .field("tv_sec", &self.t.tv_sec)
49 .field("tv_nsec", &self.t.tv_nsec)
55 pub const fn zero() -> Timespec {
56 Timespec { tv_sec: 0, tv_nsec: 0 }
59 fn new(tv_sec: i64, tv_nsec: i64) -> Timespec {
60 Timespec { tv_sec, tv_nsec }
63 pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
65 // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
66 // to optimize it into a branchless form (see also #75545):
68 // 1. `self.tv_sec - other.tv_sec` shows up as a common expression
69 // in both branches, i.e. the `else` must have its `- 1`
70 // subtraction after the common one, not interleaved with it
71 // (it used to be `self.tv_sec - 1 - other.tv_sec`)
73 // 2. the `Duration::new` call (or any other additional complexity)
74 // is outside of the `if`-`else`, not duplicated in both branches
76 // Ideally this code could be rearranged such that it more
77 // directly expresses the lower-cost behavior we want from it.
78 let (secs, nsec) = if self.tv_nsec >= other.tv_nsec {
79 ((self.tv_sec - other.tv_sec) as u64, (self.tv_nsec - other.tv_nsec) as u32)
82 (self.tv_sec - other.tv_sec - 1) as u64,
83 self.tv_nsec as u32 + (NSEC_PER_SEC as u32) - other.tv_nsec as u32,
87 Ok(Duration::new(secs, nsec))
89 match other.sub_timespec(self) {
96 pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
99 .try_into() // <- target type would be `i64`
101 .and_then(|secs| self.tv_sec.checked_add(secs))?;
103 // Nano calculations can't overflow because nanos are <1B which fit
105 let mut nsec = other.subsec_nanos() + self.tv_nsec as u32;
106 if nsec >= NSEC_PER_SEC as u32 {
107 nsec -= NSEC_PER_SEC as u32;
108 secs = secs.checked_add(1)?;
110 Some(Timespec::new(secs, nsec as i64))
113 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
116 .try_into() // <- target type would be `i64`
118 .and_then(|secs| self.tv_sec.checked_sub(secs))?;
120 // Similar to above, nanos can't overflow.
121 let mut nsec = self.tv_nsec as i32 - other.subsec_nanos() as i32;
123 nsec += NSEC_PER_SEC as i32;
124 secs = secs.checked_sub(1)?;
126 Some(Timespec::new(secs, nsec as i64))
130 pub fn to_timespec(&self) -> Option<libc::timespec> {
131 Some(libc::timespec {
132 tv_sec: self.tv_sec.try_into().ok()?,
133 tv_nsec: self.tv_nsec.try_into().ok()?,
138 impl From<libc::timespec> for Timespec {
139 fn from(t: libc::timespec) -> Timespec {
140 Timespec::new(t.tv_sec as i64, t.tv_nsec as i64)
144 #[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
146 use crate::sync::atomic::{AtomicU64, Ordering};
148 use crate::sys_common::mul_div_u64;
149 use crate::time::Duration;
151 use super::{SystemTime, Timespec, NSEC_PER_SEC};
153 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
159 #[derive(Copy, Clone)]
160 struct mach_timebase_info {
164 type mach_timebase_info_t = *mut mach_timebase_info;
165 type kern_return_t = libc::c_int;
168 pub fn now() -> Instant {
170 fn mach_absolute_time() -> u64;
172 Instant { t: unsafe { mach_absolute_time() } }
175 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
176 let diff = self.t.checked_sub(other.t)?;
178 let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64);
179 Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32))
182 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
183 Some(Instant { t: self.t.checked_add(checked_dur2intervals(other)?)? })
186 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
187 Some(Instant { t: self.t.checked_sub(checked_dur2intervals(other)?)? })
192 pub fn now() -> SystemTime {
195 let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 };
196 cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap();
197 return SystemTime::from(s);
201 impl From<libc::timeval> for Timespec {
202 fn from(t: libc::timeval) -> Timespec {
203 Timespec::new(t.tv_sec as i64, 1000 * t.tv_usec as i64)
207 impl From<libc::timeval> for SystemTime {
208 fn from(t: libc::timeval) -> SystemTime {
209 SystemTime { t: Timespec::from(t) }
213 fn checked_dur2intervals(dur: &Duration) -> Option<u64> {
215 dur.as_secs().checked_mul(NSEC_PER_SEC)?.checked_add(dur.subsec_nanos() as u64)?;
217 Some(mul_div_u64(nanos, info.denom as u64, info.numer as u64))
220 fn info() -> mach_timebase_info {
221 // INFO_BITS conceptually is an `Option<mach_timebase_info>`. We can do
222 // this in 64 bits because we know 0 is never a valid value for the
225 // Encoding this as a single `AtomicU64` allows us to use `Relaxed`
226 // operations, as we are only interested in the effects on a single
228 static INFO_BITS: AtomicU64 = AtomicU64::new(0);
230 // If a previous thread has initialized `INFO_BITS`, use it.
231 let info_bits = INFO_BITS.load(Ordering::Relaxed);
233 return info_from_bits(info_bits);
236 // ... otherwise learn for ourselves ...
238 fn mach_timebase_info(info: mach_timebase_info_t) -> kern_return_t;
241 let mut info = info_from_bits(0);
243 mach_timebase_info(&mut info);
245 INFO_BITS.store(info_to_bits(info), Ordering::Relaxed);
250 fn info_to_bits(info: mach_timebase_info) -> u64 {
251 ((info.denom as u64) << 32) | (info.numer as u64)
255 fn info_from_bits(bits: u64) -> mach_timebase_info {
256 mach_timebase_info { numer: bits as u32, denom: (bits >> 32) as u32 }
260 #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "watchos")))]
263 use crate::mem::MaybeUninit;
265 use crate::time::Duration;
267 use super::{SystemTime, Timespec};
269 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
275 pub fn now() -> Instant {
276 Instant { t: Timespec::now(libc::CLOCK_MONOTONIC) }
279 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
280 self.t.sub_timespec(&other.t).ok()
283 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
284 Some(Instant { t: self.t.checked_add_duration(other)? })
287 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
288 Some(Instant { t: self.t.checked_sub_duration(other)? })
292 impl fmt::Debug for Instant {
293 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
294 f.debug_struct("Instant")
295 .field("tv_sec", &self.t.tv_sec)
296 .field("tv_nsec", &self.t.tv_nsec)
302 pub fn now() -> SystemTime {
303 SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
307 #[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon")))]
308 pub type clock_t = libc::c_int;
309 #[cfg(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon"))]
310 pub type clock_t = libc::c_ulong;
313 pub fn now(clock: clock_t) -> Timespec {
314 // Try to use 64-bit time in preparation for Y2038.
315 #[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
317 use crate::sys::weak::weak;
319 // __clock_gettime64 was added to 32-bit arches in glibc 2.34,
320 // and it handles both vDSO calls and ENOSYS fallbacks itself.
321 weak!(fn __clock_gettime64(libc::clockid_t, *mut __timespec64) -> libc::c_int);
324 struct __timespec64 {
326 #[cfg(target_endian = "big")]
329 #[cfg(target_endian = "little")]
333 if let Some(clock_gettime64) = __clock_gettime64.get() {
334 let mut t = MaybeUninit::uninit();
335 cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
336 let t = unsafe { t.assume_init() };
337 return Timespec { tv_sec: t.tv_sec, tv_nsec: t.tv_nsec as i64 };
341 let mut t = MaybeUninit::uninit();
342 cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
343 Timespec::from(unsafe { t.assume_init() })