authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-06-19 23:29:11+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-09-24 22:05:41+02:00
log9075f8e5a1a788770cc8193d6f54118434e72a4b
treef0fb6f47aa146ce47006fcb4c682cea3a14be389
parent59ecdaea127cb680d295ad02319dacba75ac1e73

write

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

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

lib/std/event/loop.zig+26-14
...@@ -956,23 +956,35 @@ pub const Loop = struct {...@@ -956,23 +956,35 @@ pub const Loop = struct {
956956
957 /// Performs an async `os.write` using a separate thread.957 /// Performs an async `os.write` using a separate thread.
958 /// `fd` must block and not return EAGAIN.958 /// `fd` must block and not return EAGAIN.
959 pub fn write(self: *Loop, fd: os.fd_t, bytes: []const u8) os.WriteError!usize {959 pub fn write(self: *Loop, fd: os.fd_t, bytes: []const u8, simulate_evented: bool) os.WriteError!usize {
960 var req_node = Request.Node{960 if (simulate_evented) {
961 .data = .{961 var req_node = Request.Node{
962 .msg = .{962 .data = .{
963 .write = .{963 .msg = .{
964 .fd = fd,964 .write = .{
965 .bytes = bytes,965 .fd = fd,
966 .result = undefined,966 .bytes = bytes,
967 .result = undefined,
968 },
967 },969 },
970 .finish = .{ .TickNode = .{ .data = @frame() } },
968 },971 },
969 .finish = .{ .TickNode = .{ .data = @frame() } },972 };
970 },973 suspend {
971 };974 self.posixFsRequest(&req_node);
972 suspend {975 }
973 self.posixFsRequest(&req_node);976 return req_node.data.msg.write.result;
977 } else {
978 while (true) {
979 return os.write(fd, bytes) catch |err| switch (err) {
980 error.WouldBlock => {
981 self.waitUntilFdWritable(fd);
982 continue;
983 },
984 else => return err,
985 };
986 }
974 }987 }
975 return req_node.data.msg.write.result;
976 }988 }
977989
978 /// Performs an async `os.writev` using a separate thread.990 /// Performs an async `os.writev` using a separate thread.
lib/std/fs/file.zig+5-3
...@@ -547,10 +547,12 @@ pub const File = struct {...@@ -547,10 +547,12 @@ pub const File = struct {
547 pub fn write(self: File, bytes: []const u8) WriteError!usize {547 pub fn write(self: File, bytes: []const u8) WriteError!usize {
548 if (is_windows) {548 if (is_windows) {
549 return windows.WriteFile(self.handle, bytes, null, self.intended_io_mode);549 return windows.WriteFile(self.handle, bytes, null, self.intended_io_mode);
550 } else if (self.capable_io_mode != self.intended_io_mode) {550 }
551 return std.event.Loop.instance.?.write(self.handle, bytes);551
552 } else {552 if (self.intended_io_mode == .blocking) {
553 return os.write(self.handle, bytes);553 return os.write(self.handle, bytes);
554 } else {
555 return std.event.Loop.instance.?.write(self.handle, bytes, self.capable_io_mode != self.intended_io_mode);
554 }556 }
555 }557 }
556558
lib/std/os.zig+1-6
...@@ -721,12 +721,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {...@@ -721,12 +721,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
721 EINTR => continue,721 EINTR => continue,
722 EINVAL => unreachable,722 EINVAL => unreachable,
723 EFAULT => unreachable,723 EFAULT => unreachable,
724 EAGAIN => if (std.event.Loop.instance) |loop| {724 EAGAIN => return error.WouldBlock,
725 loop.waitUntilFdWritable(fd);
726 continue;
727 } else {
728 return error.WouldBlock;
729 },
730 EBADF => return error.NotOpenForWriting, // can be a race condition.725 EBADF => return error.NotOpenForWriting, // can be a race condition.
731 EDESTADDRREQ => unreachable, // `connect` was never called.726 EDESTADDRREQ => unreachable, // `connect` was never called.
732 EDQUOT => return error.DiskQuota,727 EDQUOT => return error.DiskQuota,