Module time

Source
Expand description

The core AtomicTime for real-time and static clocks.

This module provides an atomic time abstraction that supports both real-time and static clocks. It ensures thread-safe operations and monotonic time retrieval with nanosecond precision.

§Modes

  • Real-time mode: The clock continuously syncs with system wall-clock time (via SystemTime::now()). To ensure strict monotonic increments across multiple threads, the internal updates use an atomic compare-and-exchange loop (time_since_epoch). While this guarantees that every new timestamp is at least one nanosecond greater than the last, it may introduce higher contention if many threads call it heavily.

  • Static mode: The clock is manually controlled via AtomicTime::set_time or AtomicTime::increment_time, which can be useful for simulations or backtesting. You can switch modes at runtime using AtomicTime::make_realtime or AtomicTime::make_static. In static mode, we use acquire/release semantics so that updates from one thread can be observed by another; however, we do not enforce strict global ordering for manual updates. If you need strong, multi-threaded ordering in static mode, you must coordinate higher-level synchronization yourself.

Structs§

AtomicTime
Represents an atomic timekeeping structure.

Statics§

ATOMIC_CLOCK_REALTIME
Global atomic time in real-time mode for use across the system.
ATOMIC_CLOCK_STATIC
Global atomic time in static mode for use across the system.

Functions§

duration_since_unix_epoch
Returns the duration since the UNIX epoch based on SystemTime::now().
get_atomic_clock_realtime
Returns a static reference to the global atomic clock in real-time mode.
get_atomic_clock_static
Returns a static reference to the global atomic clock in static mode.
nanos_since_unix_epoch
Returns the current UNIX time in nanoseconds, based on SystemTime::now().