Merge pull request #861 from mitchellh/writer-block

termio: wake up writer thread if the writer mailbox is full
This commit is contained in:
Mitchell Hashimoto
2023-11-10 22:06:35 -08:00
committed by GitHub
2 changed files with 18 additions and 2 deletions

View File

@ -60,7 +60,9 @@ pub fn Stream(comptime Handler: type) type {
for (actions) |action_opt| {
const action = action_opt orelse continue;
// if (action != .print) {
// log.info("action: {}", .{action});
// }
// If this handler handles everything manually then we do nothing
// if it can be processed.

View File

@ -1398,7 +1398,21 @@ const StreamHandler = struct {
}
inline fn messageWriter(self: *StreamHandler, msg: termio.Message) void {
// Try to write to the mailbox with an instant timeout. If the
// mailbox is full, then we wake up the writer thread mid-processing
// so it can process the message and then try again with a forever
// wait.
if (self.ev.writer_mailbox.push(msg, .{ .instant = {} }) == 0) {
self.ev.writer_wakeup.notify() catch |err| {
log.warn("failed to wake up writer, data will be dropped err={}", .{err});
return;
};
_ = self.ev.writer_mailbox.push(msg, .{ .forever = {} });
}
// Normally, we just flag this true to wake up the writer thread
// once per batch of data.
self.writer_messaged = true;
}