mirror of
https://github.com/ziglang/zig.git
synced 2026-01-21 06:45:24 +00:00
pread
Signed-off-by: Loris Cro <kappaloris@gmail.com>
This commit is contained in:
parent
bc35435ca6
commit
bd9f2369d5
@ -888,24 +888,36 @@ pub const Loop = struct {
|
||||
|
||||
/// Performs an async `os.pread` using a separate thread.
|
||||
/// `fd` must block and not return EAGAIN.
|
||||
pub fn pread(self: *Loop, fd: os.fd_t, buf: []u8, offset: u64) os.PReadError!usize {
|
||||
var req_node = Request.Node{
|
||||
.data = .{
|
||||
.msg = .{
|
||||
.pread = .{
|
||||
.fd = fd,
|
||||
.buf = buf,
|
||||
.offset = offset,
|
||||
.result = undefined,
|
||||
pub fn pread(self: *Loop, fd: os.fd_t, buf: []u8, offset: u64, simulate_evented: bool) os.PReadError!usize {
|
||||
if (simulate_evented) {
|
||||
var req_node = Request.Node{
|
||||
.data = .{
|
||||
.msg = .{
|
||||
.pread = .{
|
||||
.fd = fd,
|
||||
.buf = buf,
|
||||
.offset = offset,
|
||||
.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.pread.result;
|
||||
} else {
|
||||
while (true) {
|
||||
return os.pread(fd, buf, offset) catch |err| switch (err) {
|
||||
error.WouldBlock => {
|
||||
self.waitUntilFdReadable(fd);
|
||||
continue;
|
||||
},
|
||||
else => return err,
|
||||
};
|
||||
}
|
||||
}
|
||||
return req_node.data.msg.pread.result;
|
||||
}
|
||||
|
||||
/// Performs an async `os.preadv` using a separate thread.
|
||||
|
||||
@ -438,10 +438,12 @@ pub const File = struct {
|
||||
pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
|
||||
if (is_windows) {
|
||||
return windows.ReadFile(self.handle, buffer, offset, self.intended_io_mode);
|
||||
} else if (self.capable_io_mode != self.intended_io_mode) {
|
||||
return std.event.Loop.instance.?.pread(self.handle, buffer, offset);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (self.intended_io_mode == .blocking) {
|
||||
return os.pread(self.handle, buffer, offset);
|
||||
} else {
|
||||
return std.event.Loop.instance.?.pread(self.handle, buffer, offset, self.capable_io_mode != self.intended_io_mode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -482,12 +482,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
|
||||
EINTR => continue,
|
||||
EINVAL => unreachable,
|
||||
EFAULT => unreachable,
|
||||
EAGAIN => if (std.event.Loop.instance) |loop| {
|
||||
loop.waitUntilFdReadable(fd);
|
||||
continue;
|
||||
} else {
|
||||
return error.WouldBlock;
|
||||
},
|
||||
EAGAIN => return error.WouldBlock,
|
||||
EBADF => return error.NotOpenForReading, // Can be a race condition.
|
||||
EIO => return error.InputOutput,
|
||||
EISDIR => return error.IsDir,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user