authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-23 20:55:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:51-07:00
logae86c0f529d720ed4cae9e5f99064d6dba635144
treefd86ffeb2568f1aaa3e3976b1b7729ea8ed54300
parentecdc00466c34424ec8467d05efe0421f868c739a

std.Io: adjust concurrent error set

Now std.Io.Threaded can return error.ConcurrencyUnavailable rather than asserting. This is handy for logic that wants to try a concurrent implementation but then fall back to a synchronous one.

4 files changed, 23 insertions(+), 13 deletions(-)

lib/std/Io.zig+10-2
......@@ -552,6 +552,8 @@ test {
552552 _ = Reader;
553553 _ = Writer;
554554 _ = tty;
555 _ = Evented;
556 _ = Threaded;
555557 _ = @import("Io/test.zig");
556558}
557559
......@@ -596,7 +598,7 @@ pub const VTable = struct {
596598 context: []const u8,
597599 context_alignment: std.mem.Alignment,
598600 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
599 ) error{OutOfMemory}!*AnyFuture,
601 ) ConcurrentError!*AnyFuture,
600602 /// This function is only called when `async` returns a non-null value.
601603 ///
602604 /// Thread-safe.
......@@ -1557,6 +1559,12 @@ pub fn async(
15571559 return future;
15581560}
15591561
1562pub const ConcurrentError = error{
1563 /// May occur due to a temporary condition such as resource exhaustion, or
1564 /// to the Io implementation not supporting concurrency.
1565 ConcurrencyUnavailable,
1566};
1567
15601568/// Calls `function` with `args`, such that the return value of the function is
15611569/// not guaranteed to be available until `await` is called, allowing the caller
15621570/// to progress while waiting for any `Io` operations.
......@@ -1568,7 +1576,7 @@ pub fn concurrent(
15681576 io: Io,
15691577 function: anytype,
15701578 args: std.meta.ArgsTuple(@TypeOf(function)),
1571) error{OutOfMemory}!Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
1579) ConcurrentError!Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
15721580 const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?;
15731581 const Args = @TypeOf(args);
15741582 const TypeErased = struct {
lib/std/Io/IoUring.zig+1-1
......@@ -866,7 +866,7 @@ fn concurrent(
866866 context: []const u8,
867867 context_alignment: Alignment,
868868 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
869) error{OutOfMemory}!*std.Io.AnyFuture {
869) Io.ConcurrentError!*std.Io.AnyFuture {
870870 assert(result_alignment.compare(.lte, Fiber.max_result_align)); // TODO
871871 assert(context_alignment.compare(.lte, Fiber.max_context_align)); // TODO
872872 assert(result_len <= Fiber.max_result_size); // TODO
lib/std/Io/Kqueue.zig+2-2
......@@ -933,14 +933,14 @@ fn concurrent(
933933 context: []const u8,
934934 context_alignment: Alignment,
935935 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
936) error{OutOfMemory}!*Io.AnyFuture {
936) Io.ConcurrentError!*Io.AnyFuture {
937937 const k: *Kqueue = @ptrCast(@alignCast(userdata));
938938 assert(result_alignment.compare(.lte, Fiber.max_result_align)); // TODO
939939 assert(context_alignment.compare(.lte, Fiber.max_context_align)); // TODO
940940 assert(result_len <= Fiber.max_result_size); // TODO
941941 assert(context.len <= Fiber.max_context_size); // TODO
942942
943 const fiber = try Fiber.allocate(k);
943 const fiber = Fiber.allocate(k) catch return error.ConcurrencyUnavailable;
944944 std.log.debug("allocated {*}", .{fiber});
945945
946946 const closure: *AsyncClosure = .fromFiber(fiber);
lib/std/Io/Threaded.zig+10-8
......@@ -91,9 +91,9 @@ pub fn init(
9191 return t;
9292}
9393
94/// Statically initialize such that any call to the following functions will
95/// fail with `error.OutOfMemory`:
96/// * `Io.VTable.concurrent`
94/// Statically initialize such that calls to `Io.VTable.concurrent` will fail
95/// with `error.ConcurrencyUnavailable`.
96///
9797/// When initialized this way, `deinit` is safe, but unnecessary to call.
9898pub const init_single_threaded: Threaded = .{
9999 .allocator = .failing,
......@@ -481,8 +481,8 @@ fn concurrent(
481481 context: []const u8,
482482 context_alignment: std.mem.Alignment,
483483 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
484) error{OutOfMemory}!*Io.AnyFuture {
485 if (builtin.single_threaded) unreachable;
484) Io.ConcurrentError!*Io.AnyFuture {
485 if (builtin.single_threaded) return error.ConcurrencyUnavailable;
486486
487487 const t: *Threaded = @ptrCast(@alignCast(userdata));
488488 const cpu_count = t.cpu_count catch 1;
......@@ -490,7 +490,9 @@ fn concurrent(
490490 const context_offset = context_alignment.forward(@sizeOf(AsyncClosure));
491491 const result_offset = result_alignment.forward(context_offset + context.len);
492492 const n = result_offset + result_len;
493 const ac: *AsyncClosure = @ptrCast(@alignCast(try gpa.alignedAlloc(u8, .of(AsyncClosure), n)));
493 const ac_bytes = gpa.alignedAlloc(u8, .of(AsyncClosure), n) catch
494 return error.ConcurrencyUnavailable;
495 const ac: *AsyncClosure = @ptrCast(@alignCast(ac_bytes));
494496
495497 ac.* = .{
496498 .closure = .{
......@@ -515,7 +517,7 @@ fn concurrent(
515517 t.threads.ensureTotalCapacity(gpa, thread_capacity) catch {
516518 t.mutex.unlock();
517519 ac.free(gpa, result_len);
518 return error.OutOfMemory;
520 return error.ConcurrencyUnavailable;
519521 };
520522
521523 t.run_queue.prepend(&ac.closure.node);
......@@ -525,7 +527,7 @@ fn concurrent(
525527 assert(t.run_queue.popFirst() == &ac.closure.node);
526528 t.mutex.unlock();
527529 ac.free(gpa, result_len);
528 return error.OutOfMemory;
530 return error.ConcurrencyUnavailable;
529531 };
530532 t.threads.appendAssumeCapacity(thread);
531533 }