Expand description
Infrastructure for integrating time primitives into async runtimes.
This module provides the necessary components to bridge time-based operations with async runtime execution. The primary workflow involves:
- Start with an
InactiveClockthat can be safely moved across threads - Activate it using
InactiveClock::activateto get aClockandClockDriver - Use the
ClockDriverto periodically advance timers in your runtime loop - Use the
Clockfor time operations like creating timers and measuring time
§Integration with Runtimes
Different runtime architectures can integrate this module as follows:
§Thread-per-core Runtimes
In thread-per-core architectures, each thread should own an isolated clock with its own timer storage. This eliminates cross-thread lock contention and scales linearly.
The pattern is to clone the InactiveClock, relocate each clone to its target thread
using ThreadAware::relocate, and then activate:
let root = InactiveClock::default();
// Clone and relocate to each thread's affinity
let mut inactive_1 = root.clone();
inactive_1.relocate(Some(affinities[0]), affinities[0]);
let mut inactive_2 = root;
inactive_2.relocate(Some(affinities[1]), affinities[1]);
// On thread 1: activate and drive timers independently
let (clock_1, driver_1) = inactive_1.activate();
// On thread 2: activate and drive timers independently
let (clock_2, driver_2) = inactive_2.activate();After relocation, each thread’s clock and driver operate on an independent set of timers.
Timers registered on clock_1 are only visible to driver_1, and the other way around. Each driver
must be advanced independently by its owning thread.
§Multi-threaded Runtimes
In multi-threaded runtimes where tasks may run on any thread, activate once and share the clock across threads. The driver should be kept on a dedicated thread or task for timer advancement:
let (clock, driver) = InactiveClock::default().activate();
// Share `clock` across threads (it is Clone + Send + Sync)
// Keep `driver` on a single thread to advance timersStructs§
- Clock
Driver - Drives timer advancement for the clock.
- Clock
Gone - Error returned when all owners of a clock have been dropped.
- Inactive
Clock - An inactive clock that must be activated before time operations can be performed.
- Isolated
- Marker for an
InactiveClockbacked by per-core isolated timer storage. - Shared
- Marker for an
InactiveClockbacked by a single shared timer set.