-
Couldn't load subscription status.
- Fork 51
Description
This obviously depends on #230.
Waiting for a duration is useful for simple blocking waits, but event loops generally find deadlines more convenient to work with, since you can just throw all the deadlines into a heap, sort by deadline, and (if your loop is multi-threaded) make your wait condition include timer add/remove events. Browser runtimes would already likely need a similar model anyways because of Atomics.waitAsync, but it's easy for a runtime using one model to expose the other.
Also, the type of wait differs heavily between platforms, with some accepting absolute deadlines, some accepting relative timeouts, and some accepting both:
- Operating systems:
- Linux: all futex waits other than
FUTEX_WAITuse absolute deadlines, and even that one has an absolute alternative documented in its manpages. - Fuschia only uses absolute deadlines in its
zx_futex_waitsyscall, and offers no relative equivalent. - macOS provides both
os_sync_wait_on_address_with_deadlineandos_sync_wait_on_address_with_timeout. - OpenBSD and Windows only offer relative timeouts in their APIs.
pthread_condvar_timedwaitonly uses absolute timeouts.
- Linux: all futex waits other than
- Embedded hardware:
- 32-bit ARM's SysTick is effectively timeout-based. It decrements every tick until it hits zero, in which it then triggers an interrupt. Setting the register essentially sets the number of ticks to wait before sending the interrupt.
- RISC-V's privileged spec uses
*timecmpregisters corresponding to*timetargets and triggers an interrupt whenever they're equal, effectively using a deadline-based system. - x86-64's HPET works nearly identically to RISC-V's mechanism. (This mostly only has relevance for VM-based server-side runtimes.)
- The real-time clock specified by ACPI for motherboards uses deadlines. (This is sometimes used on high-end microcontrollers.)
Will note that it's easy to do one in terms of the other.
- If all waits are absolute, relative waits are as simple as using a deadline of
current_time() + timeout. - If all waits are relative, absolute waits are as simple as using a timeout of
max(target_time - current_time(), 0).