authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-27 22:39:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-27 22:39:47-07:00
logd8c3738e21611f0292c205f7b5c5c0db23a23262
treeec62e82be9191f0fa6600025759df92668bc6510
parent814de45bd2e6711056c634b089b2ebf0c96f50b9

redo std.io.poll with only outputs


1 files changed, 15 insertions(+), 31 deletions(-)

lib/std/io.zig+15-31
......@@ -183,11 +183,8 @@ pub fn poll(
183183 .count = 0,
184184 };
185185 result.poll_fds[i] = .{
186 .fd = @field(files, enum_fields[i].name).file.handle,
187 .events = switch (@field(files, enum_fields[i].name).direction) {
188 .in => os.POLL.IN,
189 .out => os.POLL.OUT,
190 },
186 .fd = @field(files, enum_fields[i].name).handle,
187 .events = os.POLL.IN,
191188 .revents = undefined,
192189 };
193190 }
......@@ -200,12 +197,15 @@ pub fn Poller(comptime StreamEnum: type) type {
200197 const Fifo = std.fifo.LinearFifo(u8, .Dynamic);
201198
202199 fifos: [enum_fields.len]Fifo,
203 //directions: [enum_fields.len]PollFile.Direction,
204 //handles: [enum_fields.len]std.fs.File.Handle,
205200 poll_fds: [enum_fields.len]std.os.pollfd,
206201
207202 const Self = @This();
208203
204 pub fn deinit(self: *Self) void {
205 inline for (&self.fifos) |*q| q.deinit();
206 self.* = undefined;
207 }
208
209209 pub fn poll(self: *Self) !void {
210210 if (builtin.os.tag == .windows) {
211211 return pollWindows(self);
......@@ -241,31 +241,22 @@ pub fn Poller(comptime StreamEnum: type) type {
241241 const events_len = try os.poll(&self.poll_fds, std.math.maxInt(i32));
242242 if (events_len == 0) return;
243243
244 inline for (0..enum_fields.len) |i| {
244 inline for (&self.poll_fds, &self.fifos) |*poll_fd, *q| {
245245 // Try reading whatever is available before checking the error
246246 // conditions.
247247 // It's still possible to read after a POLL.HUP is received,
248248 // always check if there's some data waiting to be read first.
249 if (self.poll_fds[i].revents & os.POLL.IN != 0) {
250 const q = &self.fifos[i];
249 if (poll_fd.revents & os.POLL.IN != 0) {
251250 const buf = try q.writableWithSize(bump_amt);
252 const amt = try os.read(self.poll_fds[i].fd, buf);
251 const amt = try os.read(poll_fd.fd, buf);
253252 q.update(amt);
254 std.debug.print("read {d} bytes\n", .{amt});
255253 if (amt == 0) {
256254 // Remove the fd when the EOF condition is met.
257 self.poll_fds[i].fd = -1;
255 poll_fd.fd = -1;
258256 }
259 } else if (self.poll_fds[i].revents & err_mask != 0) {
257 } else if (poll_fd.revents & err_mask != 0) {
260258 // Exclude the fds that signaled an error.
261 self.poll_fds[i].fd = -1;
262 } else if (self.poll_fds[i].revents & os.POLL.OUT != 0) {
263 const q = &self.fifos[i];
264 const amt = try os.write(self.poll_fds[i].fd, q.readableSlice(0));
265 q.discard(amt);
266 if (amt == 0) {
267 self.poll_fds[i].fd = -1;
268 }
259 poll_fd.fd = -1;
269260 }
270261 }
271262 }
......@@ -280,10 +271,10 @@ pub fn PollFiles(comptime StreamEnum: type) type {
280271 for (&struct_fields, enum_fields) |*struct_field, enum_field| {
281272 struct_field.* = .{
282273 .name = enum_field.name,
283 .type = PollFile,
274 .type = fs.File,
284275 .default_value = null,
285276 .is_comptime = false,
286 .alignment = @alignOf(PollFile),
277 .alignment = @alignOf(fs.File),
287278 };
288279 }
289280 return @Type(.{ .Struct = .{
......@@ -294,13 +285,6 @@ pub fn PollFiles(comptime StreamEnum: type) type {
294285 } });
295286}
296287
297pub const PollFile = struct {
298 file: File,
299 direction: Direction,
300
301 pub const Direction = enum { in, out };
302};
303
304288test {
305289 _ = @import("io/bit_reader.zig");
306290 _ = @import("io/bit_writer.zig");