]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/time.rs
Rollup merge of #105955 - Nilstrieb:no-trivial-opt-wrappers-we-have-field-accesses...
[rust.git] / library / std / src / sys / unix / time.rs
1 use crate::fmt;
2 use crate::time::Duration;
3
4 pub use self::inner::Instant;
5
6 const NSEC_PER_SEC: u64 = 1_000_000_000;
7 pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
8 #[allow(dead_code)] // Used for pthread condvar timeouts
9 pub const TIMESPEC_MAX: libc::timespec =
10     libc::timespec { tv_sec: <libc::time_t>::MAX, tv_nsec: 1_000_000_000 - 1 };
11
12 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13 #[repr(transparent)]
14 #[rustc_layout_scalar_valid_range_start(0)]
15 #[rustc_layout_scalar_valid_range_end(999_999_999)]
16 struct Nanoseconds(u32);
17
18 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
19 pub struct SystemTime {
20     pub(in crate::sys::unix) t: Timespec,
21 }
22
23 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24 pub(in crate::sys::unix) struct Timespec {
25     tv_sec: i64,
26     tv_nsec: Nanoseconds,
27 }
28
29 impl SystemTime {
30     #[cfg_attr(target_os = "horizon", allow(unused))]
31     pub fn new(tv_sec: i64, tv_nsec: i64) -> SystemTime {
32         SystemTime { t: Timespec::new(tv_sec, tv_nsec) }
33     }
34
35     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36         self.t.sub_timespec(&other.t)
37     }
38
39     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
40         Some(SystemTime { t: self.t.checked_add_duration(other)? })
41     }
42
43     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
44         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
45     }
46 }
47
48 impl From<libc::timespec> for SystemTime {
49     fn from(t: libc::timespec) -> SystemTime {
50         SystemTime { t: Timespec::from(t) }
51     }
52 }
53
54 impl fmt::Debug for SystemTime {
55     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56         f.debug_struct("SystemTime")
57             .field("tv_sec", &self.t.tv_sec)
58             .field("tv_nsec", &self.t.tv_nsec.0)
59             .finish()
60     }
61 }
62
63 impl Timespec {
64     pub const fn zero() -> Timespec {
65         Timespec::new(0, 0)
66     }
67
68     const fn new(tv_sec: i64, tv_nsec: i64) -> Timespec {
69         assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64);
70         // SAFETY: The assert above checks tv_nsec is within the valid range
71         Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
72     }
73
74     pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
75         if self >= other {
76             // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
77             // to optimize it into a branchless form (see also #75545):
78             //
79             // 1. `self.tv_sec - other.tv_sec` shows up as a common expression
80             //    in both branches, i.e. the `else` must have its `- 1`
81             //    subtraction after the common one, not interleaved with it
82             //    (it used to be `self.tv_sec - 1 - other.tv_sec`)
83             //
84             // 2. the `Duration::new` call (or any other additional complexity)
85             //    is outside of the `if`-`else`, not duplicated in both branches
86             //
87             // Ideally this code could be rearranged such that it more
88             // directly expresses the lower-cost behavior we want from it.
89             let (secs, nsec) = if self.tv_nsec.0 >= other.tv_nsec.0 {
90                 ((self.tv_sec - other.tv_sec) as u64, self.tv_nsec.0 - other.tv_nsec.0)
91             } else {
92                 (
93                     (self.tv_sec - other.tv_sec - 1) as u64,
94                     self.tv_nsec.0 + (NSEC_PER_SEC as u32) - other.tv_nsec.0,
95                 )
96             };
97
98             Ok(Duration::new(secs, nsec))
99         } else {
100             match other.sub_timespec(self) {
101                 Ok(d) => Err(d),
102                 Err(d) => Ok(d),
103             }
104         }
105     }
106
107     pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
108         let mut secs = other
109             .as_secs()
110             .try_into() // <- target type would be `i64`
111             .ok()
112             .and_then(|secs| self.tv_sec.checked_add(secs))?;
113
114         // Nano calculations can't overflow because nanos are <1B which fit
115         // in a u32.
116         let mut nsec = other.subsec_nanos() + self.tv_nsec.0;
117         if nsec >= NSEC_PER_SEC as u32 {
118             nsec -= NSEC_PER_SEC as u32;
119             secs = secs.checked_add(1)?;
120         }
121         Some(Timespec::new(secs, nsec as i64))
122     }
123
124     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
125         let mut secs = other
126             .as_secs()
127             .try_into() // <- target type would be `i64`
128             .ok()
129             .and_then(|secs| self.tv_sec.checked_sub(secs))?;
130
131         // Similar to above, nanos can't overflow.
132         let mut nsec = self.tv_nsec.0 as i32 - other.subsec_nanos() as i32;
133         if nsec < 0 {
134             nsec += NSEC_PER_SEC as i32;
135             secs = secs.checked_sub(1)?;
136         }
137         Some(Timespec::new(secs, nsec as i64))
138     }
139
140     #[allow(dead_code)]
141     pub fn to_timespec(&self) -> Option<libc::timespec> {
142         Some(libc::timespec {
143             tv_sec: self.tv_sec.try_into().ok()?,
144             tv_nsec: self.tv_nsec.0.try_into().ok()?,
145         })
146     }
147 }
148
149 impl From<libc::timespec> for Timespec {
150     fn from(t: libc::timespec) -> Timespec {
151         Timespec::new(t.tv_sec as i64, t.tv_nsec as i64)
152     }
153 }
154
155 #[cfg(any(
156     all(target_os = "macos", any(not(target_arch = "aarch64"))),
157     target_os = "ios",
158     target_os = "watchos"
159 ))]
160 mod inner {
161     use crate::sync::atomic::{AtomicU64, Ordering};
162     use crate::sys::cvt;
163     use crate::sys_common::mul_div_u64;
164     use crate::time::Duration;
165
166     use super::{SystemTime, Timespec, NSEC_PER_SEC};
167
168     #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
169     pub struct Instant {
170         t: u64,
171     }
172
173     #[repr(C)]
174     #[derive(Copy, Clone)]
175     struct mach_timebase_info {
176         numer: u32,
177         denom: u32,
178     }
179     type mach_timebase_info_t = *mut mach_timebase_info;
180     type kern_return_t = libc::c_int;
181
182     impl Instant {
183         pub fn now() -> Instant {
184             extern "C" {
185                 fn mach_absolute_time() -> u64;
186             }
187             Instant { t: unsafe { mach_absolute_time() } }
188         }
189
190         pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
191             let diff = self.t.checked_sub(other.t)?;
192             let info = info();
193             let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64);
194             Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32))
195         }
196
197         pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
198             Some(Instant { t: self.t.checked_add(checked_dur2intervals(other)?)? })
199         }
200
201         pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
202             Some(Instant { t: self.t.checked_sub(checked_dur2intervals(other)?)? })
203         }
204     }
205
206     impl SystemTime {
207         pub fn now() -> SystemTime {
208             use crate::ptr;
209
210             let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 };
211             cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap();
212             return SystemTime::from(s);
213         }
214     }
215
216     impl From<libc::timeval> for Timespec {
217         fn from(t: libc::timeval) -> Timespec {
218             Timespec::new(t.tv_sec as i64, 1000 * t.tv_usec as i64)
219         }
220     }
221
222     impl From<libc::timeval> for SystemTime {
223         fn from(t: libc::timeval) -> SystemTime {
224             SystemTime { t: Timespec::from(t) }
225         }
226     }
227
228     fn checked_dur2intervals(dur: &Duration) -> Option<u64> {
229         let nanos =
230             dur.as_secs().checked_mul(NSEC_PER_SEC)?.checked_add(dur.subsec_nanos() as u64)?;
231         let info = info();
232         Some(mul_div_u64(nanos, info.denom as u64, info.numer as u64))
233     }
234
235     fn info() -> mach_timebase_info {
236         // INFO_BITS conceptually is an `Option<mach_timebase_info>`. We can do
237         // this in 64 bits because we know 0 is never a valid value for the
238         // `denom` field.
239         //
240         // Encoding this as a single `AtomicU64` allows us to use `Relaxed`
241         // operations, as we are only interested in the effects on a single
242         // memory location.
243         static INFO_BITS: AtomicU64 = AtomicU64::new(0);
244
245         // If a previous thread has initialized `INFO_BITS`, use it.
246         let info_bits = INFO_BITS.load(Ordering::Relaxed);
247         if info_bits != 0 {
248             return info_from_bits(info_bits);
249         }
250
251         // ... otherwise learn for ourselves ...
252         extern "C" {
253             fn mach_timebase_info(info: mach_timebase_info_t) -> kern_return_t;
254         }
255
256         let mut info = info_from_bits(0);
257         unsafe {
258             mach_timebase_info(&mut info);
259         }
260         INFO_BITS.store(info_to_bits(info), Ordering::Relaxed);
261         info
262     }
263
264     #[inline]
265     fn info_to_bits(info: mach_timebase_info) -> u64 {
266         ((info.denom as u64) << 32) | (info.numer as u64)
267     }
268
269     #[inline]
270     fn info_from_bits(bits: u64) -> mach_timebase_info {
271         mach_timebase_info { numer: bits as u32, denom: (bits >> 32) as u32 }
272     }
273 }
274
275 #[cfg(not(any(
276     all(target_os = "macos", any(not(target_arch = "aarch64"))),
277     target_os = "ios",
278     target_os = "watchos"
279 )))]
280 mod inner {
281     use crate::fmt;
282     use crate::mem::MaybeUninit;
283     use crate::sys::cvt;
284     use crate::time::Duration;
285
286     use super::{SystemTime, Timespec};
287
288     #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
289     pub struct Instant {
290         t: Timespec,
291     }
292
293     impl Instant {
294         pub fn now() -> Instant {
295             #[cfg(target_os = "macos")]
296             const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
297             #[cfg(not(target_os = "macos"))]
298             const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;
299             Instant { t: Timespec::now(clock_id) }
300         }
301
302         pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
303             self.t.sub_timespec(&other.t).ok()
304         }
305
306         pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
307             Some(Instant { t: self.t.checked_add_duration(other)? })
308         }
309
310         pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
311             Some(Instant { t: self.t.checked_sub_duration(other)? })
312         }
313     }
314
315     impl fmt::Debug for Instant {
316         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
317             f.debug_struct("Instant")
318                 .field("tv_sec", &self.t.tv_sec)
319                 .field("tv_nsec", &self.t.tv_nsec.0)
320                 .finish()
321         }
322     }
323
324     impl SystemTime {
325         pub fn now() -> SystemTime {
326             SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
327         }
328     }
329
330     impl Timespec {
331         pub fn now(clock: libc::clockid_t) -> Timespec {
332             // Try to use 64-bit time in preparation for Y2038.
333             #[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
334             {
335                 use crate::sys::weak::weak;
336
337                 // __clock_gettime64 was added to 32-bit arches in glibc 2.34,
338                 // and it handles both vDSO calls and ENOSYS fallbacks itself.
339                 weak!(fn __clock_gettime64(libc::clockid_t, *mut __timespec64) -> libc::c_int);
340
341                 #[repr(C)]
342                 struct __timespec64 {
343                     tv_sec: i64,
344                     #[cfg(target_endian = "big")]
345                     _padding: i32,
346                     tv_nsec: i32,
347                     #[cfg(target_endian = "little")]
348                     _padding: i32,
349                 }
350
351                 if let Some(clock_gettime64) = __clock_gettime64.get() {
352                     let mut t = MaybeUninit::uninit();
353                     cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
354                     let t = unsafe { t.assume_init() };
355                     return Timespec::new(t.tv_sec, t.tv_nsec as i64);
356                 }
357             }
358
359             let mut t = MaybeUninit::uninit();
360             cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
361             Timespec::from(unsafe { t.assume_init() })
362         }
363     }
364 }