1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
//! This crate contains abstractions to interact with hardware clocks.
#![no_std]
mod dummy;
use core::{fmt, ops};
use crossbeam_utils::atomic::AtomicCell;
pub use core::time::Duration;
const FEMTOS_TO_NANOS: u128 = 1_000_000;
static EARLY_SLEEP_FUNCTION: AtomicCell<fn(Duration)> = AtomicCell::new(dummy::early_sleep);
static EARLY_SLEEPER_PERIOD: AtomicCell<Period> = AtomicCell::new(Period::MAX);
static MONOTONIC_NOW_FUNCTION: AtomicCell<fn() -> Instant> = AtomicCell::new(dummy::monotonic_now);
static MONOTONIC_PERIOD: AtomicCell<Period> = AtomicCell::new(Period::MAX);
static WALL_TIME_NOW_FUNCTION: AtomicCell<fn() -> Duration> = AtomicCell::new(dummy::wall_time_now);
static WALL_TIME_PERIOD: AtomicCell<Period> = AtomicCell::new(Period::MAX);
/// A measurement of a monotonically nondecreasing clock.
///
/// The inner value usually represents the internal counter value but the type
/// is intentionally opaque and only useful with [`Duration`].
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Instant {
counter: u64,
}
impl Instant {
pub const ZERO: Self = Self { counter: 0 };
pub const MAX: Self = Self { counter: u64::MAX };
pub fn new(counter: u64) -> Self {
Self { counter }
}
pub fn now() -> Self {
now::<Monotonic>()
}
pub fn elapsed(&self) -> Duration {
Self::now().duration_since(*self)
}
/// Returns the amount of time elapsed from another instant to this one, or
/// zero duration if that instant is later than this one.
pub fn duration_since(&self, earlier: Self) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
}
pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
let instant = Instant {
counter: self.counter.checked_sub(earlier.counter)?,
};
let femtos = u128::from(instant.counter) * u128::from(MONOTONIC_PERIOD.load());
Some(Duration::from_nanos((femtos / FEMTOS_TO_NANOS) as u64))
}
}
impl Default for Instant {
fn default() -> Self {
Self::ZERO
}
}
impl ops::Add<Duration> for Instant {
type Output = Self;
fn add(self, rhs: Duration) -> Self::Output {
let femtos = rhs.as_nanos() * FEMTOS_TO_NANOS;
let ticks = (femtos / u128::from(MONOTONIC_PERIOD.load())) as u64;
Self {
counter: self
.counter
.checked_add(ticks)
.expect("overflow when adding duration to instant"),
}
}
}
impl ops::AddAssign<Duration> for Instant {
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}
impl ops::Sub<Duration> for Instant {
type Output = Self;
fn sub(self, rhs: Duration) -> Self::Output {
let femtos = rhs.as_nanos() * FEMTOS_TO_NANOS;
let ticks = (femtos / u128::from(MONOTONIC_PERIOD.load())) as u64;
Self {
counter: self
.counter
.checked_sub(ticks)
.expect("overflow when subtracting duration from instant"),
}
}
}
impl ops::Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, rhs: Instant) -> Self::Output {
self.duration_since(rhs)
}
}
impl ops::SubAssign<Duration> for Instant {
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}
/// A clock period, measured in femtoseconds.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Period(u64);
impl Period {
const MAX: Self = Self(u64::MAX);
/// Creates a new period with the specified femtoseconds.
pub fn new(period: u64) -> Self {
Self(period)
}
}
impl From<Period> for u64 {
/// Returns the period in femtoseconds.
fn from(f: Period) -> Self {
f.0
}
}
impl From<Period> for u128 {
/// Returns the period in femtoseconds.
fn from(f: Period) -> Self {
f.0.into()
}
}
impl From<u64> for Period {
/// Creates a new period with the specified femtoseconds.
fn from(period: u64) -> Self {
Self(period)
}
}
impl fmt::Display for Period {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} fs", self.0)
}
}
/// Register the clock source that can be used to sleep when interrupts are
/// disabled.
///
/// The provided early sleeper will overwrite the current early sleeper only if
/// `period` is smaller than that of the current early sleeper.
///
/// Returns whether the early sleeper was overwritten.
pub fn register_early_sleeper<T>(period: Period) -> bool
where
T: EarlySleeper,
{
if EARLY_SLEEPER_PERIOD
.fetch_update(|old_period| {
if period < old_period {
Some(period)
} else {
None
}
})
.is_ok()
{
EARLY_SLEEP_FUNCTION.store(T::sleep);
true
} else {
false
}
}
/// Wait for the given `duration`.
///
/// This function spins the current task rather than sleeping it and so, when
/// possible, the `sleep` crate should be used. However, unlike the `sleep`
/// crate, this function doesn't rely on interrupts.
///
/// This function must not be called prior to registering an early sleeper using
/// [`register_early_sleeper`].
pub fn early_sleep(duration: Duration) {
let f = EARLY_SLEEP_FUNCTION.load();
f(duration)
}
/// Register a clock source.
///
/// The provided clock source will overwrite the current clock source only if
/// `period` is smaller than that of the current clock source.
///
/// Returns whether the clock source was overwritten.
pub fn register_clock_source<T>(period: Period) -> bool
where
T: ClockSource,
{
let period_atomic = T::ClockType::period_atomic();
if period_atomic
.fetch_update(|old_period| {
if period < old_period {
Some(period)
} else {
None
}
})
.is_ok()
{
let now_fn = T::ClockType::now_fn();
now_fn.store(T::now);
true
} else {
false
}
}
/// Returns the current time.
///
/// Monotonic clocks return an [`Instant`] whereas wall time clocks return a
/// [`Duration`] signifying the time since 12:00am January 1st 1970 (i.e. Unix
/// time).
///
/// This function must not be called prior to registering a clock source of the
/// specified type using [`register_clock_source`].
pub fn now<T>() -> T::Unit
where
T: ClockType,
{
let f = T::now_fn().load();
f()
}
/// A clock source.
pub trait ClockSource {
/// The type of clock (either [`Monotonic`] or [`WallTime`]).
type ClockType: ClockType;
/// The current time according to the clock.
///
/// Monotonic clocks return an [`Instant`] whereas wall time clocks return a
/// [`Duration`] signifying the time since 12:00am January 1st 1970 (i.e.
/// Unix time).
fn now() -> <Self::ClockType as ClockType>::Unit;
}
/// A hardware clock that can sleep without relying on interrupts.
pub trait EarlySleeper: ClockSource<ClockType = Monotonic> {
/// Wait for the given `duration`.
///
/// This function spins the current task rather than sleeping it and so,
/// when possible, the `sleep` crate should be used.
///
/// However, unlike the `sleep` crate, this function doesn't rely on
/// interrupts, and can be used prior to the scheduler being initiated.
///
/// # Note to Implementors
///
/// The default implementation of this function uses [`ClockSource::now`] -
/// it can only be used if [`ClockSource::now`] doesn't rely on
/// interrupts.
fn sleep(duration: Duration) {
let start = Self::now();
while Self::now() < start + duration {}
}
}
/// Either a [`Monotonic`] or [`WallTime`] clock.
///
/// This trait is sealed and so cannot be implemented outside of this crate.
pub trait ClockType: private::Sealed {
/// The type returned by the [`now`] function.
type Unit: 'static;
#[doc(hidden)]
fn now_fn() -> &'static AtomicCell<fn() -> Self::Unit>;
#[doc(hidden)]
fn period_atomic() -> &'static AtomicCell<Period>;
}
pub struct Monotonic;
impl private::Sealed for Monotonic {}
impl ClockType for Monotonic {
type Unit = Instant;
fn now_fn() -> &'static AtomicCell<fn() -> Self::Unit> {
&MONOTONIC_NOW_FUNCTION
}
fn period_atomic() -> &'static AtomicCell<Period> {
&MONOTONIC_PERIOD
}
}
pub struct WallTime;
impl private::Sealed for WallTime {}
impl ClockType for WallTime {
type Unit = Duration;
fn now_fn() -> &'static AtomicCell<fn() -> Self::Unit> {
&WALL_TIME_NOW_FUNCTION
}
fn period_atomic() -> &'static AtomicCell<Period> {
&WALL_TIME_PERIOD
}
}
mod private {
/// This trait is a supertrait of [`Clocktype`](super::ClockType).
///
/// Since it's in a private module, it can't be implemented by types outside
/// this crate and thus neither can [`Clocktype`](super::ClockType).
pub trait Sealed {}
}