-
Notifications
You must be signed in to change notification settings - Fork 51
Open
Description
Sleep timers are normally batched for performance, with durations turned into deadlines, and effective batching requires tracking a monotonic clock to know precisely how long to wait for. In fact, processors normally mandate such batching at the operating system level. And this primitive is implemented on every platform, from embedded CPUs to high-performance operating systems, as it's a strict necessity.
Here's the API on various relevant platforms:
- Operating systems:
- Most Unix-like:
clock_gettime(CLOCK_MONOTONIC, &tv);
- Linux:
clock_gettime(CLOCK_BOOTTIME, &tv);
- Windows:
QueryInterruptTime(&ticks);
- Most Unix-like:
- The ACPI timer can be used as a clock reference even on older desktops.
- Processors:
- x86, x86-64: HPET,
rdtsc * base frequency
on modern processors - ARM: Generic Timer system register
- RISC-V: memory-mapped
mtime
- AVR doesn't natively, but real-time clock peripherals are pervasive, and Arduinos have them built-in.
- Others also have their various ways - just look at the Linux VDSO
clock_gettime
implementations for various architectures.
- x86, x86-64: HPET,
I propose something like an atomic.clock.get
that returns an i64
nanosecond count, to align with the existing wait opcodes.
Metadata
Metadata
Assignees
Labels
No labels