Merge branch 'windows-evented-io' of https://github.com/FireFox317/zig into FireFox317-windows-evented-io

master
Andrew Kelley 2020-05-01 19:02:16 -04:00
commit 988031c07c
8 changed files with 310 additions and 263 deletions

View File

@ -666,6 +666,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
/// TODO resources https://github.com/ziglang/zig/issues/4353
fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ModuleDebugInfo {
noasync {
const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
errdefer coff_file.close();
@ -819,6 +820,7 @@ fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !
return di;
}
}
fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
const num_words = try stream.readIntLittle(u32);
@ -1410,6 +1412,7 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
}
fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
noasync {
// Translate the VA into an address into this object
const relocated_address = address - self.base_address;
assert(relocated_address >= 0x100000000);
@ -1464,6 +1467,7 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
unreachable;
}
}
},
.uefi, .windows => struct {
base_address: usize,

View File

@ -639,21 +639,28 @@ pub const Dir = struct {
/// Same as `openFile` but Windows-only and the path parameter is
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
pub fn openFileW(self: Dir, sub_path_w: [*:0]const u16, flags: File.OpenFlags) File.OpenError!File {
pub fn openFileW(self: Dir, sub_path_w: []const u16, flags: File.OpenFlags) File.OpenError!File {
const w = os.windows;
const access_mask = w.SYNCHRONIZE |
return @as(File, .{
.handle = try os.windows.OpenFile(sub_path_w, .{
.dir = self.fd,
.access_mask = w.SYNCHRONIZE |
(if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
(if (flags.write) @as(u32, w.GENERIC_WRITE) else 0);
const share_access = switch (flags.lock) {
(if (flags.write) @as(u32, w.GENERIC_WRITE) else 0),
.share_access = switch (flags.lock) {
.None => @as(?w.ULONG, null),
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
.Exclusive => w.FILE_SHARE_DELETE,
};
return @as(File, .{
.handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, w.FILE_OPEN),
},
.share_access_nonblocking = flags.lock_nonblocking,
.creation = w.FILE_OPEN,
.enable_async_io = std.io.is_async and !flags.always_blocking,
}),
.io_mode = .blocking,
.async_block_allowed = if (flags.always_blocking)
File.async_block_allowed_yes
else
File.async_block_allowed_no,
});
}
@ -713,25 +720,27 @@ pub const Dir = struct {
/// Same as `createFile` but Windows-only and the path parameter is
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
pub fn createFileW(self: Dir, sub_path_w: [*:0]const u16, flags: File.CreateFlags) File.OpenError!File {
pub fn createFileW(self: Dir, sub_path_w: []const u16, flags: File.CreateFlags) File.OpenError!File {
const w = os.windows;
const access_mask = w.SYNCHRONIZE | w.GENERIC_WRITE |
(if (flags.read) @as(u32, w.GENERIC_READ) else 0);
const creation = if (flags.exclusive)
const read_flag = if (flags.read) @as(u32, w.GENERIC_READ) else 0;
return @as(File, .{
.handle = try os.windows.OpenFile(sub_path_w, .{
.dir = self.fd,
.access_mask = w.SYNCHRONIZE | w.GENERIC_WRITE | read_flag,
.share_access = switch (flags.lock) {
.None => @as(?w.ULONG, null),
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
.Exclusive => w.FILE_SHARE_DELETE,
},
.share_access_nonblocking = flags.lock_nonblocking,
.creation = if (flags.exclusive)
@as(u32, w.FILE_CREATE)
else if (flags.truncate)
@as(u32, w.FILE_OVERWRITE_IF)
else
@as(u32, w.FILE_OPEN_IF);
const share_access = switch (flags.lock) {
.None => @as(?w.ULONG, null),
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
.Exclusive => w.FILE_SHARE_DELETE,
};
return @as(File, .{
.handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, creation),
@as(u32, w.FILE_OPEN_IF),
.enable_async_io = std.io.is_async,
}),
.io_mode = .blocking,
});
}

View File

@ -62,12 +62,14 @@ pub const File = struct {
/// Sets whether or not to wait until the file is locked to return. If set to true,
/// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
/// is available to proceed.
/// is available to proceed. In async I/O mode, non-blocking at the OS level is always
/// used, and `true` means `error.WouldBlock` is returned, and `false` means
/// `error.WouldBlock` is handled by the event loop.
lock_nonblocking: bool = false,
/// This prevents `O_NONBLOCK` from being passed even if `std.io.is_async`.
/// It allows the use of `noasync` when calling functions related to opening
/// the file, reading, and writing.
/// the file, reading, writing, as well as locking functionality.
always_blocking: bool = false,
};
@ -295,6 +297,10 @@ pub const File = struct {
pub const PReadError = os.PReadError;
pub fn read(self: File, buffer: []u8) ReadError!usize {
if (builtin.os.tag == .windows) {
const enable_async_io = std.io.is_async and !self.async_block_allowed;
return windows.ReadFile(self.handle, buffer, null, enable_async_io);
}
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
return std.event.Loop.instance.?.read(self.handle, buffer);
} else {
@ -315,6 +321,10 @@ pub const File = struct {
}
pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
if (builtin.os.tag == .windows) {
const enable_async_io = std.io.is_async and !self.async_block_allowed;
return windows.ReadFile(self.handle, buffer, offset, enable_async_io);
}
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
return std.event.Loop.instance.?.pread(self.handle, buffer, offset);
} else {
@ -406,6 +416,10 @@ pub const File = struct {
pub const PWriteError = os.PWriteError;
pub fn write(self: File, bytes: []const u8) WriteError!usize {
if (builtin.os.tag == .windows) {
const enable_async_io = std.io.is_async and !self.async_block_allowed;
return windows.WriteFile(self.handle, bytes, null, enable_async_io);
}
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
return std.event.Loop.instance.?.write(self.handle, bytes);
} else {
@ -421,6 +435,10 @@ pub const File = struct {
}
pub fn pwrite(self: File, bytes: []const u8, offset: u64) PWriteError!usize {
if (builtin.os.tag == .windows) {
const enable_async_io = std.io.is_async and !self.async_block_allowed;
return windows.WriteFile(self.handle, bytes, offset, enable_async_io);
}
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
return std.event.Loop.instance.?.pwrite(self.handle, bytes, offset);
} else {

View File

@ -177,6 +177,10 @@ pub fn isAbsoluteWindowsW(path_w: [*:0]const u16) bool {
return isAbsoluteWindowsImpl(u16, mem.spanZ(path_w));
}
pub fn isAbsoluteWindowsWTF16(path: []const u16) bool {
return isAbsoluteWindowsImpl(u16, path);
}
pub const isAbsoluteWindowsC = @compileError("deprecated: renamed to isAbsoluteWindowsZ");
pub fn isAbsoluteWindowsZ(path_c: [*:0]const u8) bool {

View File

@ -42,10 +42,12 @@ fn getStdOutHandle() os.fd_t {
return os.STDOUT_FILENO;
}
// TODO: async stdout on windows (https://github.com/ziglang/zig/pull/4816#issuecomment-604521023)
pub fn getStdOut() File {
return File{
.handle = getStdOutHandle(),
.io_mode = .blocking,
.async_block_allowed = if (builtin.os.tag == .windows) File.async_block_allowed_yes else File.async_block_allowed_no,
};
}
@ -81,10 +83,12 @@ fn getStdInHandle() os.fd_t {
return os.STDIN_FILENO;
}
// TODO: async stdin on windows (https://github.com/ziglang/zig/pull/4816#issuecomment-604521023)
pub fn getStdIn() File {
return File{
.handle = getStdInHandle(),
.io_mode = .blocking,
.async_block_allowed = if (builtin.os.tag == .windows) File.async_block_allowed_yes else File.async_block_allowed_no,
};
}

View File

@ -305,7 +305,7 @@ pub const ReadError = error{
/// For POSIX the limit is `math.maxInt(isize)`.
pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
if (builtin.os.tag == .windows) {
return windows.ReadFile(fd, buf, null);
return windows.ReadFile(fd, buf, null, false);
}
if (builtin.os.tag == .wasi and !builtin.link_libc) {
@ -408,7 +408,7 @@ pub const PReadError = ReadError || error{Unseekable};
/// used to perform the I/O. `error.WouldBlock` is not possible on Windows.
pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
if (builtin.os.tag == .windows) {
return windows.ReadFile(fd, buf, offset);
return windows.ReadFile(fd, buf, offset, false);
}
while (true) {
@ -584,7 +584,7 @@ pub const WriteError = error{
/// The corresponding POSIX limit is `math.maxInt(isize)`.
pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
if (builtin.os.tag == .windows) {
return windows.WriteFile(fd, bytes, null);
return windows.WriteFile(fd, bytes, null, false);
}
if (builtin.os.tag == .wasi and !builtin.link_libc) {
@ -709,7 +709,7 @@ pub const PWriteError = WriteError || error{Unseekable};
/// The corresponding POSIX limit is `math.maxInt(isize)`.
pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
if (std.Target.current.os.tag == .windows) {
return windows.WriteFile(fd, bytes, offset);
return windows.WriteFile(fd, bytes, offset, false);
}
// Prevent EINVAL.
@ -1670,38 +1670,40 @@ pub fn renameatZ(
}
}
/// Same as `renameat` except the parameters are null-terminated UTF16LE encoded byte arrays.
/// Assumes target is Windows.
/// TODO these args can actually be slices when using ntdll. audit the rest of the W functions too.
/// Same as `renameat` but Windows-only and the path parameters are
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
pub fn renameatW(
old_dir_fd: fd_t,
old_path: [*:0]const u16,
old_path_w: []const u16,
new_dir_fd: fd_t,
new_path_w: [*:0]const u16,
new_path_w: []const u16,
ReplaceIfExists: windows.BOOLEAN,
) RenameError!void {
const access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE;
const src_fd = windows.OpenFileW(old_dir_fd, old_path, null, access_mask, null, false, windows.FILE_OPEN) catch |err| switch (err) {
error.WouldBlock => unreachable,
const src_fd = windows.OpenFile(old_path_w, .{
.dir = old_dir_fd,
.access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE,
.creation = windows.FILE_OPEN,
.enable_async_io = false,
}) catch |err| switch (err) {
error.WouldBlock => unreachable, // Not possible without `.share_access_nonblocking = true`.
else => |e| return e,
};
defer windows.CloseHandle(src_fd);
const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (MAX_PATH_BYTES - 1);
var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION)) = undefined;
const new_path = mem.span(new_path_w);
const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path.len * 2;
const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2;
if (struct_len > struct_buf_len) return error.NameTooLong;
const rename_info = @ptrCast(*windows.FILE_RENAME_INFORMATION, &rename_info_buf);
rename_info.* = .{
.ReplaceIfExists = ReplaceIfExists,
.RootDirectory = if (std.fs.path.isAbsoluteWindowsW(new_path_w)) null else new_dir_fd,
.FileNameLength = @intCast(u32, new_path.len * 2), // already checked error.NameTooLong
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd,
.FileNameLength = @intCast(u32, new_path_w.len * 2), // already checked error.NameTooLong
.FileName = undefined,
};
std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path.len], new_path);
std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
var io_status_block: windows.IO_STATUS_BLOCK = undefined;

View File

@ -103,17 +103,19 @@ pub const OpenError = error{
WouldBlock,
};
/// TODO rename to CreateFileW
/// TODO actually we don't need the path parameter to be null terminated
pub fn OpenFileW(
dir: ?HANDLE,
sub_path_w: [*:0]const u16,
sa: ?*SECURITY_ATTRIBUTES,
pub const OpenFileOptions = struct {
access_mask: ACCESS_MASK,
share_access_opt: ?ULONG,
share_access_nonblocking: bool,
dir: ?HANDLE = null,
sa: ?*SECURITY_ATTRIBUTES = null,
share_access: ULONG = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
share_access_nonblocking: bool = false,
creation: ULONG,
) OpenError!HANDLE {
enable_async_io: bool = std.io.is_async,
};
/// TODO when share_access_nonblocking is false, this implementation uses
/// untinterruptible sleep() to block. This is not the final iteration of the API.
pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HANDLE {
if (sub_path_w[0] == '.' and sub_path_w[1] == 0) {
return error.IsDir;
}
@ -123,37 +125,37 @@ pub fn OpenFileW(
var result: HANDLE = undefined;
const path_len_bytes = math.cast(u16, mem.lenZ(sub_path_w) * 2) catch |err| switch (err) {
const path_len_bytes = math.cast(u16, sub_path_w.len * 2) catch |err| switch (err) {
error.Overflow => return error.NameTooLong,
};
var nt_name = UNICODE_STRING{
.Length = path_len_bytes,
.MaximumLength = path_len_bytes,
.Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w)),
.Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w.ptr)),
};
var attr = OBJECT_ATTRIBUTES{
.Length = @sizeOf(OBJECT_ATTRIBUTES),
.RootDirectory = if (std.fs.path.isAbsoluteWindowsW(sub_path_w)) null else dir,
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(sub_path_w)) null else options.dir,
.Attributes = 0, // Note we do not use OBJ_CASE_INSENSITIVE here.
.ObjectName = &nt_name,
.SecurityDescriptor = if (sa) |ptr| ptr.lpSecurityDescriptor else null,
.SecurityDescriptor = if (options.sa) |ptr| ptr.lpSecurityDescriptor else null,
.SecurityQualityOfService = null,
};
var io: IO_STATUS_BLOCK = undefined;
const share_access = share_access_opt orelse (FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE);
var delay: usize = 1;
while (true) {
const blocking_flag: ULONG = if (!options.enable_async_io) FILE_SYNCHRONOUS_IO_NONALERT else 0;
const rc = ntdll.NtCreateFile(
&result,
access_mask,
options.access_mask,
&attr,
&io,
null,
FILE_ATTRIBUTE_NORMAL,
share_access,
creation,
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
options.share_access,
options.creation,
FILE_NON_DIRECTORY_FILE | blocking_flag,
null,
0,
);
@ -165,14 +167,16 @@ pub fn OpenFileW(
.NO_MEDIA_IN_DEVICE => return error.NoDevice,
.INVALID_PARAMETER => unreachable,
.SHARING_VIOLATION => {
if (share_access_nonblocking) {
if (options.share_access_nonblocking) {
return error.WouldBlock;
}
// TODO sleep in a way that is interruptable
// TODO integrate with async I/O
std.time.sleep(delay);
if (delay < 1 * std.time.ns_per_s) {
delay *= 2;
}
continue; // TODO: don't loop for async
continue;
},
.ACCESS_DENIED => return error.AccessDenied,
.PIPE_BUSY => return error.PipeBusy,
@ -447,10 +451,11 @@ pub const ReadFileError = error{
/// If buffer's length exceeds what a Windows DWORD integer can hold, it will be broken into
/// multiple non-atomic reads.
pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usize {
if (std.event.Loop.instance) |loop| {
pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64, enable_async_io: bool) ReadFileError!usize {
if (std.event.Loop.instance != null and enable_async_io) {
const loop = std.event.Loop.instance.?;
// TODO support async ReadFile with no offset
const off = offset.?;
const off = if (offset == null) 0 else offset.?;
var resume_node = std.event.Loop.ResumeNode.Basic{
.base = .{
.id = .Basic,
@ -465,20 +470,20 @@ pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usiz
},
};
// TODO only call create io completion port once per fd
_ = windows.CreateIoCompletionPort(fd, loop.os_data.io_port, undefined, undefined) catch undefined;
_ = CreateIoCompletionPort(in_hFile, loop.os_data.io_port, undefined, undefined) catch undefined;
loop.beginOneEvent();
suspend {
// TODO handle buffer bigger than DWORD can hold
_ = windows.kernel32.ReadFile(fd, buffer.ptr, @intCast(windows.DWORD, buffer.len), null, &resume_node.base.overlapped);
_ = kernel32.ReadFile(in_hFile, buffer.ptr, @intCast(DWORD, buffer.len), null, &resume_node.base.overlapped);
}
var bytes_transferred: windows.DWORD = undefined;
if (windows.kernel32.GetOverlappedResult(fd, &resume_node.base.overlapped, &bytes_transferred, windows.FALSE) == 0) {
switch (windows.kernel32.GetLastError()) {
var bytes_transferred: DWORD = undefined;
if (kernel32.GetOverlappedResult(in_hFile, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
switch (kernel32.GetLastError()) {
.IO_PENDING => unreachable,
.OPERATION_ABORTED => return error.OperationAborted,
.BROKEN_PIPE => return error.BrokenPipe,
.HANDLE_EOF => return @as(usize, bytes_transferred),
else => |err| return windows.unexpectedError(err),
else => |err| return unexpectedError(err),
}
}
return @as(usize, bytes_transferred);
@ -520,10 +525,11 @@ pub const WriteFileError = error{
Unexpected,
};
pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError!usize {
if (std.event.Loop.instance) |loop| {
pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64, enable_async_io: bool) WriteFileError!usize {
if (std.event.Loop.instance != null and enable_async_io) {
const loop = std.event.Loop.instance.?;
// TODO support async WriteFile with no offset
const off = offset.?;
const off = if (offset == null) 0 else offset.?;
var resume_node = std.event.Loop.ResumeNode.Basic{
.base = .{
.id = .Basic,
@ -538,14 +544,14 @@ pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError
},
};
// TODO only call create io completion port once per fd
_ = CreateIoCompletionPort(fd, loop.os_data.io_port, undefined, undefined);
_ = CreateIoCompletionPort(handle, loop.os_data.io_port, undefined, undefined) catch undefined;
loop.beginOneEvent();
suspend {
const adjusted_len = math.cast(windows.DWORD, bytes.len) catch maxInt(windows.DWORD);
_ = kernel32.WriteFile(fd, bytes.ptr, adjusted_len, null, &resume_node.base.overlapped);
const adjusted_len = math.cast(DWORD, bytes.len) catch maxInt(DWORD);
_ = kernel32.WriteFile(handle, bytes.ptr, adjusted_len, null, &resume_node.base.overlapped);
}
var bytes_transferred: windows.DWORD = undefined;
if (kernel32.GetOverlappedResult(fd, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
var bytes_transferred: DWORD = undefined;
if (kernel32.GetOverlappedResult(handle, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
switch (kernel32.GetLastError()) {
.IO_PENDING => unreachable,
.INVALID_USER_BUFFER => return error.SystemResources,
@ -553,7 +559,7 @@ pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError
.OPERATION_ABORTED => return error.OperationAborted,
.NOT_ENOUGH_QUOTA => return error.SystemResources,
.BROKEN_PIPE => return error.BrokenPipe,
else => |err| return windows.unexpectedError(err),
else => |err| return unexpectedError(err),
}
}
return bytes_transferred;

View File

@ -470,7 +470,7 @@ pub const Pdb = struct {
msf: Msf,
pub fn openFile(self: *Pdb, coff_ptr: *coff.Coff, file_name: []u8) !void {
self.in_file = try fs.cwd().openFile(file_name, .{});
self.in_file = try fs.cwd().openFile(file_name, .{ .always_blocking = true });
self.allocator = coff_ptr.allocator;
self.coff = coff_ptr;