Commit 18f6629bd8
lib/std/event/loop.zig
@@ -989,23 +989,35 @@ pub const Loop = struct {
/// Performs an async `os.writev` using a separate thread.
/// `fd` must block and not return EAGAIN.
- pub fn writev(self: *Loop, fd: os.fd_t, iov: []const os.iovec_const) os.WriteError!usize {
- var req_node = Request.Node{
- .data = .{
- .msg = .{
- .writev = .{
- .fd = fd,
- .iov = iov,
- .result = undefined,
+ pub fn writev(self: *Loop, fd: os.fd_t, iov: []const os.iovec_const, simulate_evented: bool) os.WriteError!usize {
+ if (simulate_evented) {
+ var req_node = Request.Node{
+ .data = .{
+ .msg = .{
+ .writev = .{
+ .fd = fd,
+ .iov = iov,
+ .result = undefined,
+ },
},
+ .finish = .{ .TickNode = .{ .data = @frame() } },
},
- .finish = .{ .TickNode = .{ .data = @frame() } },
- },
- };
- suspend {
- self.posixFsRequest(&req_node);
+ };
+ suspend {
+ self.posixFsRequest(&req_node);
+ }
+ return req_node.data.msg.writev.result;
+ } else {
+ while (true) {
+ return os.writev(fd, iov) catch |err| switch (err) {
+ error.WouldBlock => {
+ self.waitUntilFdWritable(fd);
+ continue;
+ },
+ else => return err,
+ };
+ }
}
- return req_node.data.msg.writev.result;
}
/// Performs an async `os.pwritev` using a separate thread.
lib/std/fs/file.zig
@@ -586,10 +586,12 @@ pub const File = struct {
if (iovecs.len == 0) return @as(usize, 0);
const first = iovecs[0];
return windows.WriteFile(self.handle, first.iov_base[0..first.iov_len], null, self.intended_io_mode);
- } else if (self.capable_io_mode != self.intended_io_mode) {
- return std.event.Loop.instance.?.writev(self.handle, iovecs);
- } else {
+ }
+
+ if (self.intended_io_mode == .blocking) {
return os.writev(self.handle, iovecs);
+ } else {
+ return std.event.Loop.instance.?.writev(self.handle, iovecs, self.capable_io_mode != self.intended_io_mode);
}
}
lib/std/os.zig
@@ -789,12 +789,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
EINTR => continue,
EINVAL => unreachable,
EFAULT => unreachable,
- EAGAIN => if (std.event.Loop.instance) |loop| {
- loop.waitUntilFdWritable(fd);
- continue;
- } else {
- return error.WouldBlock;
- },
+ EAGAIN => return error.WouldBlock,
EBADF => return error.NotOpenForWriting, // Can be a race condition.
EDESTADDRREQ => unreachable, // `connect` was never called.
EDQUOT => return error.DiskQuota,