aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-05-01 19:02:16 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-05-01 19:02:16 -0400
commit988031c07c1959b05682f007ed3bc848a75a43d0 (patch)
tree372035990a592f73cc2a065106205c1a23494834 /lib/std
parent94b0d0e80242563f4ad7ad41e3c0f5193a60b70c (diff)
parent67e51311c3352ab4b2a381bd90dc386032254058 (diff)
downloadzig-988031c07c1959b05682f007ed3bc848a75a43d0.tar.gz
zig-988031c07c1959b05682f007ed3bc848a75a43d0.zip
Merge branch 'windows-evented-io' of https://github.com/FireFox317/zig into FireFox317-windows-evented-io
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/debug.zig334
-rw-r--r--lib/std/fs.zig67
-rw-r--r--lib/std/fs/file.zig22
-rw-r--r--lib/std/fs/path.zig4
-rw-r--r--lib/std/io.zig4
-rw-r--r--lib/std/os.zig38
-rw-r--r--lib/std/os/windows.zig82
-rw-r--r--lib/std/pdb.zig2
8 files changed, 300 insertions, 253 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig
index 4459907576..d3497b4dc8 100644
--- a/lib/std/debug.zig
+++ b/lib/std/debug.zig
@@ -666,158 +666,160 @@ 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 {
- const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
- errdefer coff_file.close();
+ noasync {
+ const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
+ errdefer coff_file.close();
- const coff_obj = try allocator.create(coff.Coff);
- coff_obj.* = coff.Coff.init(allocator, coff_file);
+ const coff_obj = try allocator.create(coff.Coff);
+ coff_obj.* = coff.Coff.init(allocator, coff_file);
- var di = ModuleDebugInfo{
- .base_address = undefined,
- .coff = coff_obj,
- .pdb = undefined,
- .sect_contribs = undefined,
- .modules = undefined,
- };
+ var di = ModuleDebugInfo{
+ .base_address = undefined,
+ .coff = coff_obj,
+ .pdb = undefined,
+ .sect_contribs = undefined,
+ .modules = undefined,
+ };
- try di.coff.loadHeader();
+ try di.coff.loadHeader();
- var path_buf: [windows.MAX_PATH]u8 = undefined;
- const len = try di.coff.getPdbPath(path_buf[0..]);
- const raw_path = path_buf[0..len];
+ var path_buf: [windows.MAX_PATH]u8 = undefined;
+ const len = try di.coff.getPdbPath(path_buf[0..]);
+ const raw_path = path_buf[0..len];
- const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
+ const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
- try di.pdb.openFile(di.coff, path);
+ try di.pdb.openFile(di.coff, path);
- var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
- const version = try pdb_stream.inStream().readIntLittle(u32);
- const signature = try pdb_stream.inStream().readIntLittle(u32);
- const age = try pdb_stream.inStream().readIntLittle(u32);
- var guid: [16]u8 = undefined;
- try pdb_stream.inStream().readNoEof(&guid);
- if (version != 20000404) // VC70, only value observed by LLVM team
- return error.UnknownPDBVersion;
- if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age)
- return error.PDBMismatch;
- // We validated the executable and pdb match.
+ var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
+ const version = try pdb_stream.inStream().readIntLittle(u32);
+ const signature = try pdb_stream.inStream().readIntLittle(u32);
+ const age = try pdb_stream.inStream().readIntLittle(u32);
+ var guid: [16]u8 = undefined;
+ try pdb_stream.inStream().readNoEof(&guid);
+ if (version != 20000404) // VC70, only value observed by LLVM team
+ return error.UnknownPDBVersion;
+ if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age)
+ return error.PDBMismatch;
+ // We validated the executable and pdb match.
- const string_table_index = str_tab_index: {
- const name_bytes_len = try pdb_stream.inStream().readIntLittle(u32);
- const name_bytes = try allocator.alloc(u8, name_bytes_len);
- try pdb_stream.inStream().readNoEof(name_bytes);
+ const string_table_index = str_tab_index: {
+ const name_bytes_len = try pdb_stream.inStream().readIntLittle(u32);
+ const name_bytes = try allocator.alloc(u8, name_bytes_len);
+ try pdb_stream.inStream().readNoEof(name_bytes);
- const HashTableHeader = packed struct {
- Size: u32,
- Capacity: u32,
+ const HashTableHeader = packed struct {
+ Size: u32,
+ Capacity: u32,
- fn maxLoad(cap: u32) u32 {
- return cap * 2 / 3 + 1;
- }
- };
- const hash_tbl_hdr = try pdb_stream.inStream().readStruct(HashTableHeader);
- if (hash_tbl_hdr.Capacity == 0)
- return error.InvalidDebugInfo;
+ fn maxLoad(cap: u32) u32 {
+ return cap * 2 / 3 + 1;
+ }
+ };
+ const hash_tbl_hdr = try pdb_stream.inStream().readStruct(HashTableHeader);
+ if (hash_tbl_hdr.Capacity == 0)
+ return error.InvalidDebugInfo;
- if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
- return error.InvalidDebugInfo;
+ if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
+ return error.InvalidDebugInfo;
- const present = try readSparseBitVector(&pdb_stream.inStream(), allocator);
- if (present.len != hash_tbl_hdr.Size)
- return error.InvalidDebugInfo;
- const deleted = try readSparseBitVector(&pdb_stream.inStream(), allocator);
+ const present = try readSparseBitVector(&pdb_stream.inStream(), allocator);
+ if (present.len != hash_tbl_hdr.Size)
+ return error.InvalidDebugInfo;
+ const deleted = try readSparseBitVector(&pdb_stream.inStream(), allocator);
- const Bucket = struct {
- first: u32,
- second: u32,
- };
- const bucket_list = try allocator.alloc(Bucket, present.len);
- for (present) |_| {
- const name_offset = try pdb_stream.inStream().readIntLittle(u32);
- const name_index = try pdb_stream.inStream().readIntLittle(u32);
- const name = mem.spanZ(@ptrCast([*:0]u8, name_bytes.ptr + name_offset));
- if (mem.eql(u8, name, "/names")) {
- break :str_tab_index name_index;
+ const Bucket = struct {
+ first: u32,
+ second: u32,
+ };
+ const bucket_list = try allocator.alloc(Bucket, present.len);
+ for (present) |_| {
+ const name_offset = try pdb_stream.inStream().readIntLittle(u32);
+ const name_index = try pdb_stream.inStream().readIntLittle(u32);
+ const name = mem.spanZ(@ptrCast([*:0]u8, name_bytes.ptr + name_offset));
+ if (mem.eql(u8, name, "/names")) {
+ break :str_tab_index name_index;
+ }
}
- }
- return error.MissingDebugInfo;
- };
+ return error.MissingDebugInfo;
+ };
- di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
- di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
+ di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
+ di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
- const dbi = di.pdb.dbi;
+ const dbi = di.pdb.dbi;
- // Dbi Header
- const dbi_stream_header = try dbi.inStream().readStruct(pdb.DbiStreamHeader);
- if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team
- return error.UnknownPDBVersion;
- if (dbi_stream_header.Age != age)
- return error.UnmatchingPDB;
+ // Dbi Header
+ const dbi_stream_header = try dbi.inStream().readStruct(pdb.DbiStreamHeader);
+ if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team
+ return error.UnknownPDBVersion;
+ if (dbi_stream_header.Age != age)
+ return error.UnmatchingPDB;
- const mod_info_size = dbi_stream_header.ModInfoSize;
- const section_contrib_size = dbi_stream_header.SectionContributionSize;
+ const mod_info_size = dbi_stream_header.ModInfoSize;
+ const section_contrib_size = dbi_stream_header.SectionContributionSize;
- var modules = ArrayList(Module).init(allocator);
+ var modules = ArrayList(Module).init(allocator);
- // Module Info Substream
- var mod_info_offset: usize = 0;
- while (mod_info_offset != mod_info_size) {
- const mod_info = try dbi.inStream().readStruct(pdb.ModInfo);
- var this_record_len: usize = @sizeOf(pdb.ModInfo);
+ // Module Info Substream
+ var mod_info_offset: usize = 0;
+ while (mod_info_offset != mod_info_size) {
+ const mod_info = try dbi.inStream().readStruct(pdb.ModInfo);
+ var this_record_len: usize = @sizeOf(pdb.ModInfo);
- const module_name = try dbi.readNullTermString(allocator);
- this_record_len += module_name.len + 1;
+ const module_name = try dbi.readNullTermString(allocator);
+ this_record_len += module_name.len + 1;
- const obj_file_name = try dbi.readNullTermString(allocator);
- this_record_len += obj_file_name.len + 1;
+ const obj_file_name = try dbi.readNullTermString(allocator);
+ this_record_len += obj_file_name.len + 1;
- if (this_record_len % 4 != 0) {
- const round_to_next_4 = (this_record_len | 0x3) + 1;
- const march_forward_bytes = round_to_next_4 - this_record_len;
- try dbi.seekBy(@intCast(isize, march_forward_bytes));
- this_record_len += march_forward_bytes;
- }
+ if (this_record_len % 4 != 0) {
+ const round_to_next_4 = (this_record_len | 0x3) + 1;
+ const march_forward_bytes = round_to_next_4 - this_record_len;
+ try dbi.seekBy(@intCast(isize, march_forward_bytes));
+ this_record_len += march_forward_bytes;
+ }
- try modules.append(Module{
- .mod_info = mod_info,
- .module_name = module_name,
- .obj_file_name = obj_file_name,
+ try modules.append(Module{
+ .mod_info = mod_info,
+ .module_name = module_name,
+ .obj_file_name = obj_file_name,
- .populated = false,
- .symbols = undefined,
- .subsect_info = undefined,
- .checksum_offset = null,
- });
+ .populated = false,
+ .symbols = undefined,
+ .subsect_info = undefined,
+ .checksum_offset = null,
+ });
- mod_info_offset += this_record_len;
- if (mod_info_offset > mod_info_size)
- return error.InvalidDebugInfo;
- }
+ mod_info_offset += this_record_len;
+ if (mod_info_offset > mod_info_size)
+ return error.InvalidDebugInfo;
+ }
- di.modules = modules.toOwnedSlice();
+ di.modules = modules.toOwnedSlice();
- // Section Contribution Substream
- var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
- var sect_cont_offset: usize = 0;
- if (section_contrib_size != 0) {
- const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.inStream().readIntLittle(u32));
- if (ver != pdb.SectionContrSubstreamVersion.Ver60)
- return error.InvalidDebugInfo;
- sect_cont_offset += @sizeOf(u32);
- }
- while (sect_cont_offset != section_contrib_size) {
- const entry = try sect_contribs.addOne();
- entry.* = try dbi.inStream().readStruct(pdb.SectionContribEntry);
- sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
+ // Section Contribution Substream
+ var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
+ var sect_cont_offset: usize = 0;
+ if (section_contrib_size != 0) {
+ const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.inStream().readIntLittle(u32));
+ if (ver != pdb.SectionContrSubstreamVersion.Ver60)
+ return error.InvalidDebugInfo;
+ sect_cont_offset += @sizeOf(u32);
+ }
+ while (sect_cont_offset != section_contrib_size) {
+ const entry = try sect_contribs.addOne();
+ entry.* = try dbi.inStream().readStruct(pdb.SectionContribEntry);
+ sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
- if (sect_cont_offset > section_contrib_size)
- return error.InvalidDebugInfo;
- }
+ if (sect_cont_offset > section_contrib_size)
+ return error.InvalidDebugInfo;
+ }
- di.sect_contribs = sect_contribs.toOwnedSlice();
+ di.sect_contribs = sect_contribs.toOwnedSlice();
- return di;
+ return di;
+ }
}
fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
@@ -1410,59 +1412,61 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
}
fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
- // Translate the VA into an address into this object
- const relocated_address = address - self.base_address;
- assert(relocated_address >= 0x100000000);
-
- // Find the .o file where this symbol is defined
- const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
- return SymbolInfo{};
-
- // Take the symbol name from the N_FUN STAB entry, we're going to
- // use it if we fail to find the DWARF infos
- const stab_symbol = mem.spanZ(self.strings[symbol.nlist.n_strx..]);
+ noasync {
+ // Translate the VA into an address into this object
+ const relocated_address = address - self.base_address;
+ assert(relocated_address >= 0x100000000);
- if (symbol.ofile == null)
- return SymbolInfo{ .symbol_name = stab_symbol };
+ // Find the .o file where this symbol is defined
+ const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
+ return SymbolInfo{};
- const o_file_path = mem.spanZ(self.strings[symbol.ofile.?.n_strx..]);
+ // Take the symbol name from the N_FUN STAB entry, we're going to
+ // use it if we fail to find the DWARF infos
+ const stab_symbol = mem.spanZ(self.strings[symbol.nlist.n_strx..]);
- // Check if its debug infos are already in the cache
- var o_file_di = self.ofiles.getValue(o_file_path) orelse
- (self.loadOFile(o_file_path) catch |err| switch (err) {
- error.FileNotFound,
- error.MissingDebugInfo,
- error.InvalidDebugInfo,
- => {
+ if (symbol.ofile == null)
return SymbolInfo{ .symbol_name = stab_symbol };
- },
- else => return err,
- });
- // Translate again the address, this time into an address inside the
- // .o file
- const relocated_address_o = relocated_address - symbol.reloc;
+ const o_file_path = mem.spanZ(self.strings[symbol.ofile.?.n_strx..]);
- if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
- return SymbolInfo{
- .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
- .compile_unit_name = compile_unit.die.getAttrString(&o_file_di, DW.AT_name) catch |err| switch (err) {
- error.MissingDebugInfo, error.InvalidDebugInfo => "???",
- else => return err,
+ // Check if its debug infos are already in the cache
+ var o_file_di = self.ofiles.getValue(o_file_path) orelse
+ (self.loadOFile(o_file_path) catch |err| switch (err) {
+ error.FileNotFound,
+ error.MissingDebugInfo,
+ error.InvalidDebugInfo,
+ => {
+ return SymbolInfo{ .symbol_name = stab_symbol };
},
- .line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o) catch |err| switch (err) {
- error.MissingDebugInfo, error.InvalidDebugInfo => null,
- else => return err,
+ else => return err,
+ });
+
+ // Translate again the address, this time into an address inside the
+ // .o file
+ const relocated_address_o = relocated_address - symbol.reloc;
+
+ if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
+ return SymbolInfo{
+ .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
+ .compile_unit_name = compile_unit.die.getAttrString(&o_file_di, DW.AT_name) catch |err| switch (err) {
+ error.MissingDebugInfo, error.InvalidDebugInfo => "???",
+ else => return err,
+ },
+ .line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o) catch |err| switch (err) {
+ error.MissingDebugInfo, error.InvalidDebugInfo => null,
+ else => return err,
+ },
+ };
+ } else |err| switch (err) {
+ error.MissingDebugInfo, error.InvalidDebugInfo => {
+ return SymbolInfo{ .symbol_name = stab_symbol };
},
- };
- } else |err| switch (err) {
- error.MissingDebugInfo, error.InvalidDebugInfo => {
- return SymbolInfo{ .symbol_name = stab_symbol };
- },
- else => return err,
- }
+ else => return err,
+ }
- unreachable;
+ unreachable;
+ }
}
},
.uefi, .windows => struct {
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index a4a5a3a56a..fc8a584a68 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -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 |
- (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) {
- .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),
+ .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),
+ .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 = 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)
- @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,
- };
-
+ const read_flag = if (flags.read) @as(u32, w.GENERIC_READ) else 0;
return @as(File, .{
- .handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, creation),
+ .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),
+ .enable_async_io = std.io.is_async,
+ }),
.io_mode = .blocking,
});
}
diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig
index 63adfc9648..4ff8d47287 100644
--- a/lib/std/fs/file.zig
+++ b/lib/std/fs/file.zig
@@ -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 {
diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig
index ba375645d5..e040330dd0 100644
--- a/lib/std/fs/path.zig
+++ b/lib/std/fs/path.zig
@@ -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 {
diff --git a/lib/std/io.zig b/lib/std/io.zig
index b498f2a299..b9ab3ecc8e 100644
--- a/lib/std/io.zig
+++ b/lib/std/io.zig
@@ -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,
};
}
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 06a4c12dfc..489afe43b7 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -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) {
@@ -370,7 +370,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
const first = iov[0];
return read(fd, first.iov_base[0..first.iov_len]);
}
-
+
const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
while (true) {
// TODO handle the case when iov_len is too large and get rid of this @intCast
@@ -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;
diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig
index 7118fabf7d..7f273c141a 100644
--- a/lib/std/os/windows.zig
+++ b/lib/std/os/windows.zig
@@ -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;
diff --git a/lib/std/pdb.zig b/lib/std/pdb.zig
index 422a51a800..0943bc44f3 100644
--- a/lib/std/pdb.zig
+++ b/lib/std/pdb.zig
@@ -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;