Skip to content

Commit 7a47af8

Browse files
authored
Enable CLOCK_BOOTTIME on more platforms. (#757)
Enable `CLOCK_BOOTTIME` and `CLOCK_BOOTTIME_ALARM` on more platforms, and add a testcase for it.
1 parent cf827c7 commit 7a47af8

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/backend/libc/time/syscalls.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,15 @@ pub(crate) fn clock_gettime_dynamic(id: DynamicClockId<'_>) -> io::Result<Timesp
166166
#[cfg(linux_kernel)]
167167
DynamicClockId::Tai => c::CLOCK_TAI,
168168

169-
#[cfg(any(linux_kernel, target_os = "openbsd"))]
169+
#[cfg(any(
170+
freebsdlike,
171+
linux_kernel,
172+
target_os = "fuchsia",
173+
target_os = "openbsd"
174+
))]
170175
DynamicClockId::Boottime => c::CLOCK_BOOTTIME,
171176

172-
#[cfg(linux_kernel)]
177+
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
173178
DynamicClockId::BoottimeAlarm => c::CLOCK_BOOTTIME_ALARM,
174179
};
175180

src/clockid.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ pub enum DynamicClockId<'a> {
9696
Tai,
9797

9898
/// `CLOCK_BOOTTIME`, available on Linux >= 2.6.39
99-
#[cfg(any(linux_kernel, target_os = "openbsd"))]
99+
#[cfg(any(
100+
freebsdlike,
101+
linux_kernel,
102+
target_os = "fuchsia",
103+
target_os = "openbsd"
104+
))]
100105
Boottime,
101106

102107
/// `CLOCK_BOOTTIME_ALARM`, available on Linux >= 2.6.39
103-
#[cfg(linux_kernel)]
108+
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
104109
BoottimeAlarm,
105110
}

tests/time/clocks.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
66
use rustix::time::{clock_gettime, ClockId};
77

8+
/// Attempt to test that the boot clock is monotonic. Time may or may not
9+
/// advance, but it shouldn't regress.
10+
#[cfg(any(
11+
freebsdlike,
12+
linux_kernel,
13+
target_os = "fuchsia",
14+
target_os = "openbsd"
15+
))]
16+
#[test]
17+
fn test_boottime_clock() {
18+
use rustix::time::{clock_gettime_dynamic, DynamicClockId};
19+
20+
if let Ok(a) = clock_gettime_dynamic(DynamicClockId::Boottime) {
21+
if let Ok(b) = clock_gettime_dynamic(DynamicClockId::Boottime) {
22+
if b.tv_sec == a.tv_sec {
23+
assert!(b.tv_nsec >= a.tv_nsec);
24+
} else {
25+
assert!(b.tv_sec > a.tv_sec);
26+
}
27+
}
28+
}
29+
}
30+
831
/// Attempt to test that the uptime clock is monotonic. Time may or may not
932
/// advance, but it shouldn't regress.
1033
#[cfg(any(freebsdlike, target_os = "openbsd"))]

0 commit comments

Comments
 (0)