authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-06-19 23:08:34+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-09-24 22:04:05+02:00
logbd9f2369d5c4e5fcd38342c877f9ae6531f78909
treedff3bdfb5bb5ceaa552ef2aa06476518b145af89
parentbc35435ca6e51d0e120538398e3c708ada57f6de

pread

Signed-off-by: Loris Cro <kappaloris@gmail.com>

3 files changed, 33 insertions(+), 24 deletions(-)

lib/std/event/loop.zig+27-15
...@@ -888,24 +888,36 @@ pub const Loop = struct {...@@ -888,24 +888,36 @@ pub const Loop = struct {
888888
889 /// Performs an async `os.pread` using a separate thread.889 /// Performs an async `os.pread` using a separate thread.
890 /// `fd` must block and not return EAGAIN.890 /// `fd` must block and not return EAGAIN.
891 pub fn pread(self: *Loop, fd: os.fd_t, buf: []u8, offset: u64) os.PReadError!usize {891 pub fn pread(self: *Loop, fd: os.fd_t, buf: []u8, offset: u64, simulate_evented: bool) os.PReadError!usize {
892 var req_node = Request.Node{892 if (simulate_evented) {
893 .data = .{893 var req_node = Request.Node{
894 .msg = .{894 .data = .{
895 .pread = .{895 .msg = .{
896 .fd = fd,896 .pread = .{
897 .buf = buf,897 .fd = fd,
898 .offset = offset,898 .buf = buf,
899 .result = undefined,899 .offset = offset,
900 .result = undefined,
901 },
900 },902 },
903 .finish = .{ .TickNode = .{ .data = @frame() } },
901 },904 },
902 .finish = .{ .TickNode = .{ .data = @frame() } },905 };
903 },906 suspend {
904 };907 self.posixFsRequest(&req_node);
905 suspend {908 }
906 self.posixFsRequest(&req_node);909 return req_node.data.msg.pread.result;
910 } else {
911 while (true) {
912 return os.pread(fd, buf, offset) catch |err| switch (err) {
913 error.WouldBlock => {
914 self.waitUntilFdReadable(fd);
915 continue;
916 },
917 else => return err,
918 };
919 }
907 }920 }
908 return req_node.data.msg.pread.result;
909 }921 }
910922
911 /// Performs an async `os.preadv` using a separate thread.923 /// Performs an async `os.preadv` using a separate thread.
lib/std/fs/file.zig+5-3
...@@ -438,10 +438,12 @@ pub const File = struct {...@@ -438,10 +438,12 @@ pub const File = struct {
438 pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {438 pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
439 if (is_windows) {439 if (is_windows) {
440 return windows.ReadFile(self.handle, buffer, offset, self.intended_io_mode);440 return windows.ReadFile(self.handle, buffer, offset, self.intended_io_mode);
441 } else if (self.capable_io_mode != self.intended_io_mode) {441 }
442 return std.event.Loop.instance.?.pread(self.handle, buffer, offset);442
443 } else {443 if (self.intended_io_mode == .blocking) {
444 return os.pread(self.handle, buffer, offset);444 return os.pread(self.handle, buffer, offset);
445 } else {
446 return std.event.Loop.instance.?.pread(self.handle, buffer, offset, self.capable_io_mode != self.intended_io_mode);
445 }447 }
446 }448 }
447449
lib/std/os.zig+1-6
...@@ -482,12 +482,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {...@@ -482,12 +482,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
482 EINTR => continue,482 EINTR => continue,
483 EINVAL => unreachable,483 EINVAL => unreachable,
484 EFAULT => unreachable,484 EFAULT => unreachable,
485 EAGAIN => if (std.event.Loop.instance) |loop| {485 EAGAIN => return error.WouldBlock,
486 loop.waitUntilFdReadable(fd);
487 continue;
488 } else {
489 return error.WouldBlock;
490 },
491 EBADF => return error.NotOpenForReading, // Can be a race condition.486 EBADF => return error.NotOpenForReading, // Can be a race condition.
492 EIO => return error.InputOutput,487 EIO => return error.InputOutput,
493 EISDIR => return error.IsDir,488 EISDIR => return error.IsDir,