Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9033c83

Browse files
committedSep 28, 2023
time: use clock_gettime on all unixen
This retires gettimeofday in favor of clock_gettime(CLOCK_REALTIME) on: ``` all(target_os = "macos", not(target_arch = "aarch64")), target_os = "ios", target_os = "watchos", target_os = "tvos" ))] ``` gettimeofday was first used in time-rs/time@cc367ed which predated the introduction of clock_gettime support in macOS 10.12 Sierra. As of 58bbca9, this is the minimum supported version.
1 parent f264d28 commit 9033c83

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed
 

‎library/std/src/sys/unix/time.rs

+35-49
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ impl SystemTime {
4040
SystemTime { t: Timespec::new(tv_sec, tv_nsec) }
4141
}
4242

43+
pub fn now() -> SystemTime {
44+
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
45+
}
46+
4347
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
4448
self.t.sub_timespec(&other.t)
4549
}
@@ -79,6 +83,36 @@ impl Timespec {
7983
Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
8084
}
8185

86+
pub fn now(clock: libc::clockid_t) -> Timespec {
87+
use crate::mem::MaybeUninit;
88+
use crate::sys::cvt;
89+
90+
// Try to use 64-bit time in preparation for Y2038.
91+
#[cfg(all(
92+
target_os = "linux",
93+
target_env = "gnu",
94+
target_pointer_width = "32",
95+
not(target_arch = "riscv32")
96+
))]
97+
{
98+
use crate::sys::weak::weak;
99+
100+
// __clock_gettime64 was added to 32-bit arches in glibc 2.34,
101+
// and it handles both vDSO calls and ENOSYS fallbacks itself.
102+
weak!(fn __clock_gettime64(libc::clockid_t, *mut super::__timespec64) -> libc::c_int);
103+
104+
if let Some(clock_gettime64) = __clock_gettime64.get() {
105+
let mut t = MaybeUninit::uninit();
106+
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
107+
return Timespec::from(unsafe { t.assume_init() });
108+
}
109+
}
110+
111+
let mut t = MaybeUninit::uninit();
112+
cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
113+
Timespec::from(unsafe { t.assume_init() })
114+
}
115+
82116
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
83117
if self >= other {
84118
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
@@ -224,7 +258,6 @@ impl From<__timespec64> for Timespec {
224258
))]
225259
mod inner {
226260
use crate::sync::atomic::{AtomicU64, Ordering};
227-
use crate::sys::cvt;
228261
use crate::sys_common::mul_div_u64;
229262
use crate::time::Duration;
230263

@@ -268,16 +301,6 @@ mod inner {
268301
}
269302
}
270303

271-
impl SystemTime {
272-
pub fn now() -> SystemTime {
273-
use crate::ptr;
274-
275-
let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 };
276-
cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap();
277-
return SystemTime::from(s);
278-
}
279-
}
280-
281304
impl From<libc::timeval> for Timespec {
282305
fn from(t: libc::timeval) -> Timespec {
283306
Timespec::new(t.tv_sec as i64, 1000 * t.tv_usec as i64)
@@ -345,11 +368,9 @@ mod inner {
345368
)))]
346369
mod inner {
347370
use crate::fmt;
348-
use crate::mem::MaybeUninit;
349-
use crate::sys::cvt;
350371
use crate::time::Duration;
351372

352-
use super::{SystemTime, Timespec};
373+
use super::Timespec;
353374

354375
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
355376
pub struct Instant {
@@ -386,39 +407,4 @@ mod inner {
386407
.finish()
387408
}
388409
}
389-
390-
impl SystemTime {
391-
pub fn now() -> SystemTime {
392-
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
393-
}
394-
}
395-
396-
impl Timespec {
397-
pub fn now(clock: libc::clockid_t) -> Timespec {
398-
// Try to use 64-bit time in preparation for Y2038.
399-
#[cfg(all(
400-
target_os = "linux",
401-
target_env = "gnu",
402-
target_pointer_width = "32",
403-
not(target_arch = "riscv32")
404-
))]
405-
{
406-
use crate::sys::weak::weak;
407-
408-
// __clock_gettime64 was added to 32-bit arches in glibc 2.34,
409-
// and it handles both vDSO calls and ENOSYS fallbacks itself.
410-
weak!(fn __clock_gettime64(libc::clockid_t, *mut super::__timespec64) -> libc::c_int);
411-
412-
if let Some(clock_gettime64) = __clock_gettime64.get() {
413-
let mut t = MaybeUninit::uninit();
414-
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
415-
return Timespec::from(unsafe { t.assume_init() });
416-
}
417-
}
418-
419-
let mut t = MaybeUninit::uninit();
420-
cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
421-
Timespec::from(unsafe { t.assume_init() })
422-
}
423-
}
424410
}

‎library/std/src/time.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub struct Instant(time::Instant);
221221
/// |-----------|----------------------------------------------------------------------|
222222
/// | SGX | [`insecure_time` usercall]. More information on [timekeeping in SGX] |
223223
/// | UNIX | [clock_gettime (Realtime Clock)] |
224-
/// | Darwin | [gettimeofday] |
224+
/// | Darwin | [clock_gettime (Realtime Clock)] |
225225
/// | VXWorks | [clock_gettime (Realtime Clock)] |
226226
/// | SOLID | `SOLID_RTC_ReadTime` |
227227
/// | WASI | [__wasi_clock_time_get (Realtime Clock)] |
@@ -230,7 +230,6 @@ pub struct Instant(time::Instant);
230230
/// [currently]: crate::io#platform-specific-behavior
231231
/// [`insecure_time` usercall]: https://edp.fortanix.com/docs/api/fortanix_sgx_abi/struct.Usercalls.html#method.insecure_time
232232
/// [timekeeping in SGX]: https://edp.fortanix.com/docs/concepts/rust-std/#codestdtimecode
233-
/// [gettimeofday]: https://man7.org/linux/man-pages/man2/gettimeofday.2.html
234233
/// [clock_gettime (Realtime Clock)]: https://linux.die.net/man/3/clock_gettime
235234
/// [__wasi_clock_time_get (Realtime Clock)]: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#clock_time_get
236235
/// [GetSystemTimePreciseAsFileTime]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime

0 commit comments

Comments
 (0)
Please sign in to comment.