os: add internal_os.pipe for cross-platfor pipe

This commit is contained in:
Mitchell Hashimoto
2023-11-05 15:54:50 -08:00
parent fbd2c34487
commit 3dc2bbc9b0
3 changed files with 22 additions and 10 deletions

View File

@ -9,6 +9,7 @@ pub usingnamespace @import("homedir.zig");
pub usingnamespace @import("locale.zig"); pub usingnamespace @import("locale.zig");
pub usingnamespace @import("macos_version.zig"); pub usingnamespace @import("macos_version.zig");
pub usingnamespace @import("mouse.zig"); pub usingnamespace @import("mouse.zig");
pub usingnamespace @import("pipe.zig");
pub usingnamespace @import("resourcesdir.zig"); pub usingnamespace @import("resourcesdir.zig");
pub const TempDir = @import("TempDir.zig"); pub const TempDir = @import("TempDir.zig");
pub const passwd = @import("passwd.zig"); pub const passwd = @import("passwd.zig");

19
src/os/pipe.zig Normal file
View File

@ -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 };
},
}
}

View File

@ -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. // Create our pipe that we'll use to kill our read thread.
// pipe[0] is the read end, pipe[1] is the write end. // pipe[0] is the read end, pipe[1] is the write end.
const pipe = if (builtin.os.tag == .windows) pipe: { const pipe = try internal_os.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();
errdefer std.os.close(pipe[0]); errdefer std.os.close(pipe[0]);
errdefer std.os.close(pipe[1]); 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 { 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. // Always close our end of the pipe when we exit.
defer std.os.close(quit); defer std.os.close(quit);
@ -1154,6 +1145,7 @@ const ReadThread = struct {
switch (err) { switch (err) {
// Check for a quit signal // Check for a quit signal
.OPERATION_ABORTED => break, .OPERATION_ABORTED => break,
else => { else => {
log.err("io reader error err={}", .{err}); log.err("io reader error err={}", .{err});
unreachable; unreachable;