authorgravatar for 19855629+SuperAuguste@users.noreply.github.comSuperAuguste <19855629+SuperAuguste@users.noreply.github.com> 2024-02-24 15:44:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-25 01:41:56-08:00
log55f437b92bb394f7df558bb3209f057f9f46274f
tree48df981452556786884c3b4d53ef767b81da8aff
parent6c2eb0f131588be111652a755a4492ff72d16440

Add pollTimeout for non-blocking/timeout-having polls


1 files changed, 22 insertions(+), 6 deletions(-)

lib/std/io.zig+22-6
...@@ -512,9 +512,17 @@ pub fn Poller(comptime StreamEnum: type) type {...@@ -512,9 +512,17 @@ pub fn Poller(comptime StreamEnum: type) type {
512512
513 pub fn poll(self: *Self) !bool {513 pub fn poll(self: *Self) !bool {
514 if (builtin.os.tag == .windows) {514 if (builtin.os.tag == .windows) {
515 return pollWindows(self);515 return pollWindows(self, null);
516 } else {516 } else {
517 return pollPosix(self);517 return pollPosix(self, null);
518 }
519 }
520
521 pub fn pollTimeout(self: *Self, nanoseconds: u64) !bool {
522 if (builtin.os.tag == .windows) {
523 return pollWindows(self, nanoseconds);
524 } else {
525 return pollPosix(self, nanoseconds);
518 }526 }
519 }527 }
520528
...@@ -522,7 +530,7 @@ pub fn Poller(comptime StreamEnum: type) type {...@@ -522,7 +530,7 @@ pub fn Poller(comptime StreamEnum: type) type {
522 return &self.fifos[@intFromEnum(which)];530 return &self.fifos[@intFromEnum(which)];
523 }531 }
524532
525 fn pollWindows(self: *Self) !bool {533 fn pollWindows(self: *Self, nanoseconds: ?u64) !bool {
526 const bump_amt = 512;534 const bump_amt = 512;
527535
528 if (!self.windows.first_read_done) {536 if (!self.windows.first_read_done) {
...@@ -553,10 +561,15 @@ pub fn Poller(comptime StreamEnum: type) type {...@@ -553,10 +561,15 @@ pub fn Poller(comptime StreamEnum: type) type {
553 self.windows.active.count,561 self.windows.active.count,
554 &self.windows.active.handles_buf,562 &self.windows.active.handles_buf,
555 0,563 0,
556 os.windows.INFINITE,564 if (nanoseconds) |ns|
565 @min(std.math.cast(u32, ns / std.time.ns_per_ms) orelse (os.windows.INFINITE - 1), os.windows.INFINITE - 1)
566 else
567 os.windows.INFINITE,
557 );568 );
558 if (status == os.windows.WAIT_FAILED)569 if (status == os.windows.WAIT_FAILED)
559 return os.windows.unexpectedError(os.windows.kernel32.GetLastError());570 return os.windows.unexpectedError(os.windows.kernel32.GetLastError());
571 if (status == os.windows.WAIT_TIMEOUT)
572 return true;
560573
561 if (status < os.windows.WAIT_OBJECT_0 or status > os.windows.WAIT_OBJECT_0 + enum_fields.len - 1)574 if (status < os.windows.WAIT_OBJECT_0 or status > os.windows.WAIT_OBJECT_0 + enum_fields.len - 1)
562 unreachable;575 unreachable;
...@@ -594,7 +607,7 @@ pub fn Poller(comptime StreamEnum: type) type {...@@ -594,7 +607,7 @@ pub fn Poller(comptime StreamEnum: type) type {
594 }607 }
595 }608 }
596609
597 fn pollPosix(self: *Self) !bool {610 fn pollPosix(self: *Self, nanoseconds: ?u64) !bool {
598 // We ask for ensureUnusedCapacity with this much extra space. This611 // We ask for ensureUnusedCapacity with this much extra space. This
599 // has more of an effect on small reads because once the reads612 // has more of an effect on small reads because once the reads
600 // start to get larger the amount of space an ArrayList will613 // start to get larger the amount of space an ArrayList will
...@@ -603,7 +616,10 @@ pub fn Poller(comptime StreamEnum: type) type {...@@ -603,7 +616,10 @@ pub fn Poller(comptime StreamEnum: type) type {
603616
604 const err_mask = os.POLL.ERR | os.POLL.NVAL | os.POLL.HUP;617 const err_mask = os.POLL.ERR | os.POLL.NVAL | os.POLL.HUP;
605618
606 const events_len = try os.poll(&self.poll_fds, std.math.maxInt(i32));619 const events_len = try os.poll(&self.poll_fds, if (nanoseconds) |ns|
620 std.math.cast(i32, ns / std.time.ns_per_ms) orelse std.math.maxInt(i32)
621 else
622 -1);
607 if (events_len == 0) {623 if (events_len == 0) {
608 for (self.poll_fds) |poll_fd| {624 for (self.poll_fds) |poll_fd| {
609 if (poll_fd.fd != -1) return true;625 if (poll_fd.fd != -1) return true;