Deadline is used to wait efficiently for a pointer’s value to change using Futex and a fixed timeout.

Futex’s timedWait() api uses a relative duration which suffers from over-waiting when used in a loop which is often required due to the possibility of spurious wakeups.

Deadline instead converts the relative timeout to an absolute one so that multiple calls to Futex timedWait() can block for and report more accurate error.Timeouts.

Fields

timeout: ?u64,
started: std.time.Timer,

Functions

fn init(expires_in_ns: ?u64) Deadline

Create the deadline to expire after the given amount of time in nanoseconds pass…

Create the deadline to expire after the given amount of time in nanoseconds passes. Pass in null to have the deadline call Futex.wait() and never expire.

fn wait(self: *Deadline, ptr: *const Atomic(u32), expect: u32) error{Timeout}!void

Wait until either:

  • the ptr’s value changes from expect.
  • Futex.wake()

Wait until either:

  • the ptr’s value changes from expect.
  • Futex.wake() is called on the ptr.
  • A spurious wake occurs.
  • The deadline expires; In which case error.Timeout is returned.