Skip to content

Commit cdb6502

Browse files
authored
Use CLOCK_BOOTTIME on Fuchsia (#848)
Fuchsia added kernel support for a monotonic clock that keeps running while the system is suspended. They technically have not yet exposed it through their libc, but one maintainer said that they will do so soon.
1 parent 86bc01b commit cdb6502

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ tiny-skia = { version = "0.11.1", default-features = false, features = [
8484
tiny-skia-path = { version = "0.11.1", default-features = false, optional = true }
8585

8686
# SVG Rendering
87-
foldhash = { version = "0.1.3", optional = true }
87+
foldhash = { version = "0.1.3", default-features = false, optional = true }
8888

8989
# Networking
9090
splits-io-api = { version = "0.4.0", optional = true }
@@ -117,7 +117,7 @@ windows-sys = { version = "0.59.0", features = [
117117
"Win32_Graphics_Gdi",
118118
], optional = true }
119119

120-
[target.'cfg(any(target_os = "linux", target_os = "l4re", target_os = "android", target_os = "macos", target_os = "ios"))'.dependencies]
120+
[target.'cfg(any(target_os = "linux", target_os = "l4re", target_os = "android", target_os = "fuchsia", target_os = "macos", target_os = "ios"))'.dependencies]
121121
# We need libc for our own implementation of Instant
122122
libc = { version = "0.2.101", optional = true }
123123

src/platform/normal/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ cfg_if::cfg_if! {
1919
// correctly implemented in Linux and due to backwards compatibility
2020
// concerns they were never able to fix it properly. Thus `CLOCK_MONOTONIC`
2121
// means uptime on Linux whereas on other Unixes it means real time (the BSD
22-
// family). So the solution is to use this on all operating systems that are
23-
// based on the Linux kernel.
22+
// family). As a solution the Linux kernel provides `CLOCK_BOOTTIME` to
23+
// measure the real time.
2424
//
2525
// # macOS and iOS
2626
//
@@ -36,26 +36,19 @@ cfg_if::cfg_if! {
3636
//
3737
// # Fuchsia
3838
//
39-
// Fuchsia is based on the new Zircon kernel. It has two functions for
40-
// querying the time:
39+
// Fuchsia is based on the new Zircon kernel. It has `zx_clock_get_boot` to
40+
// query the real time and `zx_clock_get_monotonic` to query the uptime.
4141
//
42-
// zx_clock_get:
43-
// https://fuchsia.dev/fuchsia-src/reference/syscalls/clock_get
44-
// zx_clock_get_monotonic:
45-
// https://fuchsia.dev/fuchsia-src/reference/syscalls/clock_get_monotonic
42+
// https://fuchsia.dev/reference/syscalls/clock_get_boot
4643
//
47-
// `zx_clock_get_monotonic` specifically calls out that it `does not adjust
48-
// during sleep` which seems to mean that it doesn't count the time the OS
49-
// is suspended. This is further evidenced by their libc implementation not
50-
// treating `CLOCK_BOOTTIME` differently and a bug ticket being linked
51-
// there:
52-
// https://cs.opensource.google/fuchsia/fuchsia/+/main:zircon/third_party/ulib/musl/src/time/clock_gettime.c;l=40;drc=35e7a15cb21e16f0705560e5812b7a045d42c8a5
44+
// They are supposed to be available through `CLOCK_BOOTTIME` and
45+
// `CLOCK_MONOTONIC` respectively, just like on Linux.
5346
//
5447
// # WASI
5548
//
5649
// https://github.com/WebAssembly/WASI/blob/5ab83a68d4eb4f218a898ed03b963b7393caaedc/phases/snapshot/docs.md#variant-cases
5750
//
58-
// WASI seems to underspecify its `monotonic` a bit, but says that it `is
51+
// WASI seems to under specify its `monotonic` a bit, but says that it `is
5952
// defined as a clock measuring real time`, making it sound like a compliant
6053
// implementation should measure the time the OS is suspended as well.
6154
//
@@ -98,10 +91,14 @@ cfg_if::cfg_if! {
9891
// the Linux kernel and instead has its own implementation in JavaScript
9992
// where it actually errors out on `CLOCK_BOOTTIME`:
10093
// https://github.com/emscripten-core/emscripten/blob/9bdb310b89472a0f4d64f36e4a79273d8dc7fa98/system/lib/libc/emscripten_time.c#L50-L57
94+
//
95+
// And we add Fuchsia to this list as it's based on the Zircon kernel which
96+
// uses `CLOCK_BOOTTIME` in the same way as Linux.
10197
if #[cfg(any(
10298
target_os = "linux",
10399
target_os = "l4re",
104100
target_os = "android",
101+
target_os = "fuchsia",
105102
))] {
106103
use core::{mem::MaybeUninit, ops::Sub};
107104

0 commit comments

Comments
 (0)