wait
Wait until either:
- the
ptr's value changes fromexpect. Futex.wake()is called on theptr.- A spurious wake occurs.
- The deadline expires; In which case
error.Timeoutis 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
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);
}