From 3dc2bbc9b08615085f8eadf5103bc2ed88fe832a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 5 Nov 2023 15:54:50 -0800 Subject: [PATCH] os: add internal_os.pipe for cross-platfor pipe --- src/os/main.zig | 1 + src/os/pipe.zig | 19 +++++++++++++++++++ src/termio/Exec.zig | 12 ++---------- 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 src/os/pipe.zig diff --git a/src/os/main.zig b/src/os/main.zig index b2d073f55..7fdcb2d8b 100644 --- a/src/os/main.zig +++ b/src/os/main.zig @@ -9,6 +9,7 @@ pub usingnamespace @import("homedir.zig"); pub usingnamespace @import("locale.zig"); pub usingnamespace @import("macos_version.zig"); pub usingnamespace @import("mouse.zig"); +pub usingnamespace @import("pipe.zig"); pub usingnamespace @import("resourcesdir.zig"); pub const TempDir = @import("TempDir.zig"); pub const passwd = @import("passwd.zig"); diff --git a/src/os/pipe.zig b/src/os/pipe.zig new file mode 100644 index 000000000..67c6b943e --- /dev/null +++ b/src/os/pipe.zig @@ -0,0 +1,19 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const windows = @import("windows.zig"); + +/// pipe() that works on Windows and POSIX. +pub fn pipe() ![2]std.os.fd_t { + switch (builtin.os.tag) { + else => return try std.os.pipe(), + .windows => { + var read: windows.HANDLE = undefined; + var write: windows.HANDLE = undefined; + if (windows.exp.kernel32.CreatePipe(&read, &write, null, 0) == 0) { + return windows.unexpectedError(windows.kernel32.GetLastError()); + } + + return .{ read, write }; + }, + } +} diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 2dba63fd6..3ae0f5469 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -194,15 +194,7 @@ pub fn threadEnter(self: *Exec, thread: *termio.Thread) !ThreadData { // Create our pipe that we'll use to kill our read thread. // pipe[0] is the read end, pipe[1] is the write end. - const pipe = if (builtin.os.tag == .windows) pipe: { - var read: windows.HANDLE = undefined; - var write: windows.HANDLE = undefined; - if (windows.exp.kernel32.CreatePipe(&read, &write, null, 0) == 0) { - return windows.unexpectedError(windows.kernel32.GetLastError()); - } - - break :pipe .{ read, write }; - } else try std.os.pipe(); + const pipe = try internal_os.pipe(); errdefer std.os.close(pipe[0]); errdefer std.os.close(pipe[1]); @@ -1140,7 +1132,6 @@ const ReadThread = struct { } } - /// The main entrypoint for the thread. fn threadMainWindows(fd: std.os.fd_t, ev: *EventData, quit: std.os.fd_t) void { // Always close our end of the pipe when we exit. defer std.os.close(quit); @@ -1154,6 +1145,7 @@ const ReadThread = struct { switch (err) { // Check for a quit signal .OPERATION_ABORTED => break, + else => { log.err("io reader error err={}", .{err}); unreachable;