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-10-02 16:30:59-07:00
log4a3ef0f779ab7a18a5968537f2cec573aff11554
tree9650d87e4c660ba0fd78d137e1052c39efcab0a2
parent6ba65ca972cf38a57266cf624932ae61d4cbba2d

introduce Io.select and implement it in thread pool


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

lib/std/Io.zig+155-60
......@@ -626,17 +626,21 @@ pub const VTable = struct {
626626 /// Thread-safe.
627627 cancelRequested: *const fn (?*anyopaque) bool,
628628
629 /// Blocks until one of the futures from the list has a result ready, such
630 /// that awaiting it will not block. Returns that index.
631 select: *const fn (?*anyopaque, futures: []const *AnyFuture) usize,
632
629633 mutexLock: *const fn (?*anyopaque, prev_state: Mutex.State, mutex: *Mutex) Cancelable!void,
630634 mutexUnlock: *const fn (?*anyopaque, prev_state: Mutex.State, mutex: *Mutex) void,
631635
632636 conditionWait: *const fn (?*anyopaque, cond: *Condition, mutex: *Mutex) Cancelable!void,
633637 conditionWake: *const fn (?*anyopaque, cond: *Condition, wake: Condition.Wake) void,
634638
635 createFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) FileOpenError!fs.File,
636 openFile: *const fn (?*anyopaque, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) FileOpenError!fs.File,
637 closeFile: *const fn (?*anyopaque, fs.File) void,
638 pread: *const fn (?*anyopaque, file: fs.File, buffer: []u8, offset: std.posix.off_t) FilePReadError!usize,
639 pwrite: *const fn (?*anyopaque, file: fs.File, buffer: []const u8, offset: std.posix.off_t) FilePWriteError!usize,
639 createFile: *const fn (?*anyopaque, dir: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File,
640 openFile: *const fn (?*anyopaque, dir: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File,
641 closeFile: *const fn (?*anyopaque, File) void,
642 pread: *const fn (?*anyopaque, file: File, buffer: []u8, offset: std.posix.off_t) File.PReadError!usize,
643 pwrite: *const fn (?*anyopaque, file: File, buffer: []const u8, offset: std.posix.off_t) File.PWriteError!usize,
640644
641645 now: *const fn (?*anyopaque, clockid: std.posix.clockid_t) ClockGetTimeError!Timestamp,
642646 sleep: *const fn (?*anyopaque, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void,
......@@ -647,28 +651,118 @@ pub const Cancelable = error{
647651 Canceled,
648652};
649653
650pub const OpenFlags = fs.File.OpenFlags;
651pub const CreateFlags = fs.File.CreateFlags;
654pub const Dir = struct {
655 handle: Handle,
656
657 pub fn cwd() Dir {
658 return .{ .handle = std.fs.cwd().fd };
659 }
660
661 pub const Handle = std.posix.fd_t;
662
663 pub fn openFile(dir: Dir, io: Io, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
664 return io.vtable.openFile(io.userdata, dir, sub_path, flags);
665 }
666
667 pub fn createFile(dir: Dir, io: Io, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
668 return io.vtable.createFile(io.userdata, dir, sub_path, flags);
669 }
670
671 pub const WriteFileOptions = struct {
672 /// On Windows, `sub_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
673 /// On WASI, `sub_path` should be encoded as valid UTF-8.
674 /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding.
675 sub_path: []const u8,
676 data: []const u8,
677 flags: File.CreateFlags = .{},
678 };
679
680 pub const WriteFileError = File.WriteError || File.OpenError || Cancelable;
681
682 /// Writes content to the file system, using the file creation flags provided.
683 pub fn writeFile(dir: Dir, io: Io, options: WriteFileOptions) WriteFileError!void {
684 var file = try dir.createFile(io, options.sub_path, options.flags);
685 defer file.close(io);
686 try file.writeAll(io, options.data);
687 }
688};
689
690pub const File = struct {
691 handle: Handle,
692
693 pub const Handle = std.posix.fd_t;
694
695 pub const OpenFlags = fs.File.OpenFlags;
696 pub const CreateFlags = fs.File.CreateFlags;
697
698 pub const OpenError = fs.File.OpenError || Cancelable;
699
700 pub fn close(file: File, io: Io) void {
701 return io.vtable.closeFile(io.userdata, file);
702 }
703
704 pub const ReadError = fs.File.ReadError || Cancelable;
705
706 pub fn read(file: File, io: Io, buffer: []u8) ReadError!usize {
707 return @errorCast(file.pread(io, buffer, -1));
708 }
709
710 pub const PReadError = fs.File.PReadError || Cancelable;
711
712 pub fn pread(file: File, io: Io, buffer: []u8, offset: std.posix.off_t) PReadError!usize {
713 return io.vtable.pread(io.userdata, file, buffer, offset);
714 }
715
716 pub const WriteError = fs.File.WriteError || Cancelable;
717
718 pub fn write(file: File, io: Io, buffer: []const u8) WriteError!usize {
719 return @errorCast(file.pwrite(io, buffer, -1));
720 }
721
722 pub const PWriteError = fs.File.PWriteError || Cancelable;
723
724 pub fn pwrite(file: File, io: Io, buffer: []const u8, offset: std.posix.off_t) PWriteError!usize {
725 return io.vtable.pwrite(io.userdata, file, buffer, offset);
726 }
727
728 pub fn writeAll(file: File, io: Io, bytes: []const u8) WriteError!void {
729 var index: usize = 0;
730 while (index < bytes.len) {
731 index += try file.write(io, bytes[index..]);
732 }
733 }
652734
653pub const FileOpenError = fs.File.OpenError || Cancelable;
654pub const FileReadError = fs.File.ReadError || Cancelable;
655pub const FilePReadError = fs.File.PReadError || Cancelable;
656pub const FileWriteError = fs.File.WriteError || Cancelable;
657pub const FilePWriteError = fs.File.PWriteError || Cancelable;
735 pub fn readAll(file: File, io: Io, buffer: []u8) ReadError!usize {
736 var index: usize = 0;
737 while (index != buffer.len) {
738 const amt = try file.read(io, buffer[index..]);
739 if (amt == 0) break;
740 index += amt;
741 }
742 return index;
743 }
744};
658745
659746pub const Timestamp = enum(i96) {
660747 _,
661748
662 pub fn durationTo(from: Timestamp, to: Timestamp) i96 {
663 return @intFromEnum(to) - @intFromEnum(from);
749 pub fn durationTo(from: Timestamp, to: Timestamp) Duration {
750 return .{ .nanoseconds = @intFromEnum(to) - @intFromEnum(from) };
664751 }
665752
666 pub fn addDuration(from: Timestamp, duration: i96) Timestamp {
667 return @enumFromInt(@intFromEnum(from) + duration);
753 pub fn addDuration(from: Timestamp, duration: Duration) Timestamp {
754 return @enumFromInt(@intFromEnum(from) + duration.nanoseconds);
668755 }
669756};
670pub const Deadline = union(enum) {
757pub const Duration = struct {
671758 nanoseconds: i96,
759
760 pub fn ms(x: u64) Duration {
761 return .{ .nanoseconds = @as(i96, x) * std.time.ns_per_ms };
762 }
763};
764pub const Deadline = union(enum) {
765 duration: Duration,
672766 timestamp: Timestamp,
673767};
674768pub const ClockGetTimeError = std.posix.ClockGetTimeError || Cancelable;
......@@ -1055,7 +1149,7 @@ pub fn Queue(Elem: type) type {
10551149
10561150/// Calls `function` with `args`, such that the return value of the function is
10571151/// not guaranteed to be available until `await` is called.
1058pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
1152pub fn async(io: Io, function: anytype, args: std.meta.ArgsTuple(@TypeOf(function))) Future(@typeInfo(@TypeOf(function)).@"fn".return_type.?) {
10591153 const Result = @typeInfo(@TypeOf(function)).@"fn".return_type.?;
10601154 const Args = @TypeOf(args);
10611155 const TypeErased = struct {
......@@ -1079,7 +1173,7 @@ pub fn async(io: Io, function: anytype, args: anytype) Future(@typeInfo(@TypeOf(
10791173
10801174/// Calls `function` with `args` asynchronously. The resource cleans itself up
10811175/// when the function returns. Does not support await, cancel, or a return value.
1082pub fn go(io: Io, function: anytype, args: anytype) void {
1176pub fn go(io: Io, function: anytype, args: std.meta.ArgsTuple(@TypeOf(function))) void {
10831177 const Args = @TypeOf(args);
10841178 const TypeErased = struct {
10851179 fn start(context: *const anyopaque) void {
......@@ -1095,55 +1189,56 @@ pub fn go(io: Io, function: anytype, args: anytype) void {
10951189 );
10961190}
10971191
1098pub fn openFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.OpenFlags) FileOpenError!fs.File {
1099 return io.vtable.openFile(io.userdata, dir, sub_path, flags);
1100}
1101
1102pub fn createFile(io: Io, dir: fs.Dir, sub_path: []const u8, flags: fs.File.CreateFlags) FileOpenError!fs.File {
1103 return io.vtable.createFile(io.userdata, dir, sub_path, flags);
1104}
1105
1106pub fn closeFile(io: Io, file: fs.File) void {
1107 return io.vtable.closeFile(io.userdata, file);
1108}
1109
1110pub fn read(io: Io, file: fs.File, buffer: []u8) FileReadError!usize {
1111 return @errorCast(io.pread(file, buffer, -1));
1112}
1113
1114pub fn pread(io: Io, file: fs.File, buffer: []u8, offset: std.posix.off_t) FilePReadError!usize {
1115 return io.vtable.pread(io.userdata, file, buffer, offset);
1192pub fn now(io: Io, clockid: std.posix.clockid_t) ClockGetTimeError!Timestamp {
1193 return io.vtable.now(io.userdata, clockid);
11161194}
11171195
1118pub fn write(io: Io, file: fs.File, buffer: []const u8) FileWriteError!usize {
1119 return @errorCast(io.pwrite(file, buffer, -1));
1196pub fn sleep(io: Io, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void {
1197 return io.vtable.sleep(io.userdata, clockid, deadline);
11201198}
11211199
1122pub fn pwrite(io: Io, file: fs.File, buffer: []const u8, offset: std.posix.off_t) FilePWriteError!usize {
1123 return io.vtable.pwrite(io.userdata, file, buffer, offset);
1200pub fn sleepDuration(io: Io, duration: Duration) SleepError!void {
1201 return io.vtable.sleep(io.userdata, .MONOTONIC, .{ .duration = duration });
11241202}
11251203
1126pub fn writeAll(io: Io, file: fs.File, bytes: []const u8) FileWriteError!void {
1127 var index: usize = 0;
1128 while (index < bytes.len) {
1129 index += try io.write(file, bytes[index..]);
1204/// Given a struct with each field a `*Future`, returns a union with the same
1205/// fields, each field type the future's result.
1206pub fn SelectUnion(S: type) type {
1207 const struct_fields = @typeInfo(S).@"struct".fields;
1208 var fields: [struct_fields.len]std.builtin.Type.UnionField = undefined;
1209 for (&fields, struct_fields) |*union_field, struct_field| {
1210 const F = @typeInfo(struct_field.type).pointer.child;
1211 const Result = @TypeOf(@as(F, undefined).result);
1212 union_field.* = .{
1213 .name = struct_field.name,
1214 .type = Result,
1215 .alignment = struct_field.alignment,
1216 };
11301217 }
1218 return @Type(.{ .@"union" = .{
1219 .layout = .auto,
1220 .tag_type = std.meta.FieldEnum(S),
1221 .fields = &fields,
1222 .decls = &.{},
1223 } });
11311224}
11321225
1133pub fn readAll(io: Io, file: fs.File, buffer: []u8) FileReadError!usize {
1134 var index: usize = 0;
1135 while (index != buffer.len) {
1136 const amt = try io.read(file, buffer[index..]);
1137 if (amt == 0) break;
1138 index += amt;
1226/// `s` is a struct with every field a `*Future(T)`, where `T` can be any type,
1227/// and can be different for each field.
1228pub fn select(io: Io, s: anytype) SelectUnion(@TypeOf(s)) {
1229 const U = SelectUnion(@TypeOf(s));
1230 const S = @TypeOf(s);
1231 const fields = @typeInfo(S).@"struct".fields;
1232 var futures: [fields.len]*AnyFuture = undefined;
1233 inline for (fields, &futures) |field, *any_future| {
1234 const future = @field(s, field.name);
1235 any_future.* = future.any_future orelse return @unionInit(U, field.name, future.result);
1236 }
1237 switch (io.vtable.select(io.userdata, &futures)) {
1238 inline 0...(fields.len - 1) => |selected_index| {
1239 const field_name = fields[selected_index].name;
1240 return @unionInit(U, field_name, @field(s, field_name).await(io));
1241 },
1242 else => unreachable,
11391243 }
1140 return index;
1141}
1142
1143pub fn now(io: Io, clockid: std.posix.clockid_t) ClockGetTimeError!Timestamp {
1144 return io.vtable.now(io.userdata, clockid);
1145}
1146
1147pub fn sleep(io: Io, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void {
1148 return io.vtable.sleep(io.userdata, clockid, deadline);
11491244}
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}