termio: use a dedicated io reader thread?

This commit is contained in:
Mitchell Hashimoto
2023-02-05 21:33:15 -08:00
parent 24373d84af
commit a5d03d1318

View File

@ -62,6 +62,8 @@ grid_size: renderer.GridSize,
/// The data associated with the currently running thread. /// The data associated with the currently running thread.
data: ?*EventData, data: ?*EventData,
io_thread: ?std.Thread,
/// Initialize the exec implementation. This will also start the child /// Initialize the exec implementation. This will also start the child
/// process. /// process.
pub fn init(alloc: Allocator, opts: termio.Options) !Exec { pub fn init(alloc: Allocator, opts: termio.Options) !Exec {
@ -151,6 +153,7 @@ pub fn init(alloc: Allocator, opts: termio.Options) !Exec {
.window_mailbox = opts.window_mailbox, .window_mailbox = opts.window_mailbox,
.grid_size = opts.grid_size, .grid_size = opts.grid_size,
.data = null, .data = null,
.io_thread = null,
}; };
} }
@ -230,20 +233,27 @@ pub fn threadEnter(self: *Exec, loop: *xev.Loop) !ThreadData {
}, },
}, },
}; };
errdefer ev_data_ptr.deinit(); errdefer ev_data_ptr.deinit(self.alloc);
// Store our data so our callbacks can access it // Store our data so our callbacks can access it
self.data = ev_data_ptr; self.data = ev_data_ptr;
// Start our stream read self.io_thread = try std.Thread.spawn(
stream.read( .{},
loop, ioMain,
&ev_data_ptr.data_stream_c_read, .{ self.pty.master, ev_data_ptr },
.{ .slice = &ev_data_ptr.data_stream_buf },
EventData,
ev_data_ptr,
ttyRead,
); );
self.io_thread.?.setName("io-reader") catch {};
// Start our stream read
// stream.read(
// loop,
// &ev_data_ptr.data_stream_c_read,
// .{ .slice = &ev_data_ptr.data_stream_buf },
// EventData,
// ev_data_ptr,
// ttyRead,
// );
// Return our thread data // Return our thread data
return ThreadData{ return ThreadData{
@ -252,6 +262,25 @@ pub fn threadEnter(self: *Exec, loop: *xev.Loop) !ThreadData {
}; };
} }
/// The main entrypoint for the thread.
pub fn ioMain(fd: std.os.fd_t, ev: *EventData) void {
while (true) {
const n = std.os.read(fd, &ev.data_stream_buf) catch |err| {
log.err("READ ERROR err={}", .{err});
return;
};
_ = @call(.always_inline, ttyRead, .{
ev,
undefined,
undefined,
undefined,
.{ .slice = &ev.data_stream_buf },
n,
});
}
}
pub fn threadExit(self: *Exec, data: ThreadData) void { pub fn threadExit(self: *Exec, data: ThreadData) void {
_ = data; _ = data;