Skip to content

Commit

Permalink
aio/coro: rename batch to complete
Browse files Browse the repository at this point in the history
A much less confusing name as it has less overlap with multi
  • Loading branch information
Cloudef committed Jun 19, 2024
1 parent 7be8553 commit bb8e666
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions docs/pages/aio-immediate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ try aio.multi(.{
The `.link_next` field of operation can be used to link the operation to the next operation.
When linking operations, the next operation won't start until this operation is complete.

#### Using batch
#### Using complete

Batch is similar to multi, but it will not return `error.SomeOperationFailed` in case any of the operations fail.
Instead batch returns `aio.CompletionResult` which contains the number of operations that was completed, and number of
Complete is similar to multi, but it will not return `error.SomeOperationFailed` in case any of the operations fail.
Instead complete returns `aio.CompletionResult` which contains the number of operations that was completed, and number of
errors that occured. To find out which operations failed, errors have to be stored somewhere by setting the `.out_error`
field of the operation. The batch call may still fail in implementation defined ways, such as running out of system resources.
field of the operation. The complete call may still fail in implementation defined ways, such as running out of system resources.

```zig
var my_buffer: [1024]u8 = undefined;
var my_len: usize = undefined;
var write_error: std.posix.WriteError = undefined;
var read_error: std.posix.ReadError = undefined;
const res = try aio.batch(.{
const res = try aio.complete(.{
aio.Write{.file = f, .buffer = "contents", .out_error = &write_error, .link_next = true},
aio.Read{.file = f, .buffer = &my_buffer, .out_error = &read_error, .out_read = &my_len},
});
Expand Down
2 changes: 1 addition & 1 deletion examples/aio_static.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn main() !void {
var buf2: [4096]u8 = undefined;
var len2: usize = 0;

const ret = try aio.batch(.{
const ret = try aio.complete(.{
aio.Read{
.file = f,
.buffer = &buf,
Expand Down
6 changes: 3 additions & 3 deletions src/aio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub const Dynamic = struct {

/// Completes a list of operations immediately, blocks until complete
/// For error handling you must check the `out_error` field in the operation
pub inline fn batch(operations: anytype) ImmediateError!CompletionResult {
pub inline fn complete(operations: anytype) ImmediateError!CompletionResult {
const ti = @typeInfo(@TypeOf(operations));
if (comptime ti == .Struct and ti.Struct.is_tuple) {
return IO.immediate(operations.len, &struct { ops: @TypeOf(operations) }{ .ops = operations });
Expand All @@ -90,7 +90,7 @@ pub inline fn batch(operations: anytype) ImmediateError!CompletionResult {
/// Completes a list of operations immediately, blocks until complete
/// Returns `error.SomeOperationFailed` if any operation failed
pub inline fn multi(operations: anytype) (ImmediateError || error{SomeOperationFailed})!void {
const res = try batch(operations);
const res = try complete(operations);
if (res.num_errors > 0) return error.SomeOperationFailed;
}

Expand All @@ -99,7 +99,7 @@ pub inline fn single(operation: anytype) (ImmediateError || OperationError)!void
var op: @TypeOf(operation) = operation;
var err: @TypeOf(operation).Error = error.Success;
op.out_error = &err;
_ = try batch(.{op});
_ = try complete(.{op});
if (err != error.Success) return err;
}

Expand Down
6 changes: 3 additions & 3 deletions src/coro.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub const io = struct {
/// The IO operations can be cancelled by calling `wakeup`
/// For error handling you must check the `out_error` field in the operation
/// Returns the number of errors occured, 0 if there were no errors
pub inline fn batch(operations: anytype) aio.QueueError!u16 {
pub inline fn complete(operations: anytype) aio.QueueError!u16 {
if (Fiber.current()) |fiber| {
var task: *Scheduler.TaskState = @ptrFromInt(fiber.getUserDataPtr().*);

Expand Down Expand Up @@ -86,7 +86,7 @@ pub const io = struct {
/// The IO operations can be cancelled by calling `wakeupFromIo`, or doing `aio.Cancel`
/// Returns `error.SomeOperationFailed` if any operation failed
pub inline fn multi(operations: anytype) (aio.QueueError || error{SomeOperationFailed})!void {
if (try batch(operations) > 0) return error.SomeOperationFailed;
if (try complete(operations) > 0) return error.SomeOperationFailed;
}

/// Completes a single operation immediately, blocks the coroutine until complete
Expand All @@ -95,7 +95,7 @@ pub const io = struct {
var op: @TypeOf(operation) = operation;
var err: @TypeOf(operation).Error = error.Success;
op.out_error = &err;
_ = try batch(.{op});
_ = try complete(.{op});
if (err != error.Success) return err;
}
};
Expand Down

0 comments on commit bb8e666

Please sign in to comment.