Skip to content

Commit ab8dfd9

Browse files
committed
Switch Instant to use CLOCK_BOOTTIME on Android.
This is a long-term patch we have carried for the Android platform: https://android.googlesource.com/toolchain/android_rust/+/refs/heads/main/patches/longterm/rustc-0018-Switch-Instant-to-use-CLOCK_BOOTTIME.patch
1 parent 350d0ef commit ab8dfd9

File tree

1 file changed

+6
-1
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+6
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ pub struct Instant {
275275
impl Instant {
276276
#[cfg(target_vendor = "apple")]
277277
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
278-
#[cfg(not(target_vendor = "apple"))]
278+
#[cfg(target_os = "android")]
279+
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_BOOTTIME;
280+
#[cfg(not(any(target_vendor = "apple", target_os = "android")))]
279281
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_MONOTONIC;
280282
pub fn now() -> Instant {
281283
// https://www.manpagez.com/man/3/clock_gettime/
@@ -289,6 +291,9 @@ impl Instant {
289291
//
290292
// Instant on macos was historically implemented using mach_absolute_time;
291293
// we preserve this value domain out of an abundance of caution.
294+
//
295+
// On Android, we use CLOCK_BOOTTIME because we want the duration between
296+
// Instants to not be affected by suspends.
292297
Instant { t: Timespec::now(Self::CLOCK_ID) }
293298
}
294299

0 commit comments

Comments
 (0)