1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
//! 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
//! its replica clients (`Peers`), its queue (the workers' and readers'
//! 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 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).
//! - `open(l) Host`: its devices made over the session's core and parts,
//! the host the core steps with (the core's, set there), who wakes it
//! when the queue gets a post (`l.inbox`); 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 watches, the ptys, the queue and the core go.
//! - `start(l)`: after the first effects, the ptys and the watches
//! read: 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).
//! - `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 Ptys = host_io.Ptys;
const file_watch = @import("dev").file_watch;
const daemon = @import("daemon.zig");
const upgrade = @import("upgrade.zig");
const ninep_io = @import("9p_io.zig");
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,
/// What the session's workers and readers post, for the frontend
/// to take in its wait.
inbox: host_io.Inbox = undefined,
ptys: Ptys = undefined,
watches: file_watch.Watches = 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.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);
l.ptys = .{ .queue = &l.inbox, .prompt_rcs = &l.prompt_rcs };
defer l.ptys.deinit();
l.watches = .open(l.io, l.gpa, &l.inbox);
defer l.watches.deinit();
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.
if (upgrade.resumed) |*rec| {
l.ptys.resumeFrom(l.core, rec);
l.effects();
}
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.
if (o.pair != null and daemon.link < 0 and l.peers.count() == 0) break;
fe.sent();
upgrade.locate();
// Asked in the step (the word), done between steps: the
// session whole.
fe.upgradeAsked();
// 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);
}
// 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);
l.fs = null;
}
}
fn effects(l: *L) void {
while (l.core.nextEffect()) |e| l.core.perform(e);
}
/// 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();
}
/// 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 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
/// let go (host_io.restoreCore); its shells and the frontend's
/// devices with it.
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);
// 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;
if (l.fs) |f| f.reset(nc);
l.core.deinit();
l.core = nc;
l.fe.restored(l);
}
};
}
|