authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-06-16 23:14:05+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-09-24 22:00:55+02:00
log08364ac773bdc95b9407974b5c761dbdab863f4d
tree32f61702861b3476b49b65df2693ba887753d676
parent730428bfd615cab415b2942fc9b781428a0ff692

read

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

3 files changed, 32 insertions(+), 23 deletions(-)

lib/std/event/loop.zig+26-14
...@@ -822,23 +822,35 @@ pub const Loop = struct {...@@ -822,23 +822,35 @@ pub const Loop = struct {
822822
823 /// Performs an async `os.read` using a separate thread.823 /// Performs an async `os.read` using a separate thread.
824 /// `fd` must block and not return EAGAIN.824 /// `fd` must block and not return EAGAIN.
825 pub fn read(self: *Loop, fd: os.fd_t, buf: []u8) os.ReadError!usize {825 pub fn read(self: *Loop, fd: os.fd_t, buf: []u8, simulate_evented: bool) os.ReadError!usize {
826 var req_node = Request.Node{826 if (simulate_evented) {
827 .data = .{827 var req_node = Request.Node{
828 .msg = .{828 .data = .{
829 .read = .{829 .msg = .{
830 .fd = fd,830 .read = .{
831 .buf = buf,831 .fd = fd,
832 .result = undefined,832 .buf = buf,
833 .result = undefined,
834 },
833 },835 },
836 .finish = .{ .TickNode = .{ .data = @frame() } },
834 },837 },
835 .finish = .{ .TickNode = .{ .data = @frame() } },838 };
836 },839 suspend {
837 };840 self.posixFsRequest(&req_node);
838 suspend {841 }
839 self.posixFsRequest(&req_node);842 return req_node.data.msg.read.result;
843 } else {
844 while (true) {
845 return os.read(fd, buf) catch |err| switch (err) {
846 error.WouldBlock => {
847 self.waitUntilFdReadable(fd);
848 continue;
849 },
850 else => return err,
851 };
852 }
840 }853 }
841 return req_node.data.msg.read.result;
842 }854 }
843855
844 /// Performs an async `os.readv` using a separate thread.856 /// Performs an async `os.readv` using a separate thread.
lib/std/fs/file.zig+5-3
...@@ -414,10 +414,12 @@ pub const File = struct {...@@ -414,10 +414,12 @@ pub const File = struct {
414 pub fn read(self: File, buffer: []u8) ReadError!usize {414 pub fn read(self: File, buffer: []u8) ReadError!usize {
415 if (is_windows) {415 if (is_windows) {
416 return windows.ReadFile(self.handle, buffer, null, self.intended_io_mode);416 return windows.ReadFile(self.handle, buffer, null, self.intended_io_mode);
417 } else if (self.capable_io_mode != self.intended_io_mode) {417 }
418 return std.event.Loop.instance.?.read(self.handle, buffer);418
419 } else {419 if (self.intended_io_mode == .blocking or !std.io.is_async) {
420 return os.read(self.handle, buffer);420 return os.read(self.handle, buffer);
421 } else {
422 return std.event.Loop.instance.?.read(self.handle, buffer, self.capable_io_mode != self.intended_io_mode);
421 }423 }
422 }424 }
423425
lib/std/os.zig+1-6
...@@ -366,12 +366,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -366,12 +366,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
366 EINTR => continue,366 EINTR => continue,
367 EINVAL => unreachable,367 EINVAL => unreachable,
368 EFAULT => unreachable,368 EFAULT => unreachable,
369 EAGAIN => if (std.event.Loop.instance) |loop| {369 EAGAIN => return error.WouldBlock,
370 loop.waitUntilFdReadable(fd);
371 continue;
372 } else {
373 return error.WouldBlock;
374 },
375 EBADF => return error.NotOpenForReading, // Can be a race condition.370 EBADF => return error.NotOpenForReading, // Can be a race condition.
376 EIO => return error.InputOutput,371 EIO => return error.InputOutput,
377 EISDIR => return error.IsDir,372 EISDIR => return error.IsDir,