aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoris Cro <kappaloris@gmail.com>2020-06-19 23:29:11 +0200
committerLoris Cro <kappaloris@gmail.com>2020-09-24 22:05:41 +0200
commit9075f8e5a1a788770cc8193d6f54118434e72a4b (patch)
treef0fb6f47aa146ce47006fcb4c682cea3a14be389
parent59ecdaea127cb680d295ad02319dacba75ac1e73 (diff)
downloadzig-9075f8e5a1a788770cc8193d6f54118434e72a4b.tar.gz
zig-9075f8e5a1a788770cc8193d6f54118434e72a4b.zip
write
Signed-off-by: Loris Cro <kappaloris@gmail.com>
-rw-r--r--lib/std/event/loop.zig40
-rw-r--r--lib/std/fs/file.zig8
-rw-r--r--lib/std/os.zig7
3 files changed, 32 insertions, 23 deletions
diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig
index a14e798c50..ec45e85630 100644
--- a/lib/std/event/loop.zig
+++ b/lib/std/event/loop.zig
@@ -956,23 +956,35 @@ pub const Loop = struct {
/// Performs an async `os.write` using a separate thread.
/// `fd` must block and not return EAGAIN.
- pub fn write(self: *Loop, fd: os.fd_t, bytes: []const u8) os.WriteError!usize {
- var req_node = Request.Node{
- .data = .{
- .msg = .{
- .write = .{
- .fd = fd,
- .bytes = bytes,
- .result = undefined,
+ pub fn write(self: *Loop, fd: os.fd_t, bytes: []const u8, simulate_evented: bool) os.WriteError!usize {
+ if (simulate_evented) {
+ var req_node = Request.Node{
+ .data = .{
+ .msg = .{
+ .write = .{
+ .fd = fd,
+ .bytes = bytes,
+ .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.write.result;
+ } else {
+ while (true) {
+ return os.write(fd, bytes) catch |err| switch (err) {
+ error.WouldBlock => {
+ self.waitUntilFdWritable(fd);
+ continue;
+ },
+ else => return err,
+ };
+ }
}
- return req_node.data.msg.write.result;
}
/// Performs an async `os.writev` using a separate thread.
diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig
index 4aa33fc5b9..428b758e61 100644
--- a/lib/std/fs/file.zig
+++ b/lib/std/fs/file.zig
@@ -547,10 +547,12 @@ pub const File = struct {
pub fn write(self: File, bytes: []const u8) WriteError!usize {
if (is_windows) {
return windows.WriteFile(self.handle, bytes, null, self.intended_io_mode);
- } else if (self.capable_io_mode != self.intended_io_mode) {
- return std.event.Loop.instance.?.write(self.handle, bytes);
- } else {
+ }
+
+ if (self.intended_io_mode == .blocking) {
return os.write(self.handle, bytes);
+ } else {
+ return std.event.Loop.instance.?.write(self.handle, bytes, self.capable_io_mode != self.intended_io_mode);
}
}
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 6fb9d4388b..da5ce13508 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -721,12 +721,7 @@ pub fn write(fd: fd_t, bytes: []const u8) 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,