This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function
pubfnwaitTimeout(cond: *Condition, io: Io, mutex: *Mutex, timeout: Timeout) WaitTimeoutError!void {
constdeadline = timeout.toDeadline(io);
varepoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before state load
{
constprev_state = cond.state.fetchAdd(.{ .waiters = 1, .signals = 0 }, .monotonic);
assert(prev_state.waiters < math.maxInt(u16)); // overflow caused by too many waiters
}
mutex.unlock(io);
defermutex.lockUncancelable(io);
while (true) {
constresult = io.futexWaitTimeout(u32, &cond.epoch.raw, epoch, deadline);
epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before `state` laod// We were woken normally, so try to consume a pending signal. A signal takes// priority over an expired deadline, so this is checked before the deadline// below. On error we safely remove ourselves as a waiter and propagate the error.if (result) |_| {
varprev_state = cond.state.load(.monotonic);
while (prev_state.signals > 0) {
prev_state = cond.state.cmpxchgWeak(prev_state, .{
.waiters = prev_state.waiters - 1,
.signals = prev_state.signals - 1,
}, .acquire, .monotonic) orelse {
// We successfully consumed a signal.return;
};
}
} else |err| {
cond.deregister(io);
returnerr;
}
// There are no signals available and no error; if a timeout was specified and// the deadline has passed, remove ourselves as a waiter and return// `error.Timeout`. Otherwise, this was a spurious wakeup: loop back to the// futex wait.switch (deadline) {
.none => {},
.deadline => |d| if (d.untilNow(io).raw.nanoseconds >= 0) {
cond.deregister(io);
returnerror.Timeout;
},
.duration => unreachable,
}
}
}
Blocks until the condition is signaled, canceled, or the provided timeout expires.
See also:
waitwaitUncancelable