Skip to main content

Module coarse_clock

Module coarse_clock 

Source
Expand description

A coarse wall clock read off the per-lane hot path (issue #1792 item 4).

The voucher/preimage accept path reads the wall clock twice while it holds the per-lane Mutex: the capability-expiry gate and the last_voucher_at liveness stamp. Both tolerate roughly one-second resolution — expiry is an on-chain grant deadline, and the stamp only drives the admin “seconds since last voucher” readout — so paying a SystemTime::now() syscall inside that critical section is pure per-proof overhead that lengthens the most contended lock hold on the paid path.

CoarseClock turns each of those two syscalls into a single relaxed AtomicU64 load (still two reads per proof, one load each). A background task (CoarseClock::spawn_refresher) stamps the current wall clock into the cell on a timer; the hot path just reads it.

Fallback. Until a refresher runs the cell holds 0, and every read falls back to a live SystemTime::now(). So a clock with no refresher attached (every unit and loopback test, which build a handler without one) reports the exact wall clock on every read — the coarsening is a production-only optimization, not a semantic change. 0 is a safe sentinel: a live millisecond reading is 0 only at the Unix epoch itself, which a running node’s clock is not at, so a 0 in the cell means “unrefreshed”, not “midnight 1970”. Even a stray 0 (a refresher that wrote one, or a clock pinned there) is harmless — reads just route to a live SystemTime::now() until the next tick, correct but not coarse.

Structs§

CoarseClock
A wall clock the paid-delivery hot path can read with a single relaxed atomic load instead of a SystemTime::now() syscall (issue #1792 item 4).