authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-04-03 17:54:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-20 10:38:39-07:00
log0f67ea4fa45ffd62a81d906447ca821488ef0598
tree4f76b16d5d9e54f18e100b0c8b8a7aba512b9e4e
parent7b5886118dabb59967d3e9b17d0502146df2ef92

introduce Io.select and implement it in thread pool


2 files changed, 228 insertions(+), 78 deletions(-)

lib/std/Io.zig+155-60
......@@ -979,17 +979,21 @@ pub const VTable = struct {
979979 /// Thread-safe.
980980 cancelRequested: *const fn (?*anyopaque) bool,
981981
982 /// Blocks until one of the futures from the list has a result ready, such
983 /// that awaiting it will not block. Returns that index.
984 select: *const fn (?*anyopaque, futures: []const *AnyFuture) usize,
985
982986 mutexLock: *const fn (?*anyopaque, prev_state: Mutex.State, mutex: *Mutex) Cancelable!void,
983987 mutexUnlock: *const fn (?*anyopaque, prev_state: Mutex.State, mutex: *Mutex) void,
984988
985989 conditionWait: *const fn (?*anyopaque, cond: *Condition, mutex: *Mutex) Cancelable!void,
986990 conditionWake: *const fn (?*anyopaque, cond: *Condition, wake: Condition.Wake) void,
987991
988 createFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) FileOpenError!fs.File,
989 openFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) FileOpenError!fs.File,
990 closeFile: *const fn (?*anyopaque, fs.File) void,
991 pread: *const fn (?*anyopaque, file: fs.File, buffer: []u8, offset: std.posix.off_t) FilePReadError!usize,
992 pwrite: *const fn (?*anyopaque, file: fs.File, buffer: []const u8, offset: std.posix.off_t) FilePWriteError!usize,
992 createFile: *const fn (?*anyopaque, dir: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File,
993 openFile: *const fn (?*anyopaque, dir: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File,
994 closeFile: *const fn (?*anyopaque, File) void,
995 pread: *const fn (?*anyopaque, file: File, buffer: []u8, offset: std.posix.off_t) File.PReadError!usize,
996 pwrite: *const fn (?*anyopaque, file: File, buffer: []const u8, offset: std.posix.off_t) File.PWriteError!usize,
993997
994998 now: *const fn (?*anyopaque, clockid: std.posix.clockid_t) ClockGetTimeError!Timestamp,
995999 sleep: *const fn (?*anyopaque, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void,
......@@ -1000,28 +1004,118 @@ pub const Cancelable = error{
10001004 Canceled,
10011005};
10021006
1003pub const OpenFlags = fs.File.OpenFlags;
1004pub const CreateFlags = fs.File.CreateFlags;
1007pub const Dir = struct {
1008 handle: Handle,
1009
1010 pub fn cwd() Dir {
1011 return .{ .handle = std.fs.cwd().fd };
1012 }
1013
1014 pub const Handle = std.posix.fd_t;
1015
1016 pub fn openFile(dir: Dir, io: Io, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
1017 return io.vtable.openFile(io.userdata, dir, sub_path, flags);
1018 }
1019
1020 pub fn createFile(dir: Dir, io: Io, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
1021 return io.vtable.createFile(io.userdata, dir, sub_path, flags);
1022 }
1023
1024 pub const WriteFileOptions = struct {
1025 /// On Windows, `sub_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
1026 /// On WASI, `sub_path` should be encoded as valid UTF-8.
1027 /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding.
1028 sub_path: []const u8,
1029 data: []const u8,
1030 flags: File.CreateFlags = .{},
1031 };
1032
1033 pub const WriteFileError = File.WriteError || File.OpenError || Cancelable;
1034
1035 /// Writes content to the file system, using the file creation flags provided.
1036 pub fn writeFile(dir: Dir, io: Io, options: WriteFileOptions) WriteFileError!void {
1037 var file = try dir.createFile(io, options.sub_path, options.flags);
1038 defer file.close(io);
1039 try file.writeAll(io, options.data);
1040 }
1041};
1042
1043pub const File = struct {
1044 handle: Handle,
1045
1046 pub const Handle = std.posix.fd_t;
10051047
1006pub const FileOpenError = fs.File.OpenError || Cancelable;
1007pub const FileReadError = fs.File.ReadError || Cancelable;
1008pub const FilePReadError = fs.File.PReadError || Cancelable;
1009pub const FileWriteError = fs.File.WriteError || Cancelable;
1010pub const FilePWriteError = fs.File.PWriteError || Cancelable;
1048 pub const OpenFlags = fs.File.OpenFlags;
1049 pub const CreateFlags = fs.File.CreateFlags;
1050
1051 pub const OpenError = fs.File.OpenError || Cancelable;
1052
1053 pub fn close(file: File, io: Io) void {
1054 return io.vtable.closeFile(io.userdata, file);
1055 }
1056
1057 pub const ReadError = fs.File.ReadError || Cancelable;
1058
1059 pub fn read(file: File, io: Io, buffer: []u8) ReadError!usize {
1060 return @errorCast(file.pread(io, buffer, -1));
1061 }
1062
1063 pub const PReadError = fs.File.PReadError || Cancelable;
1064
1065 pub fn pread(file: File, io: Io, buffer: []u8, offset: std.posix.off_t) PReadError!usize {
1066 return io.vtable.pread(io.userdata, file, buffer, offset);
1067 }
1068
1069 pub const WriteError = fs.File.WriteError || Cancelable;
1070
1071 pub fn write(file: File, io: Io, buffer: []const u8) WriteError!usize {
1072 return @errorCast(file.pwrite(io, buffer, -1));
1073 }
1074
1075 pub const PWriteError = fs.File.PWriteError || Cancelable;
1076
1077 pub fn pwrite(file: File, io: Io, buffer: []const u8, offset: std.posix.off_t) PWriteError!usize {
1078 return io.vtable.pwrite(io.userdata, file, buffer, offset);
1079 }
1080
1081 pub fn writeAll(file: File, io: Io, bytes: []const u8) WriteError!void {
1082 var index: usize = 0;
1083 while (index < bytes.len) {
1084 index += try file.write(io, bytes[index..]);
1085 }
1086 }
1087
1088 pub fn readAll(file: File, io: Io, buffer: []u8) ReadError!usize {
1089 var index: usize = 0;
1090 while (index != buffer.len) {
1091 const amt = try file.read(io, buffer[index..]);
1092 if (amt == 0) break;
1093 index += amt;
1094 }
1095 return index;
1096 }
1097};
10111098
10121099pub const Timestamp = enum(i96) {
10131100 _,
10141101
1015 pub fn durationTo(from: Timestamp, to: Timestamp) i96 {
1016 return @intFromEnum(to) - @intFromEnum(from);
1102 pub fn durationTo(from: Timestamp, to: Timestamp) Duration {
1103 return .{ .nanoseconds = @intFromEnum(to) - @intFromEnum(from) };
10171104 }
10181105
1019 pub fn addDuration(from: Timestamp, duration: i96) Timestamp {
1020 return @enumFromInt(@intFromEnum(from) + duration);
1106 pub fn addDuration(from: Timestamp, duration: Duration) Timestamp {
1107 return @enumFromInt(@intFromEnum(from) + duration.nanoseconds);
10211108 }
10221109};
1023pub const Deadline = union(enum) {
1110pub const Duration = struct {
10241111 nanoseconds: i96,
1112
1113 pub fn ms(x: u64) Duration {
1114 return .{ .nanoseconds = @as(i96, x) * std.time.ns_per_ms };
1115 }
1116};
1117pub const Deadline = union(enum) {
1118 duration: Duration,
10251119 timestamp: Timestamp,
10261120};
10271121pub const ClockGetTimeError = std.posix.ClockGetTimeError || Cancelable;
......@@ -1408,7 +1502,7 @@ pub fn Queue(Elem: type) type {
14081502
14091503/// Calls `function` with `args`, such that the return value of the function is
14101504/// not guaranteed to be available until `await` is called.
1411pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
1505pub fn async(io: Io, function: anytype, args: std.meta.ArgsTuple(@TypeOf(function))) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
14121506 const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?;
14131507 const Args = @TypeOf(args);
14141508 const TypeErased = struct {
......@@ -1432,7 +1526,7 @@ pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(
14321526
14331527/// Calls `function` with `args` asynchronously. The resource cleans itself up
14341528/// when the function returns. Does not support await, cancel, or a return value.
1435pub fn go(io: Io, function: anytype, args: anytype) void {
1529pub fn go(io: Io, function: anytype, args: std.meta.ArgsTuple(@TypeOf(function))) void {
14361530 const Args = @TypeOf(args);
14371531 const TypeErased = struct {
14381532 fn start(context: *const anyopaque) void {
......@@ -1448,55 +1542,56 @@ pub fn go(io: Io, function: anytype, args: anytype) void {
14481542 );
14491543}
14501544
1451pub fn openFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) FileOpenError!fs.File {
1452 return io.vtable.openFile(io.userdata, dir, sub_path, flags);
1453}
1454
1455pub fn createFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) FileOpenError!fs.File {
1456 return io.vtable.createFile(io.userdata, dir, sub_path, flags);
1457}
1458
1459pub fn closeFile(io: Io, file: fs.File) void {
1460 return io.vtable.closeFile(io.userdata, file);
1461}
1462
1463pub fn read(io: Io, file: fs.File, buffer: []u8) FileReadError!usize {
1464 return @errorCast(io.pread(file, buffer, -1));
1465}
1466
1467pub fn pread(io: Io, file: fs.File, buffer: []u8, offset: std.posix.off_t) FilePReadError!usize {
1468 return io.vtable.pread(io.userdata, file, buffer, offset);
1545pub fn now(io: Io, clockid: std.posix.clockid_t) ClockGetTimeError!Timestamp {
1546 return io.vtable.now(io.userdata, clockid);
14691547}
14701548
1471pub fn write(io: Io, file: fs.File, buffer: []const u8) FileWriteError!usize {
1472 return @errorCast(io.pwrite(file, buffer, -1));
1549pub fn sleep(io: Io, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void {
1550 return io.vtable.sleep(io.userdata, clockid, deadline);
14731551}
14741552
1475pub fn pwrite(io: Io, file: fs.File, buffer: []const u8, offset: std.posix.off_t) FilePWriteError!usize {
1476 return io.vtable.pwrite(io.userdata, file, buffer, offset);
1553pub fn sleepDuration(io: Io, duration: Duration) SleepError!void {
1554 return io.vtable.sleep(io.userdata, .MONOTONIC, .{ .duration = duration });
14771555}
14781556
1479pub fn writeAll(io: Io, file: fs.File, bytes: []const u8) FileWriteError!void {
1480 var index: usize = 0;
1481 while (index < bytes.len) {
1482 index += try io.write(file, bytes[index..]);
1557/// Given a struct with each field a `*Future`, returns a union with the same
1558/// fields, each field type the future's result.
1559pub fn SelectUnion(S: type) type {
1560 const struct_fields = @typeInfo(S).@"struct".fields;
1561 var fields: [struct_fields.len]std.builtin.Type.UnionField = undefined;
1562 for (&fields, struct_fields) |*union_field, struct_field| {
1563 const F = @typeInfo(struct_field.type).pointer.child;
1564 const Result = @TypeOf(@as(F, undefined).result);
1565 union_field.* = .{
1566 .name = struct_field.name,
1567 .type = Result,
1568 .alignment = struct_field.alignment,
1569 };
14831570 }
1571 return @Type(.{ .@"union" = .{
1572 .layout = .auto,
1573 .tag_type = std.meta.FieldEnum(S),
1574 .fields = &fields,
1575 .decls = &.{},
1576 } });
14841577}
14851578
1486pub fn readAll(io: Io, file: fs.File, buffer: []u8) FileReadError!usize {
1487 var index: usize = 0;
1488 while (index != buffer.len) {
1489 const amt = try io.read(file, buffer[index..]);
1490 if (amt == 0) break;
1491 index += amt;
1579/// `s` is a struct with every field a `*Future(T)`, where `T` can be any type,
1580/// and can be different for each field.
1581pub fn select(io: Io, s: anytype) SelectUnion(@TypeOf(s)) {
1582 const U = SelectUnion(@TypeOf(s));
1583 const S = @TypeOf(s);
1584 const fields = @typeInfo(S).@"struct".fields;
1585 var futures: [fields.len]*AnyFuture = undefined;
1586 inline for (fields, &futures) |field, *any_future| {
1587 const future = @field(s, field.name);
1588 any_future.* = future.any_future orelse return @unionInit(U, field.name, future.result);
1589 }
1590 switch (io.vtable.select(io.userdata, &futures)) {
1591 inline 0...(fields.len - 1) => |selected_index| {
1592 const field_name = fields[selected_index].name;
1593 return @unionInit(U, field_name, @field(s, field_name).await(io));
1594 },
1595 else => unreachable,
14921596 }
1493 return index;
1494}
1495
1496pub fn now(io: Io, clockid: std.posix.clockid_t) ClockGetTimeError!Timestamp {
1497 return io.vtable.now(io.userdata, clockid);
1498}
1499
1500pub fn sleep(io: Io, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void {
1501 return io.vtable.sleep(io.userdata, clockid, deadline);
15021597}
lib/std/Thread/Pool.zig+73-18
......@@ -335,6 +335,7 @@ pub fn io(pool: *Pool) Io {
335335 .go = go,
336336 .cancel = cancel,
337337 .cancelRequested = cancelRequested,
338 .select = select,
338339
339340 .mutexLock = mutexLock,
340341 .mutexUnlock = mutexUnlock,
......@@ -358,10 +359,13 @@ const AsyncClosure = struct {
358359 func: *const fn (context: *anyopaque, result: *anyopaque) void,
359360 runnable: Runnable = .{ .runFn = runFn },
360361 reset_event: std.Thread.ResetEvent,
362 select_condition: ?*std.Thread.ResetEvent,
361363 cancel_tid: std.Thread.Id,
362364 context_offset: usize,
363365 result_offset: usize,
364366
367 const done_reset_event: *std.Thread.ResetEvent = @ptrFromInt(std.mem.alignBackward(usize, std.math.maxInt(usize), @alignOf(std.Thread.ResetEvent)));
368
365369 const canceling_tid: std.Thread.Id = switch (@typeInfo(std.Thread.Id)) {
366370 .int => |int_info| switch (int_info.signedness) {
367371 .signed => -1,
......@@ -396,6 +400,17 @@ const AsyncClosure = struct {
396400 .acq_rel,
397401 .acquire,
398402 )) |cancel_tid| assert(cancel_tid == canceling_tid);
403
404 if (@atomicRmw(
405 ?*std.Thread.ResetEvent,
406 &closure.select_condition,
407 .Xchg,
408 done_reset_event,
409 .release,
410 )) |select_reset| {
411 assert(select_reset != done_reset_event);
412 select_reset.set();
413 }
399414 closure.reset_event.set();
400415 }
401416
......@@ -455,6 +470,7 @@ fn @"async"(
455470 .result_offset = result_offset,
456471 .reset_event = .{},
457472 .cancel_tid = 0,
473 .select_condition = null,
458474 };
459475 @memcpy(closure.contextPointer()[0..context.len], context);
460476 pool.run_queue.prepend(&closure.runnable.node);
......@@ -720,47 +736,54 @@ fn conditionWake(userdata: ?*anyopaque, cond: *Io.Condition, wake: Io.Condition.
720736
721737fn createFile(
722738 userdata: ?*anyopaque,
723 dir: std.fs.Dir,
739 dir: Io.Dir,
724740 sub_path: []const u8,
725 flags: std.fs.File.CreateFlags,
726) Io.FileOpenError!std.fs.File {
741 flags: Io.File.CreateFlags,
742) Io.File.OpenError!Io.File {
727743 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
728744 try pool.checkCancel();
729 return dir.createFile(sub_path, flags);
745 const fs_dir: std.fs.Dir = .{ .fd = dir.handle };
746 const fs_file = try fs_dir.createFile(sub_path, flags);
747 return .{ .handle = fs_file.handle };
730748}
731749
732750fn openFile(
733751 userdata: ?*anyopaque,
734 dir: std.fs.Dir,
752 dir: Io.Dir,
735753 sub_path: []const u8,
736 flags: std.fs.File.OpenFlags,
737) Io.FileOpenError!std.fs.File {
754 flags: Io.File.OpenFlags,
755) Io.File.OpenError!Io.File {
738756 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
739757 try pool.checkCancel();
740 return dir.openFile(sub_path, flags);
758 const fs_dir: std.fs.Dir = .{ .fd = dir.handle };
759 const fs_file = try fs_dir.openFile(sub_path, flags);
760 return .{ .handle = fs_file.handle };
741761}
742762
743fn closeFile(userdata: ?*anyopaque, file: std.fs.File) void {
763fn closeFile(userdata: ?*anyopaque, file: Io.File) void {
744764 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
745765 _ = pool;
746 return file.close();
766 const fs_file: std.fs.File = .{ .handle = file.handle };
767 return fs_file.close();
747768}
748769
749fn pread(userdata: ?*anyopaque, file: std.fs.File, buffer: []u8, offset: std.posix.off_t) Io.FilePReadError!usize {
770fn pread(userdata: ?*anyopaque, file: Io.File, buffer: []u8, offset: std.posix.off_t) Io.File.PReadError!usize {
750771 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
751772 try pool.checkCancel();
773 const fs_file: std.fs.File = .{ .handle = file.handle };
752774 return switch (offset) {
753 -1 => file.read(buffer),
754 else => file.pread(buffer, @bitCast(offset)),
775 -1 => fs_file.read(buffer),
776 else => fs_file.pread(buffer, @bitCast(offset)),
755777 };
756778}
757779
758fn pwrite(userdata: ?*anyopaque, file: std.fs.File, buffer: []const u8, offset: std.posix.off_t) Io.FilePWriteError!usize {
780fn pwrite(userdata: ?*anyopaque, file: Io.File, buffer: []const u8, offset: std.posix.off_t) Io.File.PWriteError!usize {
759781 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
760782 try pool.checkCancel();
783 const fs_file: std.fs.File = .{ .handle = file.handle };
761784 return switch (offset) {
762 -1 => file.write(buffer),
763 else => file.pwrite(buffer, @bitCast(offset)),
785 -1 => fs_file.write(buffer),
786 else => fs_file.pwrite(buffer, @bitCast(offset)),
764787 };
765788}
766789
......@@ -774,7 +797,7 @@ fn now(userdata: ?*anyopaque, clockid: std.posix.clockid_t) Io.ClockGetTimeError
774797fn sleep(userdata: ?*anyopaque, clockid: std.posix.clockid_t, deadline: Io.Deadline) Io.SleepError!void {
775798 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
776799 const deadline_nanoseconds: i96 = switch (deadline) {
777 .nanoseconds => |nanoseconds| nanoseconds,
800 .duration => |duration| duration.nanoseconds,
778801 .timestamp => |timestamp| @intFromEnum(timestamp),
779802 };
780803 var timespec: std.posix.timespec = .{
......@@ -784,7 +807,7 @@ fn sleep(userdata: ?*anyopaque, clockid: std.posix.clockid_t, deadline: Io.Deadl
784807 while (true) {
785808 try pool.checkCancel();
786809 switch (std.os.linux.E.init(std.os.linux.clock_nanosleep(clockid, .{ .ABSTIME = switch (deadline) {
787 .nanoseconds => false,
810 .duration => false,
788811 .timestamp => true,
789812 } }, &timespec, &timespec))) {
790813 .SUCCESS => return,
......@@ -795,3 +818,35 @@ fn sleep(userdata: ?*anyopaque, clockid: std.posix.clockid_t, deadline: Io.Deadl
795818 }
796819 }
797820}
821
822fn select(userdata: ?*anyopaque, futures: []const *Io.AnyFuture) usize {
823 const pool: *std.Thread.Pool = @alignCast(@ptrCast(userdata));
824 _ = pool;
825
826 var reset_event: std.Thread.ResetEvent = .{};
827
828 for (futures, 0..) |future, i| {
829 const closure: *AsyncClosure = @ptrCast(@alignCast(future));
830 if (@atomicRmw(?*std.Thread.ResetEvent, &closure.select_condition, .Xchg, &reset_event, .seq_cst) == AsyncClosure.done_reset_event) {
831 for (futures[0..i]) |cleanup_future| {
832 const cleanup_closure: *AsyncClosure = @ptrCast(@alignCast(cleanup_future));
833 if (@atomicRmw(?*std.Thread.ResetEvent, &cleanup_closure.select_condition, .Xchg, null, .seq_cst) == AsyncClosure.done_reset_event) {
834 cleanup_closure.reset_event.wait(); // Ensure no reference to our stack-allocated reset_event.
835 }
836 }
837 return i;
838 }
839 }
840
841 reset_event.wait();
842
843 var result: ?usize = null;
844 for (futures, 0..) |future, i| {
845 const closure: *AsyncClosure = @ptrCast(@alignCast(future));
846 if (@atomicRmw(?*std.Thread.ResetEvent, &closure.select_condition, .Xchg, null, .seq_cst) == AsyncClosure.done_reset_event) {
847 closure.reset_event.wait(); // Ensure no reference to our stack-allocated reset_event.
848 if (result == null) result = i; // In case multiple are ready, return first.
849 }
850 }
851 return result.?;
852}