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 {
822822
823823 /// Performs an async `os.read` using a separate thread.
824824 /// `fd` must block and not return EAGAIN.
825 pub fn read(self: *Loop, fd: os.fd_t, buf: []u8) os.ReadError!usize {
826 var req_node = Request.Node{
827 .data = .{
828 .msg = .{
829 .read = .{
830 .fd = fd,
831 .buf = buf,
832 .result = undefined,
825 pub fn read(self: *Loop, fd: os.fd_t, buf: []u8, simulate_evented: bool) os.ReadError!usize {
826 if (simulate_evented) {
827 var req_node = Request.Node{
828 .data = .{
829 .msg = .{
830 .read = .{
831 .fd = fd,
832 .buf = buf,
833 .result = undefined,
834 },
833835 },
836 .finish = .{ .TickNode = .{ .data = @frame() } },
834837 },
835 .finish = .{ .TickNode = .{ .data = @frame() } },
836 },
837 };
838 suspend {
839 self.posixFsRequest(&req_node);
838 };
839 suspend {
840 self.posixFsRequest(&req_node);
841 }
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 }
840853 }
841 return req_node.data.msg.read.result;
842854 }
843855
844856 /// Performs an async `os.readv` using a separate thread.
lib/std/fs/file.zig+5-3
......@@ -414,10 +414,12 @@ pub const File = struct {
414414 pub fn read(self: File, buffer: []u8) ReadError!usize {
415415 if (is_windows) {
416416 return windows.ReadFile(self.handle, buffer, null, self.intended_io_mode);
417 } else if (self.capable_io_mode != self.intended_io_mode) {
418 return std.event.Loop.instance.?.read(self.handle, buffer);
419 } else {
417 }
418
419 if (self.intended_io_mode == .blocking or !std.io.is_async) {
420420 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);
421423 }
422424 }
423425
lib/std/os.zig+1-6
......@@ -366,12 +366,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
366366 EINTR => continue,
367367 EINVAL => unreachable,
368368 EFAULT => unreachable,
369 EAGAIN => if (std.event.Loop.instance) |loop| {
370 loop.waitUntilFdReadable(fd);
371 continue;
372 } else {
373 return error.WouldBlock;
374 },
369 EAGAIN => return error.WouldBlock,
375370 EBADF => return error.NotOpenForReading, // Can be a race condition.
376371 EIO => return error.InputOutput,
377372 EISDIR => return error.IsDir,