diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/main.zig | 35 | ||||
| -rw-r--r-- | runtime/root.zig | 2 | ||||
| -rw-r--r-- | runtime/session.zig | 155 |
3 files changed, 190 insertions, 2 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index 92821c8a..2b600d63 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -595,6 +595,9 @@ const Here = struct { core: *pardes.Pardes, engine: Engine, fd: c_int, + /// Its serve thread, which reads the core until the other end hangs up + /// (`Tree.close` joins it). + thread: std.Thread, in: [@import("runtime").ninep.msize]u8, out: [2 * @import("runtime").ninep.msize]u8, buf: [@import("runtime").ninep.msize]u8, @@ -649,12 +652,23 @@ const Tree = struct { var fds: [2]std.c.fd_t = undefined; if (socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocketPair; h.fd = fds[1]; - (try std.Thread.spawn(.{}, Here.serve, .{h})).detach(); + h.thread = try std.Thread.spawn(.{}, Here.serve, .{h}); t.here = h; try t.f.init(io, .{ .socket = .{ .handle = fds[0], .address = undefined } }, "pardes"); return t; } + /// Let go (a test's; a command's process just ends). A core made here + /// is served to the end of what was sent it (a clunk, which the client + /// never waits for: Fsys.clunk) before its memory may go: its thread + /// sees the hangup and is joined. + fn close(t: *Tree) void { + t.f.close(); + const h = t.here orelse return; + h.thread.join(); + _ = std.c.close(h.fd); + } + /// The whole file at `path`. fn read(t: *Tree, gpa: std.mem.Allocator, path: []const u8) ![]const u8 { var out: std.Io.Writer.Allocating = .init(gpa); @@ -1861,7 +1875,7 @@ test "cli: docs/typ/command-line.txt is what pardes help --all prints" { defer arena.deinit(); const a = arena.allocator(); const t = try Tree.open(std.testing.io, a, null); - defer t.f.close(); + defer t.close(); var out: std.Io.Writer.Allocating = .init(a); try writeHelpAll(&out.writer, t); var dir = try std.Io.Dir.cwd().openDir(std.testing.io, @import("doc_typ").dir, .{}); @@ -1873,6 +1887,23 @@ test "cli: docs/typ/command-line.txt is what pardes help --all prints" { } } +test "cli: a core made here is let go only once its serve thread has ended: read, closed and freed at once, again and again" { + if (comptime !ninep_io.supported) return error.SkipZigTest; + // Each read ends with a clunk the client never waits for (Fsys.clunk): + // the serve thread is still answering it as the tree is closed and the + // core's memory let go (the page allocator's: unmapped, so a read of + // it after is a fault, not a stale value). + for (0..300) |_| { + var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator); + defer arena.deinit(); + const t = try Tree.open(std.testing.io, arena.allocator(), null); + defer t.close(); + var out: std.Io.Writer.Allocating = .init(arena.allocator()); + try t.f.readFile("README", &out.writer); + try std.testing.expect(out.written().len > 0); + } +} + test "cli: the first screen names every command, and every flag has its help" { var buf: [8192]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); diff --git a/runtime/root.zig b/runtime/root.zig index 61db8cbe..ec89e734 100644 --- a/runtime/root.zig +++ b/runtime/root.zig @@ -5,6 +5,7 @@ //! and `core`. pub const daemon = @import("daemon.zig"); pub const client = @import("client.zig"); +pub const session = @import("session.zig"); pub const upgrade = @import("upgrade.zig"); pub const crash = @import("crash.zig"); pub const addon_test = @import("addon_test.zig"); @@ -14,6 +15,7 @@ pub const ninep = @import("9p.zig"); test { _ = daemon; _ = client; + _ = session; _ = upgrade; _ = ninep_io; _ = ninep_io.Client; diff --git a/runtime/session.zig b/runtime/session.zig new file mode 100644 index 00000000..f401c85a --- /dev/null +++ b/runtime/session.zig @@ -0,0 +1,155 @@ +//! A session's loop: the terminal's (dev/tui `run`) and the window's +//! (dev/gui `runNative`) alike, each with a frontend of its own. It holds +//! the session's allocators and workers, its core (an upgrade's, a dump's +//! or a new one), the prompt files its shells start with, its 9P listener +//! and its replica clients (`Peers`): a step, its unit to the clients and +//! theirs taken in, an Upgrade or a Restore between steps; and the order +//! its parts end in. Its devices (the ptys and their readers, the files +//! watched, the LSP's and the pipes' and addons' jobs, the queue they post +//! to, the session's own input) are still its frontend's (each `Shell`, +//! the services step's to make one). What a frontend does is: +//! +//! - `options(o)`: before the core: what it gives the core it starts (a +//! window's size, for a dump's layout). +//! - `open(l) Host`: its devices made over the session's core and parts, +//! the host the core steps with (the core's, set there); what it does +//! before the core's first effects (an upgraded terminal's panes its +//! own again, whether anyone looks). +//! - `close(l)`: its devices stopped, after the clients are let go and +//! before the core goes. +//! - `resumePtys(rec)`: an upgraded session's shells, its own again. +//! - `start(l)`: after the first effects: its input, its readers, the +//! listener's wake, a detached session's first wait (`l.watch`). +//! - `sent()`: after each step's unit went to the clients. +//! - `upgradeAsked()`: an Upgrade asked in the step, done (it execs). +//! - `note(n)`: what the daemon says (a terminal, an adopted shell, a +//! client's connection), onto its queue. +//! - `watched()`: the files it watches (a Restore stops watching them). +//! - `restoring(nc)`, `restored(l)`: a Restore's new core, before it +//! replaces the old one (its devices let go, what it is told of the +//! frontend) and after (its own view of it again). +const std = @import("std"); +const pardes = @import("core"); +const replica = pardes.replica; +const host_io = @import("dev").host_io; +const file_watch = @import("dev").file_watch; +const daemon = @import("daemon.zig"); +const upgrade = @import("upgrade.zig"); +const ninep_io = @import("9p_io.zig"); + +/// The files a frontend watches (host_io.restoreCore lets them go). +pub const Watched = struct { fd: c_int, table: *file_watch.Table }; + +pub fn Loop(comptime Frontend: type) type { + return struct { + const L = @This(); + + io: std.Io, + gpa: std.mem.Allocator, + allocs: pardes.memory.Allocators, + /// Its frame timings, the frontend's to write as it draws + /// (Pardes.frames). + frames: pardes.FrameStats = .{}, + core: *pardes.Pardes = undefined, + prompt_rcs: host_io.Shell.PromptFiles = undefined, + fs: ?*ninep_io.Listener = null, + peers: replica.Peers = .{}, + host: pardes.Host = undefined, + fe: *Frontend, + + /// The session, from its start to its end. + pub fn run(init: std.process.Init, opts: pardes.Options, fe: *Frontend) !void { + var l: L = .{ .io = init.io, .gpa = init.gpa, .allocs = pardes.memory.init(init.gpa), .fe = fe }; + defer pardes.memory.deinit(); + host_io.startWorkers(l.allocs); + defer host_io.stopWorkers(); + var o = opts; + o.addons = pardes.Addon.scan(); + o.frames = &l.frames; + o.image_allocator = l.allocs.image; + o.tree_sitter_allocator = l.allocs.tree_sitter; + o.frame_allocator = l.allocs.frame; + fe.options(&o); + l.core = if (upgrade.resumed) |*rec| + try upgrade.load(l.gpa, l.allocs.pardes, o, rec) + else if (o.load_path) |lp| blk: { + const bytes = try pardes.filesystem.readFile(&pardes.tape.none, l.gpa, lp); + defer l.gpa.free(bytes); + break :blk try pardes.dump.initFromDump(l.allocs.pardes, o, bytes); + } else try pardes.Pardes.init(l.allocs.pardes, o); + defer l.core.deinit(); + l.prompt_rcs = host_io.Shell.prepare(); + defer l.prompt_rcs.deinit(); + l.fs = ninep_io.start(l.io, l.gpa, l.core); + defer if (l.fs) |f| f.deinit(l.gpa); + l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name() }; + + l.host = try fe.open(&l); + defer fe.close(&l); + defer l.peers.deinit(); + // Before the core goes: each client sees its link end. + defer l.peers.dropAll(l.core); + l.effects(); + // ...and its shells, once a fresh session's own has a directory + // to adopt them in. + if (upgrade.resumed) |*rec| { + fe.resumePtys(rec); + l.effects(); + } + try fe.start(&l); + + while (!l.core.quit) { + pardes.turn.bump(&pardes.turn.restores); + try l.core.pump(l.host); + // Its unit to each replica client, and theirs taken in. + l.peers.boundary(l.core); + fe.sent(); + upgrade.locate(); + // Asked in the step (the word), done between steps: the + // session whole. + fe.upgradeAsked(); + // A terminal (`pardes --attach`), a window asked for again, + // an adopted shell or a client's connection, handed on by + // the daemon's thread; started after the first frame, off a + // start's way. + l.watch(); + // A session that ended does not restore into one. + if (l.core.quit) break; + if (l.core.takeRestore()) |rp| l.restore(rp); + } + // Its serve threads read the core: they end first. + if (l.fs) |f| { + f.deinit(l.gpa); + l.fs = null; + } + } + + fn effects(l: *L) void { + while (l.core.nextEffect()) |e| l.core.perform(e); + } + + /// What the daemon says, onto the frontend's queue (once: its + /// reader is started by the first call). + pub fn watch(l: *L) void { + daemon.watch(l.fe, note); + } + + fn note(ctx: *anyopaque, n: daemon.Note) void { + const fe: *Frontend = @ptrCast(@alignCast(ctx)); + fe.note(n); + } + + /// A Restore: the core its dump makes replaces this one, its clients + /// let go (host_io.restoreCore); the frontend's devices with it. + fn restore(l: *L, path: []const u8) void { + const w = l.fe.watched(); + const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, w.fd, w.table) orelse return; + l.fe.restoring(nc); + nc.host = l.host; + if (l.fs) |f| f.reset(nc); + l.core.deinit(); + l.core = nc; + l.fe.restored(l); + } + }; +} |
