Skip to content

Wait until deadline variant #231

@dead-claudia

Description

@dead-claudia

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_WAIT use absolute deadlines, and even that one has an absolute alternative documented in its manpages.
    • Fuschia only uses absolute deadlines in its zx_futex_wait syscall, and offers no relative equivalent.
    • macOS provides both os_sync_wait_on_address_with_deadline and os_sync_wait_on_address_with_timeout.
    • OpenBSD and Windows only offer relative timeouts in their APIs.
    • pthread_condvar_timedwait only uses absolute timeouts.
  • 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 *timecmp registers corresponding to *time targets 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions