Skip to content

Commit

Permalink
fix: switch to clock_gettime(3) for macOS/iOS for monotonic clock (#97
Browse files Browse the repository at this point in the history
)

This fixes an issue where on M1 (and presumably M2/M3) hardware,
the usage of mach_absolute_time() resulted in undermeasuring the
passage of time when compared to std::time::Instant. Now we read
from the monotonic clock (using clock_gettime, but conceptually
the same as calling mach_continuous_time()) which properly tracks
the passage of time regardless of device sleep, frequency, etc.
  • Loading branch information
tobz committed Dec 31, 2023
1 parent cc2c4b1 commit 0e6d83d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
matrix:
rust_version: ['1.60.0', 'stable', 'nightly']
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-20.04, ubuntu-22.04, macOS-11, macOS-12, windows-2019, windows-2022]
steps:
- uses: actions/checkout@v3
- name: Install Rust ${{ matrix.rust_version }}
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - ReleaseDate

### Fixed

- Fixed an issue with the monotonic clock for macOS/iOS where it would undermeasure time compared to
what would be measured by `std::time::Instant` due to using a clock source that did not account
for device sleep.

## [0.12.1] - 2023-10-31

### Fixed
Expand Down
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ raw-cpuid = "11.0"
[target.'cfg(target_arch = "x86_64")'.dependencies]
raw-cpuid = "11.0"

[target.'cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows", target_arch = "wasm32")))'.dependencies]
[target.'cfg(not(any(target_os = "windows", target_arch = "wasm32")))'.dependencies]
libc = "0.2"

[target.'cfg(target_os = "macos")'.dependencies]
mach2 = "0.4"

[target.'cfg(target_os = "ios")'.dependencies]
mach2 = "0.4"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["profileapi"] }

Expand Down
24 changes: 0 additions & 24 deletions src/clocks/monotonic/macos.rs

This file was deleted.

19 changes: 2 additions & 17 deletions src/clocks/monotonic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod macos;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use self::macos::Monotonic;

#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
Expand All @@ -13,17 +8,7 @@ mod wasm;
#[cfg(target_arch = "wasm32")]
pub use self::wasm::Monotonic;

#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "windows",
target_arch = "wasm32"
)))]
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
mod unix;
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "windows",
target_arch = "wasm32"
)))]
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
pub use self::unix::Monotonic;

0 comments on commit 0e6d83d

Please sign in to comment.