DoxigAlpha

wait

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.

Function parameters

Parameters

#
self:*Deadline
ptr:*const atomic.Value(u32)
expect:u32

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

Types

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

Checks if `ptr` still contains the value `expect` and, if so, blocks the caller until either:

Functions

#
wait
Checks if `ptr` still contains the value `expect` and, if so, blocks the caller until either:
timedWait
Checks if `ptr` still contains the value `expect` and, if so, blocks the caller until either:
wake
Unblocks at most `max_waiters` callers blocked in a `wait()` call on `ptr`.

Source

Implementation

#
pub fn wait(self: *Deadline, ptr: *const atomic.Value(u32), expect: u32) error{Timeout}!void {
    @branchHint(.cold);

    // Check if we actually have a timeout to wait until.
    // If not just wait "forever".
    const timeout_ns = self.timeout orelse {
        return Futex.wait(ptr, expect);
    };

    // Get how much time has passed since we started waiting
    // then subtract that from the init() timeout to get how much longer to wait.
    // Use overflow to detect when we've been waiting longer than the init() timeout.
    const elapsed_ns = self.started.read();
    const until_timeout_ns = std.math.sub(u64, timeout_ns, elapsed_ns) catch 0;
    return Futex.timedWait(ptr, expect, until_timeout_ns);
}