Skip to content

Commit 97b49a0

Browse files
committed
Use __clock_gettime64 on 32-bit linux-gnu
1 parent bee923f commit 97b49a0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,33 @@ mod inner {
339339

340340
impl Timespec {
341341
pub fn now(clock: clock_t) -> Timespec {
342+
// Try to use 64-bit time in preparation for Y2038.
343+
#[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
344+
{
345+
use crate::sys::weak::weak;
346+
347+
// __clock_gettime64 was added to 32-bit arches in glibc 2.34,
348+
// and it handles both vDSO calls and ENOSYS fallbacks itself.
349+
weak!(fn __clock_gettime64(libc::clockid_t, *mut __timespec64) -> libc::c_int);
350+
351+
#[repr(C)]
352+
struct __timespec64 {
353+
tv_sec: i64,
354+
#[cfg(target_endian = "big")]
355+
_padding: i32,
356+
tv_nsec: i32,
357+
#[cfg(target_endian = "little")]
358+
_padding: i32,
359+
}
360+
361+
if let Some(clock_gettime64) = __clock_gettime64.get() {
362+
let mut t = MaybeUninit::uninit();
363+
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
364+
let t = unsafe { t.assume_init() };
365+
return Timespec { tv_sec: t.tv_sec, tv_nsec: t.tv_nsec as i64 };
366+
}
367+
}
368+
342369
let mut t = MaybeUninit::uninit();
343370
cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
344371
Timespec::from(unsafe { t.assume_init() })

0 commit comments

Comments
 (0)