From 0f73adfda1cff9c740160717b5431ebada6b8755 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 22 Mar 2024 11:24:15 -0700 Subject: [PATCH] fix various windows build issues --- src/backend/iocp.zig | 63 ++++++++++++++++++++++---------------------- src/watcher/tcp.zig | 2 +- src/watcher/udp.zig | 2 +- src/windows.zig | 11 ++++---- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/backend/iocp.zig b/src/backend/iocp.zig index 508a7f2..55b08ea 100644 --- a/src/backend/iocp.zig +++ b/src/backend/iocp.zig @@ -5,6 +5,7 @@ const windows = @import("../windows.zig"); const queue = @import("../queue.zig"); const heap = @import("../heap.zig"); const xev = @import("../main.zig").IOCP; +const posix = std.posix; const log = std.log.scoped(.libxev_iocp); @@ -263,7 +264,7 @@ pub const Loop = struct { self.asyncs = .{}; while (asyncs.pop()) |c| { - const c_wakeup = c.op.async_wait.wakeup.swap(false, .SeqCst); + const c_wakeup = c.op.async_wait.wakeup.swap(false, .seq_cst); // If we aren't waking this one up, requeue if (!c_wakeup) { @@ -477,15 +478,15 @@ pub const Loop = struct { .accept => |*v| action: { if (v.internal_accept_socket == null) { - var addr: std.os.sockaddr.storage = undefined; - var addr_len: i32 = @sizeOf(std.os.sockaddr.storage); + var addr: posix.sockaddr.storage = undefined; + var addr_len: i32 = @sizeOf(posix.sockaddr.storage); - std.debug.assert(windows.ws2_32.getsockname(asSocket(v.socket), @as(*std.os.sockaddr, @ptrCast(&addr)), &addr_len) == 0); + std.debug.assert(windows.ws2_32.getsockname(asSocket(v.socket), @as(*posix.sockaddr, @ptrCast(&addr)), &addr_len) == 0); var socket_type: i32 = 0; const socket_type_bytes = std.mem.asBytes(&socket_type); var opt_len: i32 = @as(i32, @intCast(socket_type_bytes.len)); - std.debug.assert(windows.ws2_32.getsockopt(asSocket(v.socket), std.os.SOL.SOCKET, std.os.SO.TYPE, socket_type_bytes, &opt_len) == 0); + std.debug.assert(windows.ws2_32.getsockopt(asSocket(v.socket), posix.SOL.SOCKET, posix.SO.TYPE, socket_type_bytes, &opt_len) == 0); v.internal_accept_socket = windows.WSASocketW(addr.family, socket_type, 0, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED) catch |err| { break :action .{ .result = .{ .accept = err } }; @@ -501,7 +502,7 @@ pub const Loop = struct { &v.storage, 0, 0, - @as(u32, @intCast(@sizeOf(std.os.sockaddr.storage))), + @as(u32, @intCast(@sizeOf(posix.sockaddr.storage))), &discard, &completion.overlapped, ); @@ -560,7 +561,7 @@ pub const Loop = struct { }; }, - .shutdown => |*v| .{ .result = .{ .shutdown = std.os.shutdown(asSocket(v.socket), v.how) } }, + .shutdown => |*v| .{ .result = .{ .shutdown = posix.shutdown(asSocket(v.socket), v.how) } }, .write => |*v| action: { self.associate_fd(completion.handle().?) catch unreachable; @@ -830,7 +831,7 @@ pub const Loop = struct { // The completion has been wakeup, this is used to see which completion in the async queue // needs to be removed. - completion.op.async_wait.wakeup.store(true, .SeqCst); + completion.op.async_wait.wakeup.store(true, .seq_cst); const result = windows.kernel32.PostQueuedCompletionStatus( self.iocp_handle, @@ -1065,11 +1066,11 @@ pub const Completion = struct { var opt_len: i32 = @as(i32, @intCast(socket_type_bytes.len)); // Here we assume the call will succeed because the socket should be valid. - std.debug.assert(windows.ws2_32.getsockopt(asSocket(v.fd), std.os.SOL.SOCKET, std.os.SO.TYPE, socket_type_bytes, &opt_len) == 0); + std.debug.assert(windows.ws2_32.getsockopt(asSocket(v.fd), posix.SOL.SOCKET, posix.SO.TYPE, socket_type_bytes, &opt_len) == 0); break :t socket_type; }; - if (socket_type == std.os.SOCK.STREAM and bytes_transferred == 0) { + if (socket_type == posix.SOCK.STREAM and bytes_transferred == 0) { break :r .{ .recv = error.EOF }; } @@ -1197,7 +1198,7 @@ pub const Operation = union(OperationType) { accept: struct { socket: windows.HANDLE, - storage: [@sizeOf(std.os.sockaddr.storage)]u8 = undefined, + storage: [@sizeOf(posix.sockaddr.storage)]u8 = undefined, internal_accept_socket: ?windows.HANDLE = null, }, @@ -1224,7 +1225,7 @@ pub const Operation = union(OperationType) { shutdown: struct { socket: windows.HANDLE, - how: std.os.ShutdownHow = .both, + how: posix.ShutdownHow = .both, }, write: struct { @@ -1260,8 +1261,8 @@ pub const Operation = union(OperationType) { recvfrom: struct { fd: windows.HANDLE, buffer: ReadBuffer, - addr: std.os.sockaddr = undefined, - addr_size: std.os.socklen_t = @sizeOf(std.os.sockaddr), + addr: posix.sockaddr = undefined, + addr_size: posix.socklen_t = @sizeOf(posix.sockaddr), wsa_buffer: windows.ws2_32.WSABUF = undefined, }, @@ -1329,7 +1330,7 @@ pub const ConnectError = error{ Unexpected, }; -pub const ShutdownError = std.os.ShutdownError || error{ +pub const ShutdownError = posix.ShutdownError || error{ Unexpected, }; @@ -1954,16 +1955,16 @@ test "iocp: socket accept/connect/send/recv/close" { // Create a TCP server socket const address = try net.Address.parseIp4("127.0.0.1", 3131); const kernel_backlog = 1; - const ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); - errdefer std.os.close(ln); + const ln = try windows.WSASocketW(posix.AF.INET, posix.SOCK.STREAM, posix.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); + errdefer posix.close(ln); - try std.os.setsockopt(ln, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); - try std.os.bind(ln, &address.any, address.getOsSockLen()); - try std.os.listen(ln, kernel_backlog); + try posix.setsockopt(ln, posix.SOL.SOCKET, posix.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); + try posix.bind(ln, &address.any, address.getOsSockLen()); + try posix.listen(ln, kernel_backlog); // Create a TCP client socket - const client_conn = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); - errdefer std.os.close(client_conn); + const client_conn = try windows.WSASocketW(posix.AF.INET, posix.SOCK.STREAM, posix.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); + errdefer posix.close(client_conn); var server_conn_result: Result = undefined; var c_accept: Completion = .{ @@ -2220,11 +2221,11 @@ test "iocp: recv cancellation" { // Create a TCP server socket const address = try net.Address.parseIp4("127.0.0.1", 3131); - const socket = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); - errdefer std.os.close(socket); + const socket = try windows.WSASocketW(posix.AF.INET, posix.SOCK.DGRAM, posix.IPPROTO.UDP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); + errdefer posix.close(socket); - try std.os.setsockopt(socket, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); - try std.os.bind(socket, &address.any, address.getOsSockLen()); + try posix.setsockopt(socket, posix.SOL.SOCKET, posix.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); + try posix.bind(socket, &address.any, address.getOsSockLen()); var recv_buf: [128]u8 = undefined; var recv_result: Result = undefined; @@ -2293,12 +2294,12 @@ test "iocp: accept cancellation" { // Create a TCP server socket const address = try net.Address.parseIp4("127.0.0.1", 3131); const kernel_backlog = 1; - const ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); - errdefer std.os.close(ln); + const ln = try windows.WSASocketW(posix.AF.INET, posix.SOCK.STREAM, posix.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED); + errdefer posix.close(ln); - try std.os.setsockopt(ln, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); - try std.os.bind(ln, &address.any, address.getOsSockLen()); - try std.os.listen(ln, kernel_backlog); + try posix.setsockopt(ln, posix.SOL.SOCKET, posix.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1))); + try posix.bind(ln, &address.any, address.getOsSockLen()); + try posix.listen(ln, kernel_backlog); var server_conn_result: Result = undefined; var c_accept: Completion = .{ diff --git a/src/watcher/tcp.zig b/src/watcher/tcp.zig index bcdf36d..e923b50 100644 --- a/src/watcher/tcp.zig +++ b/src/watcher/tcp.zig @@ -33,7 +33,7 @@ pub fn TCP(comptime xev: type) type { if (xev.backend == .wasi_poll) @compileError("unsupported in WASI"); const fd = if (xev.backend == .iocp) - try std.os.windows.WSASocketW(addr.any.family, std.os.SOCK.STREAM, 0, null, 0, std.os.windows.ws2_32.WSA_FLAG_OVERLAPPED) + try std.os.windows.WSASocketW(addr.any.family, posix.SOCK.STREAM, 0, null, 0, std.os.windows.ws2_32.WSA_FLAG_OVERLAPPED) else fd: { // On io_uring we don't use non-blocking sockets because we may // just get EAGAIN over and over from completions. diff --git a/src/watcher/udp.zig b/src/watcher/udp.zig index d742ff6..1b26a39 100644 --- a/src/watcher/udp.zig +++ b/src/watcher/udp.zig @@ -214,7 +214,7 @@ fn UDPSendto(comptime xev: type) type { fn UDPSendtoIOCP(comptime xev: type) type { return struct { const Self = @This(); - const windows = std.posix.windows; + const windows = std.os.windows; fd: windows.HANDLE, diff --git a/src/windows.zig b/src/windows.zig index a8bb380..7716e9e 100644 --- a/src/windows.zig +++ b/src/windows.zig @@ -1,5 +1,6 @@ const std = @import("std"); const windows = std.os.windows; +const posix = std.posix; pub usingnamespace std.os.windows; @@ -103,7 +104,7 @@ pub const exp = struct { ) callconv(windows.WINAPI) windows.BOOL; }; - pub const CreateFileError = error{} || std.os.UnexpectedError; + pub const CreateFileError = error{} || posix.UnexpectedError; pub fn CreateFile( lpFileName: [*:0]const u16, @@ -161,7 +162,7 @@ pub const exp = struct { return @as(usize, @intCast(written)); } - pub const DeleteFileError = error{} || std.os.UnexpectedError; + pub const DeleteFileError = error{} || posix.UnexpectedError; pub fn DeleteFile(name: [*:0]const u16) DeleteFileError!void { const result: windows.BOOL = windows.kernel32.DeleteFileW(name); @@ -173,7 +174,7 @@ pub const exp = struct { } } - pub const CreateJobObjectError = error{AlreadyExists} || std.os.UnexpectedError; + pub const CreateJobObjectError = error{AlreadyExists} || posix.UnexpectedError; pub fn CreateJobObject( lpSecurityAttributes: ?*windows.SECURITY_ATTRIBUTES, lpName: ?windows.LPCSTR, @@ -186,7 +187,7 @@ pub const exp = struct { }; } - pub fn AssignProcessToJobObject(hJob: windows.HANDLE, hProcess: windows.HANDLE) std.os.UnexpectedError!void { + pub fn AssignProcessToJobObject(hJob: windows.HANDLE, hProcess: windows.HANDLE) posix.UnexpectedError!void { const result: windows.BOOL = kernel32.AssignProcessToJobObject(hJob, hProcess); if (result == windows.FALSE) { const err = windows.kernel32.GetLastError(); @@ -201,7 +202,7 @@ pub const exp = struct { JobObjectInformationClass: JOBOBJECT_INFORMATION_CLASS, lpJobObjectInformation: windows.LPVOID, cbJobObjectInformationLength: windows.DWORD, - ) std.os.UnexpectedError!void { + ) posix.UnexpectedError!void { const result: windows.BOOL = kernel32.SetInformationJobObject( hJob, JobObjectInformationClass,