authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-17 20:53:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-20 10:38:39-07:00
log363d7deb8a5dad9f66be84d3bb62a7b129c2bfad
tree5adee115c9b0c7ce99811b6b6e79dd53afffa46a
parent33b10abaf607d794c9fc379248c8b97c6195c053

std.Io: fix error handling and asyncParallel docs


3 files changed, 20 insertions(+), 20 deletions(-)

lib/std/Io.zig+10-10
......@@ -946,7 +946,7 @@ pub const VTable = struct {
946946 context: []const u8,
947947 context_alignment: std.mem.Alignment,
948948 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
949 ) ?*AnyFuture,
949 ) error{OutOfMemory}!*AnyFuture,
950950 /// Returning `null` indicates resource allocation failed.
951951 ///
952952 /// Thread-safe.
......@@ -959,7 +959,7 @@ pub const VTable = struct {
959959 context: []const u8,
960960 context_alignment: std.mem.Alignment,
961961 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
962 ) ?*AnyFuture,
962 ) error{OutOfMemory}!*AnyFuture,
963963 /// Executes `start` asynchronously in a manner such that it cleans itself
964964 /// up. This mode does not support results, await, or cancel.
965965 ///
......@@ -1573,7 +1573,7 @@ pub fn asyncConcurrent(
15731573 }
15741574 };
15751575 var future: Future(Result) = undefined;
1576 future.any_future = io.vtable.asyncConcurrent(
1576 future.any_future = try io.vtable.asyncConcurrent(
15771577 io.userdata,
15781578 @sizeOf(Result),
15791579 .of(Result),
......@@ -1584,14 +1584,14 @@ pub fn asyncConcurrent(
15841584 return future;
15851585}
15861586
1587/// Calls `function` with `args`, such that the return value of the function is
1588/// not guaranteed to be available until `await` is called, while simultaneously
1589/// passing control flow back to the caller.
1587/// Simultaneously calls `function` with `args` while passing control flow back
1588/// to the caller. The return value of the function is not guaranteed to be
1589/// available until `await` is called.
15901590///
15911591/// This has the strongest guarantees of all async family functions, placing
15921592/// the most restrictions on what kind of `Io` implementations are supported.
1593/// By calling `asyncConcurrent` instead, one allows, for example,
1594/// stackful single-threaded non-blocking I/O.
1593/// By calling `asyncConcurrent` instead, one allows, for example, stackful
1594/// single-threaded non-blocking I/O.
15951595///
15961596/// See also:
15971597/// * `asyncConcurrent`
......@@ -1611,9 +1611,9 @@ pub fn asyncParallel(
16111611 }
16121612 };
16131613 var future: Future(Result) = undefined;
1614 future.any_future = io.vtable.asyncConcurrent(
1614 future.any_future = try io.vtable.asyncParallel(
16151615 io.userdata,
1616 @ptrCast((&future.result)[0..1]),
1616 @sizeOf(Result),
16171617 .of(Result),
16181618 @ptrCast((&args)[0..1]),
16191619 .of(Args),
lib/std/Io/EventLoop.zig+4-4
......@@ -879,7 +879,7 @@ fn async(
879879 context_alignment: Alignment,
880880 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
881881) ?*std.Io.AnyFuture {
882 return asyncConcurrent(userdata, result.len, result_alignment, context, context_alignment, start) orelse {
882 return asyncConcurrent(userdata, result.len, result_alignment, context, context_alignment, start) catch {
883883 start(context.ptr, result.ptr);
884884 return null;
885885 };
......@@ -892,14 +892,14 @@ fn asyncConcurrent(
892892 context: []const u8,
893893 context_alignment: Alignment,
894894 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
895) ?*std.Io.AnyFuture {
895) error{OutOfMemory}!*std.Io.AnyFuture {
896896 assert(result_alignment.compare(.lte, Fiber.max_result_align)); // TODO
897897 assert(context_alignment.compare(.lte, Fiber.max_context_align)); // TODO
898898 assert(result_len <= Fiber.max_result_size); // TODO
899899 assert(context.len <= Fiber.max_context_size); // TODO
900900
901901 const event_loop: *EventLoop = @alignCast(@ptrCast(userdata));
902 const fiber = Fiber.allocate(event_loop) catch return null;
902 const fiber = try Fiber.allocate(event_loop);
903903 std.log.debug("allocated {*}", .{fiber});
904904
905905 const closure: *AsyncClosure = .fromFiber(fiber);
......@@ -945,7 +945,7 @@ fn asyncParallel(
945945 context: []const u8,
946946 context_alignment: Alignment,
947947 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
948) ?*std.Io.AnyFuture {
948) error{OutOfMemory}!*std.Io.AnyFuture {
949949 _ = userdata;
950950 _ = result_len;
951951 _ = result_alignment;
lib/std/Io/ThreadPool.zig+6-6
......@@ -220,7 +220,7 @@ fn async(
220220 }
221221 const pool: *Pool = @alignCast(@ptrCast(userdata));
222222 const cpu_count = pool.cpu_count catch {
223 return asyncParallel(userdata, result.len, result_alignment, context, context_alignment, start) orelse {
223 return asyncParallel(userdata, result.len, result_alignment, context, context_alignment, start) catch {
224224 start(context.ptr, result.ptr);
225225 return null;
226226 };
......@@ -291,8 +291,8 @@ fn asyncParallel(
291291 context: []const u8,
292292 context_alignment: std.mem.Alignment,
293293 start: *const fn (context: *const anyopaque, result: *anyopaque) void,
294) ?*Io.AnyFuture {
295 if (builtin.single_threaded) return null;
294) error{OutOfMemory}!*Io.AnyFuture {
295 if (builtin.single_threaded) unreachable;
296296
297297 const pool: *Pool = @alignCast(@ptrCast(userdata));
298298 const cpu_count = pool.cpu_count catch 1;
......@@ -300,7 +300,7 @@ fn asyncParallel(
300300 const context_offset = context_alignment.forward(@sizeOf(AsyncClosure));
301301 const result_offset = result_alignment.forward(context_offset + context.len);
302302 const n = result_offset + result_len;
303 const closure: *AsyncClosure = @alignCast(@ptrCast(gpa.alignedAlloc(u8, .of(AsyncClosure), n) catch return null));
303 const closure: *AsyncClosure = @alignCast(@ptrCast(try gpa.alignedAlloc(u8, .of(AsyncClosure), n)));
304304
305305 closure.* = .{
306306 .func = start,
......@@ -324,7 +324,7 @@ fn asyncParallel(
324324 pool.threads.ensureTotalCapacity(gpa, thread_capacity) catch {
325325 pool.mutex.unlock();
326326 closure.free(gpa, result_len);
327 return null;
327 return error.OutOfMemory;
328328 };
329329
330330 pool.run_queue.prepend(&closure.runnable.node);
......@@ -334,7 +334,7 @@ fn asyncParallel(
334334 assert(pool.run_queue.popFirst() == &closure.runnable.node);
335335 pool.mutex.unlock();
336336 closure.free(gpa, result_len);
337 return null;
337 return error.OutOfMemory;
338338 };
339339 pool.threads.appendAssumeCapacity(thread);
340340 }