Skip to content

Instantly share code, notes, and snippets.

@lachie
Created February 5, 2022 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lachie/4b5e11589a00b644792f1df7b9596cf7 to your computer and use it in GitHub Desktop.
Save lachie/4b5e11589a00b644792f1df7b9596cf7 to your computer and use it in GitHub Desktop.
problem demo - interrupting a read
const std = @import("std");
const fs = std.fs;
const time = std.time;
pub const io_mode = .evented;
fn portReader(r: fs.File.Reader) !void {
var buf = [_]u8{0} ** 1024;
while (true) {
std.debug.print("awaiting input\n", .{});
const bytes = try r.read(buf[0..]);
std.debug.print("bytes: {any}\n", .{bytes});
if (bytes == 0) {
break;
}
std.debug.print("{s}", .{buf[0..bytes]});
// ... etc
}
}
pub fn main() anyerror!void {
const port = try fs.cwd().openFile("/dev/ttyACM0", .{});
defer port.close();
std.debug.print("port: {any}\n", .{port});
var readerFrame = async portReader(port.reader());
std.debug.print("sleeping 5s\n", .{});
time.sleep(time.ns_per_s * 5);
// What I really want to happen here is that the blocked read
// above is interrupted and returns 0.
// But it just keeps on being blocked.
std.debug.print("closing port\n", .{});
port.close();
try await readerFrame;
std.debug.print("done\n", .{});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment