diff --git a/lib/std/Io.zig b/lib/std/Io.zig index 03b33dae06d069496a800737c6891ab560e8db9f..b44cbe8b4ea8fa025e66dbfa6eda4b8092e7cdce 100644 --- a/lib/std/Io.zig +++ b/lib/std/Io.zig @@ -1681,20 +1681,29 @@ pub const Condition = struct { .epoch = .init(0), }; + /// Blocks until the condition is signaled or canceled. + /// + /// See also: + /// * `waitUncancelable` + /// * `waitTimeout` pub fn wait(cond: *Condition, io: Io, mutex: *Mutex) Cancelable!void { - try waitInner(cond, io, mutex, false); - } - - /// Same as `wait`, except does not introduce a cancelation point. - /// - /// For a description of cancelation and cancelation points, see `Future.cancel`. - pub fn waitUncancelable(cond: *Condition, io: Io, mutex: *Mutex) void { - waitInner(cond, io, mutex, true) catch |err| switch (err) { - error.Canceled => unreachable, + waitTimeout(cond, io, mutex, .none) catch |err| switch (err) { + error.Timeout => unreachable, + error.Canceled => |e| return e, }; } - fn waitInner(cond: *Condition, io: Io, mutex: *Mutex, uncancelable: bool) Cancelable!void { + pub const WaitTimeoutError = Cancelable || Timeout.Error; + + /// Blocks until the condition is signaled, canceled, or the provided + /// timeout expires. + /// + /// See also: + /// * `wait` + /// * `waitUncancelable` + pub fn waitTimeout(cond: *Condition, io: Io, mutex: *Mutex, timeout: Timeout) WaitTimeoutError!void { + const deadline = timeout.toDeadline(io); + var epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before state load { @@ -1706,10 +1715,7 @@ pub const Condition = struct { defer mutex.lockUncancelable(io); while (true) { - const result = if (uncancelable) - io.futexWaitUncancelable(u32, &cond.epoch.raw, epoch) - else - io.futexWait(u32, &cond.epoch.raw, epoch); + const result = io.futexWaitTimeout(u32, &cond.epoch.raw, epoch, deadline); epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before `state` laod @@ -1729,13 +1735,62 @@ pub const Condition = struct { } // There are no more signals available; this was a spurious wakeup or an error. If it - // was an error, we will remove ourselves as a waiter and return that error. Otherwise, - // we'll loop back to the futex wait. + // was an error, we will remove ourselves as a waiter and return that error. If a + // timeout was specified and the deadline has passed, we remove ourselves as a waiter + // and return `error.Timeout`. Otherwise, we'll loop back to the futex wait. result catch |err| { const prev_state = cond.state.fetchSub(.{ .waiters = 1, .signals = 0 }, .monotonic); assert(prev_state.waiters > 0); // underflow caused by illegal state return err; }; + switch (deadline) { + .none => {}, + .deadline => |d| if (d.untilNow(io).raw.nanoseconds >= 0) { + const prev_state = cond.state.fetchSub(.{ .waiters = 1, .signals = 0 }, .monotonic); + assert(prev_state.waiters > 0); // underflow caused by illegal state + return error.Timeout; + }, + .duration => unreachable, + } + } + } + + /// Same as `wait`, except does not introduce a cancelation point. + /// + /// See `Future.cancel` for a description of cancelation points. + pub fn waitUncancelable(cond: *Condition, io: Io, mutex: *Mutex) void { + var epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before state load + + { + const prev_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); + defer mutex.lockUncancelable(io); + + while (true) { + io.futexWaitUncancelable(u32, &cond.epoch.raw, epoch); + + epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before `state` laod + + // Even on error, try to consume a pending signal first. Otherwise a race might + // cause a signal to get stuck in the state with no corresponding waiter. + { + var prev_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; + }; + } + } + + // There are no more signals available; this was a spurious wakeup, + // so we'll loop back to the futex wait. } } diff --git a/lib/std/Io/test.zig b/lib/std/Io/test.zig index bd6b433d058540ec95225662a0a51038f005a971..20426acb9c82f357bdff97940148028cddd91d2c 100644 --- a/lib/std/Io/test.zig +++ b/lib/std/Io/test.zig @@ -971,3 +971,91 @@ test "Select.cancel with no tasks, no deadlock" { var select: Io.Select(U) = .init(io, &.{}); try expectEqual(null, select.cancel()); } + +test "Condition.waitTimeout" { + const io = testing.io; + + const Context = struct { + ready: Io.Event = .unset, + mutex: Io.Mutex = .init, + cond: Io.Condition = .init, + value: u32 = 0, + + fn worker(ctx: *@This()) !void { + defer ctx.ready.set(io); + + try ctx.mutex.lock(io); + defer ctx.mutex.unlock(io); + + try expectError(error.Timeout, ctx.cond.waitTimeout(io, &ctx.mutex, .{ .duration = .{ + .raw = .fromMilliseconds(1), + .clock = .awake, + } })); + try expectEqual(0, ctx.value); + + ctx.ready.set(io); + + while (ctx.value == 0) try ctx.cond.wait(io, &ctx.mutex); + try expectEqual(1, ctx.value); + } + }; + + var ctx: Context = .{}; + + var future = io.concurrent(Context.worker, .{&ctx}) catch |err| switch (err) { + error.ConcurrencyUnavailable => return error.SkipZigTest, + }; + defer future.cancel(io) catch {}; + + try ctx.ready.wait(io); + + try ctx.mutex.lock(io); + ctx.value = 1; + ctx.mutex.unlock(io); + ctx.cond.signal(io); + + try future.await(io); +} + +test "Condition.waitUncancelable" { + const io = testing.io; + + const Context = struct { + ready: Io.Event = .unset, + mutex: Io.Mutex = .init, + cond: Io.Condition = .init, + value: u32 = 0, + + fn worker(ctx: *@This()) !void { + defer ctx.ready.set(io); + + try ctx.mutex.lock(io); + defer ctx.mutex.unlock(io); + + try expectEqual(0, ctx.value); + + ctx.ready.set(io); + + ctx.cond.waitUncancelable(io, &ctx.mutex); + + while (ctx.value == 0) try ctx.cond.wait(io, &ctx.mutex); + try expectEqual(1, ctx.value); + } + }; + + var ctx: Context = .{}; + + var future = io.concurrent(Context.worker, .{&ctx}) catch |err| switch (err) { + error.ConcurrencyUnavailable => return error.SkipZigTest, + }; + defer future.cancel(io) catch {}; + + try ctx.ready.wait(io); + + try ctx.mutex.lock(io); + ctx.value = 1; + ctx.mutex.unlock(io); + ctx.cond.signal(io); + + try future.await(io); +}