diff options
Diffstat (limited to 'runtime/session.zig')
| -rw-r--r-- | runtime/session.zig | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/runtime/session.zig b/runtime/session.zig index fdd72c2b..9c8446e3 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -6,10 +6,10 @@ //! posts, `Inbox`), its ptys (`Ptys`) and the files it watches //! (`file_watch.Watches`): 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 other devices (the LSP's and the pipes' and addons' -//! jobs, the session's own input) are still its frontend's (each `Shell`); -//! the language servers' answers come through its queue (`Inbox.startLsp`). -//! What a frontend does is: +//! parts end in. Its workers (a language server's query, the pipes, the +//! addons' jobs and draws, a search) answer through its queue, which holds +//! their tasks and ends them (`Inbox.land`, `Inbox.deinit`); the session's +//! own input is its frontend's (each `Shell`). 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). @@ -25,8 +25,6 @@ //! 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 (an adopted shell, a client's -//! connection, a new daemon's hello taken), onto its queue. //! - `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). @@ -75,6 +73,7 @@ pub fn Loop(comptime Frontend: type) type { o.tree_sitter_allocator = l.allocs.tree_sitter; o.frame_allocator = l.allocs.frame; fe.options(&o); + o.tape = pardes.tape.fromEnv(); l.core = if (upgrade.resumed) |*rec| try upgrade.load(l.gpa, l.allocs.pardes, o, rec) else if (o.load_path) |lp| blk: { @@ -90,6 +89,8 @@ pub fn Loop(comptime Frontend: type) type { l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name() }; l.inbox = .{ .io = l.io, .gpa = l.gpa, .lsp_allocator = l.allocs.lsp }; defer l.inbox.deinit(); + // A search's worker may be waiting on the queue for room. + defer l.core.stream.cancelTask(); // The language servers' status lines, from the first effects on. pardes.lsp.setStatusSink(&l.inbox, host_io.Inbox.lspStatus); defer pardes.lsp.setStatusSink(null, null); @@ -100,9 +101,14 @@ pub fn Loop(comptime Frontend: type) type { l.host = try fe.open(&l); defer fe.close(&l); + daemon.notesTo(&l, note); defer l.peers.deinit(); // Before the core goes: each client sees its link end. defer l.peers.dropAll(l.core); + // A first client on the socket it was started with, before its + // first effects: it has every terminal from its first byte, and + // no reload (Core.openView). + if (o.pair) |fd| l.peers.join(l.core, .{ .fd = fd }); l.effects(); // ...and its shells, once a fresh session's own has a directory // to adopt them in. @@ -113,12 +119,19 @@ pub fn Loop(comptime Frontend: type) type { l.ptys.start(l.core); l.watches.start(); try fe.start(&l); + // A `new --detach`'s launch returns now. + daemon.launched(); 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); + // A pair's session with no daemon ends with its last link: + // nothing could attach to it again; with one, when its last + // client closed its window (a close, not a loss), no Detach + // typed in any. + if (o.pair != null and l.peers.count() == 0 and (daemon.link < 0 or l.peers.closed)) break; fe.sent(); upgrade.locate(); // Asked in the step (the word), done between steps: the @@ -132,6 +145,8 @@ pub fn Loop(comptime Frontend: type) type { if (l.core.quit) break; if (l.core.takeRestore()) |rp| l.restore(rp); } + // The clients' last word: the session ended (Exit). + l.peers.ended(0); // Its serve threads read the core: they end first. if (l.fs) |f| { f.deinit(l.gpa); @@ -143,17 +158,28 @@ pub fn Loop(comptime Frontend: type) type { while (l.core.nextEffect()) |e| l.core.perform(e); } - /// What the daemon says, onto the frontend's queue (once: its + /// What the daemon says, onto the session's queue (once: its /// reader is started by the first call); and a daemon started /// since the last one died told the shells' masters it held. pub fn watch(l: *L) void { if (daemon.relinked()) l.ptys.tellDaemon(); - daemon.watch(l.fe, note); + daemon.watch(); } + /// The daemon's thread: an adopted shell onto the queue (taken + /// in the frontend's wait), a client's connection to `Peers` + /// (joined at the next step boundary), a new daemon's hello taken + /// a wake (`watch` tells it the rest). fn note(ctx: *anyopaque, n: daemon.Note) void { - const fe: *Frontend = @ptrCast(@alignCast(ctx)); - fe.note(n); + const l: *L = @ptrCast(@alignCast(ctx)); + switch (n) { + .relinked => l.inbox.wake(), + .adopted => |a| { + for (a.pids[0..a.n], a.fds[0..a.n]) |pid, fd| l.inbox.push(.{ .adopted = .{ .pid = pid, .fd = fd } }); + l.inbox.push(.{ .adopted = .{ .pid = 0, .fd = -1 } }); + }, + .replica => |j| l.peers.handed(j.fd, j.view), + } } /// A Restore: the core its dump makes replaces this one, its clients @@ -162,7 +188,9 @@ pub fn Loop(comptime Frontend: type) type { fn restore(l: *L, path: []const u8) void { const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, l.watches.fd, &l.watches.table) orelse return; l.fe.restoring(nc); - // An answer to the old core's query is turned away. + // The old core's pipes end, and an answer to its query is + // turned away. + l.inbox.pipe_tasks.cancelAll(l.io); l.inbox.discardCompletions(); l.ptys.stop(); nc.host = l.host; |
