diff options
| author | Jonathan Marler <johnnymarler@gmail.com> | 2023-02-28 21:00:10 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-03-01 12:21:53 -0500 |
| commit | f2b15420ad595f77b6a3575dd7a6e85411dc69e9 (patch) | |
| tree | 714a157e82d7d9cfddfdede6be93306a30c2d141 | |
| parent | 138e8b162aeecc69cc62f0822e73b1862b7d07f6 (diff) | |
| download | zig-f2b15420ad595f77b6a3575dd7a6e85411dc69e9.tar.gz zig-f2b15420ad595f77b6a3575dd7a6e85411dc69e9.zip | |
std.io.poll: remove done function
| -rw-r--r-- | lib/std/child_process.zig | 3 | ||||
| -rw-r--r-- | lib/std/io.zig | 31 |
2 files changed, 17 insertions, 17 deletions
diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index 440b512fd2..7ff48a5652 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -235,8 +235,7 @@ pub const ChildProcess = struct { }); defer poller.deinit(); - while (!poller.done()) { - try poller.poll(); + while (try poller.poll()) { if (poller.fifo(.stdout).count > max_output_bytes) return error.StdoutStreamTooLong; if (poller.fifo(.stderr).count > max_output_bytes) diff --git a/lib/std/io.zig b/lib/std/io.zig index ea48b2d121..0faba2b652 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -249,7 +249,7 @@ pub fn Poller(comptime StreamEnum: type) type { self.* = undefined; } - pub fn poll(self: *Self) !void { + pub fn poll(self: *Self) !bool { if (builtin.os.tag == .windows) { return pollWindows(self); } else { @@ -261,16 +261,7 @@ pub fn Poller(comptime StreamEnum: type) type { return &self.fifos[@enumToInt(which)]; } - pub fn done(self: Self) bool { - if (builtin.os.tag == .windows) - return self.windows.first_read_done and self.windows.active.count == 0; - - for (self.poll_fds) |poll_fd| { - if (poll_fd.fd != -1) return false; - } else return true; - } - - fn pollWindows(self: *Self) !void { + fn pollWindows(self: *Self) !bool { const bump_amt = 512; if (!self.windows.first_read_done) { @@ -295,7 +286,7 @@ pub fn Poller(comptime StreamEnum: type) type { } while (true) { - if (self.windows.active.count == 0) return; + if (self.windows.active.count == 0) return false; const status = os.windows.kernel32.WaitForMultipleObjects( self.windows.active.count, @@ -338,11 +329,11 @@ pub fn Poller(comptime StreamEnum: type) type { .pending => {}, .closed => self.windows.active.removeAt(active_idx), } - return; + return true; } } - fn pollPosix(self: *Self) !void { + fn pollPosix(self: *Self) !bool { // We ask for ensureUnusedCapacity with this much extra space. This // has more of an effect on small reads because once the reads // start to get larger the amount of space an ArrayList will @@ -352,8 +343,13 @@ pub fn Poller(comptime StreamEnum: type) type { const err_mask = os.POLL.ERR | os.POLL.NVAL | os.POLL.HUP; const events_len = try os.poll(&self.poll_fds, std.math.maxInt(i32)); - if (events_len == 0) return; + if (events_len == 0) { + for (self.poll_fds) |poll_fd| { + if (poll_fd.fd != -1) return true; + } else return false; + } + var keep_polling = false; inline for (&self.poll_fds, &self.fifos) |*poll_fd, *q| { // Try reading whatever is available before checking the error // conditions. @@ -366,12 +362,17 @@ pub fn Poller(comptime StreamEnum: type) type { if (amt == 0) { // Remove the fd when the EOF condition is met. poll_fd.fd = -1; + } else { + keep_polling = true; } } else if (poll_fd.revents & err_mask != 0) { // Exclude the fds that signaled an error. poll_fd.fd = -1; + } else if (poll_fd.fd != -1) { + keep_polling = true; } } + return keep_polling; } }; } |
