Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_platform/src/time/fallback.rs
6849 views
1
//! Provides a fallback implementation of `Instant` from the standard library.
2
3
#![expect(
4
unsafe_code,
5
reason = "Instant fallback requires unsafe to allow users to update the internal value"
6
)]
7
8
use crate::sync::atomic::{AtomicPtr, Ordering};
9
10
use core::{
11
fmt,
12
ops::{Add, AddAssign, Sub, SubAssign},
13
time::Duration,
14
};
15
16
static ELAPSED_GETTER: AtomicPtr<()> = AtomicPtr::new(unset_getter as *mut _);
17
18
/// Fallback implementation of `Instant` suitable for a `no_std` environment.
19
///
20
/// If you are on any of the following target architectures, this is a drop-in replacement:
21
///
22
/// - `x86`
23
/// - `x86_64`
24
/// - `aarch64`
25
///
26
/// On any other architecture, you must call [`Instant::set_elapsed`], providing a method
27
/// which when called supplies a monotonically increasing count of elapsed nanoseconds relative
28
/// to some arbitrary point in time.
29
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
30
pub struct Instant(Duration);
31
32
impl Instant {
33
/// Returns an instant corresponding to "now".
34
#[must_use]
35
pub fn now() -> Instant {
36
let getter = ELAPSED_GETTER.load(Ordering::Acquire);
37
38
// SAFETY: Function pointer is always valid
39
let getter = unsafe { core::mem::transmute::<*mut (), fn() -> Duration>(getter) };
40
41
Self((getter)())
42
}
43
44
/// Provides a function returning the amount of time that has elapsed since execution began.
45
/// The getter provided to this method will be used by [`now`](Instant::now).
46
///
47
/// # Safety
48
///
49
/// - The function provided must accurately represent the elapsed time.
50
/// - The function must preserve all invariants of the [`Instant`] type.
51
/// - The pointer to the function must be valid whenever [`Instant::now`] is called.
52
pub unsafe fn set_elapsed(getter: fn() -> Duration) {
53
ELAPSED_GETTER.store(getter as *mut _, Ordering::Release);
54
}
55
56
/// Returns the amount of time elapsed from another instant to this one,
57
/// or zero duration if that instant is later than this one.
58
#[must_use]
59
pub fn duration_since(&self, earlier: Instant) -> Duration {
60
self.saturating_duration_since(earlier)
61
}
62
63
/// Returns the amount of time elapsed from another instant to this one,
64
/// or None if that instant is later than this one.
65
///
66
/// Due to monotonicity bugs, even under correct logical ordering of the passed `Instant`s,
67
/// this method can return `None`.
68
#[must_use]
69
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
70
self.0.checked_sub(earlier.0)
71
}
72
73
/// Returns the amount of time elapsed from another instant to this one,
74
/// or zero duration if that instant is later than this one.
75
#[must_use]
76
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
77
self.0.saturating_sub(earlier.0)
78
}
79
80
/// Returns the amount of time elapsed since this instant.
81
#[must_use]
82
pub fn elapsed(&self) -> Duration {
83
Instant::now().saturating_duration_since(*self)
84
}
85
86
/// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
87
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
88
/// otherwise.
89
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
90
self.0.checked_add(duration).map(Instant)
91
}
92
93
/// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
94
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
95
/// otherwise.
96
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
97
self.0.checked_sub(duration).map(Instant)
98
}
99
}
100
101
impl Add<Duration> for Instant {
102
type Output = Instant;
103
104
/// # Panics
105
///
106
/// This function may panic if the resulting point in time cannot be represented by the
107
/// underlying data structure. See [`Instant::checked_add`] for a version without panic.
108
fn add(self, other: Duration) -> Instant {
109
self.checked_add(other)
110
.expect("overflow when adding duration to instant")
111
}
112
}
113
114
impl AddAssign<Duration> for Instant {
115
fn add_assign(&mut self, other: Duration) {
116
*self = *self + other;
117
}
118
}
119
120
impl Sub<Duration> for Instant {
121
type Output = Instant;
122
123
fn sub(self, other: Duration) -> Instant {
124
self.checked_sub(other)
125
.expect("overflow when subtracting duration from instant")
126
}
127
}
128
129
impl SubAssign<Duration> for Instant {
130
fn sub_assign(&mut self, other: Duration) {
131
*self = *self - other;
132
}
133
}
134
135
impl Sub<Instant> for Instant {
136
type Output = Duration;
137
138
/// Returns the amount of time elapsed from another instant to this one,
139
/// or zero duration if that instant is later than this one.
140
fn sub(self, other: Instant) -> Duration {
141
self.duration_since(other)
142
}
143
}
144
145
impl fmt::Debug for Instant {
146
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147
self.0.fmt(f)
148
}
149
}
150
151
fn unset_getter() -> Duration {
152
crate::cfg::switch! {
153
#[cfg(target_arch = "x86")] => {
154
// SAFETY: standard technique for getting a nanosecond counter on x86
155
let nanos = unsafe {
156
core::arch::x86::_rdtsc()
157
};
158
Duration::from_nanos(nanos)
159
}
160
#[cfg(target_arch = "x86_64")] => {
161
// SAFETY: standard technique for getting a nanosecond counter on x86_64
162
let nanos = unsafe {
163
core::arch::x86_64::_rdtsc()
164
};
165
Duration::from_nanos(nanos)
166
}
167
#[cfg(target_arch = "aarch64")] => {
168
// SAFETY: standard technique for getting a nanosecond counter of aarch64
169
let nanos = unsafe {
170
let mut ticks: u64;
171
core::arch::asm!("mrs {}, cntvct_el0", out(reg) ticks);
172
ticks
173
};
174
Duration::from_nanos(nanos)
175
}
176
_ => {
177
panic!("An elapsed time getter has not been provided to `Instant`. Please use `Instant::set_elapsed(...)` before calling `Instant::now()`")
178
}
179
}
180
}
181
182