summaryrefslogtreecommitdiff
path: root/src/detached/server.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/detached/server.zig')
-rw-r--r--src/detached/server.zig103
1 files changed, 29 insertions, 74 deletions
diff --git a/src/detached/server.zig b/src/detached/server.zig
index 6c8d4352..989bdc0f 100644
--- a/src/detached/server.zig
+++ b/src/detached/server.zig
@@ -194,12 +194,7 @@ pub const Session = struct {
};
for (pair) |fd| {
if (libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)) < 0) return error.SocketOptionFailed;
- const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0));
- if (flags < 0) return error.SocketOptionFailed;
- var options: libc.O = @bitCast(@as(u32, @bitCast(flags)));
- options.NONBLOCK = true;
- if (libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(options))))) < 0)
- return error.SocketOptionFailed;
+ if (!setNonblock(fd)) return error.SocketOptionFailed;
if (comptime darwin) {
const on: c_int = 1;
if (libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int)) != 0)
@@ -213,7 +208,7 @@ pub const Session = struct {
fn cancelWorkers(s: *Session) void {
if (s.lsp_task) |*task| {
- task.future.cancel(s.io) catch {};
+ task.future.cancel(s.io);
s.lsp_task = null;
}
s.pipe_tasks.cancelAll(s.io);
@@ -229,7 +224,7 @@ pub const Session = struct {
.lsp => |result| {
s.core.update(.{ .lsp_resp = .{ .id = result.id, .rows = result.rows } });
if (s.lsp_task) |*task| if (task.id == result.id) {
- task.future.await(s.io) catch {};
+ task.future.await(s.io);
s.lsp_task = null;
};
},
@@ -345,7 +340,7 @@ pub const Session = struct {
_ = libc.close(fd);
return false;
}
- setNonblock(fd);
+ _ = setNonblock(fd);
s.listener = fd;
s.path_len = path.len;
return true;
@@ -371,7 +366,7 @@ pub const Session = struct {
fn clockNow(ctx: ?*anyopaque) u64 {
const s = of(ctx);
- return s.test_clock orelse host_io.monotonicNs();
+ return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
}
const vtable: host_io.Host.VTable = .{
@@ -404,25 +399,14 @@ pub const Session = struct {
fn lspRequest(ctx: ?*anyopaque, request: host_io.Lsp.Request) void {
const s = of(ctx);
_ = s.drainCompletions(true);
+ // Canceled here, not in `start`: what it delivered before it went
+ // is drained first, which leaves its slot in the mailbox free.
if (s.lsp_task) |*task| {
- task.future.cancel(s.io) catch {};
+ task.future.cancel(s.io);
s.lsp_task = null;
_ = s.drainCompletions(true);
}
- const job = host_io.Lsp.snapshot(s.worker_gpa, s.core, request) catch |err| {
- s.core.update(.{ .lsp_resp = .{ .id = request.id, .rows = null } });
- return s.core.reportError(request.pane, "lsp", err);
- };
- const future = s.io.concurrent(lspWorker, .{ s.io, s.worker_gpa, job, &s.mailbox }) catch |err| {
- job.free(s.worker_gpa);
- s.core.update(.{ .lsp_resp = .{ .id = request.id, .rows = null } });
- return s.core.reportError(request.pane, "lsp", err);
- };
- s.lsp_task = .{ .id = request.id, .future = future };
- }
-
- fn lspWorker(io: std.Io, gpa: std.mem.Allocator, job: *host_io.Lsp.Job, box: *Mailbox) anyerror!void {
- host_io.Lsp.work(io, gpa, job, box, deliverLspRows);
+ host_io.Lsp.start(s.io, s.worker_gpa, s.core, &s.lsp_task, request, &s.mailbox, deliverLspRows);
}
fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
@@ -443,26 +427,12 @@ pub const Session = struct {
fn pipeRequest(ctx: ?*anyopaque, id: u32) void {
const s = of(ctx);
_ = s.drainCompletions(true);
- if (s.pipe_tasks.full()) {
- s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
- return;
- }
- const request = s.core.pipe.pipeRequest(id) orelse return;
- const job = selection_pipe.Job.copy(s.worker_gpa, request) catch |err| {
- s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
- return s.core.reportError(s.core.active, "pipe", err);
- };
- const future = s.io.concurrent(pipeWorker, .{ s.io, s.worker_gpa, job, &s.mailbox }) catch |err| {
- job.deinit(s.worker_gpa);
- s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
- return s.core.reportError(s.core.active, "pipe", err);
- };
- std.debug.assert(s.pipe_tasks.add(.{ .id = id, .future = future }));
+ selection_pipe.start(s.io, s.worker_gpa, s.core, &s.pipe_tasks, id, &s.mailbox, deliverPipe);
}
- fn pipeWorker(io: std.Io, gpa: std.mem.Allocator, job: *selection_pipe.Job, box: *Mailbox) anyerror!void {
- defer job.deinit(gpa);
- box.post(.{ .pipe = selection_pipe.runJob(gpa, io, job) });
+ fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
+ const box: *Mailbox = @ptrCast(@alignCast(ctx.?));
+ box.post(.{ .pipe = response });
}
fn primary(s: *Session) ?*Client {
@@ -519,7 +489,7 @@ pub const Session = struct {
}
const command = if (s.core.panes[pane]) |pn| pn.command != null else false;
s.ptys[pane] = .{ .fd = child.file.handle, .pid = child.pid, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } };
- setNonblock(child.file.handle);
+ _ = setNonblock(child.file.handle);
var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined;
// Only a shell given no directory is asked where it is: one given a
// directory is named by it already (newShell), and asked now, before
@@ -682,7 +652,7 @@ pub const Session = struct {
pt.out = .empty;
if (pt.pid == 0) return;
_ = libc.kill(pt.pid, libc.SIG.HUP);
- pt.kill_at = monotonicMs() + 100;
+ pt.kill_at = std.Io.Clock.awake.now(s.io).toMilliseconds() + 100;
s.harvest();
if (pt.pid == 0) return;
for (&s.retired_shells) |*shell| if (shell.pid == 0) {
@@ -757,7 +727,7 @@ pub const Session = struct {
}
fn harvest(s: *Session) void {
- const now = monotonicMs();
+ const now = std.Io.Clock.awake.now(s.io).toMilliseconds();
for (&s.retired_shells) |*shell| host_io.reapRetired(&shell.pid, &shell.kill_at, now);
for (&s.ptys) |*pty| if (pty.fd < 0) host_io.reapRetired(&pty.pid, &pty.kill_at, now);
}
@@ -840,7 +810,7 @@ pub const Session = struct {
s.core.unattended = s.primary() == null;
pardes.Messages.expireUnattended(s.core);
- const now = monotonicMs();
+ const now = std.Io.Clock.awake.now(s.io).toMilliseconds();
var fds: [poll_slots]libc.pollfd = undefined;
var src: [poll_slots]Source = undefined;
var n: usize = 0;
@@ -894,7 +864,7 @@ pub const Session = struct {
// The wait is the 9P connections' turn with the core.
if (n == 0) {
pardes.turn.rest();
- nap(if (timeout_ms == 0) 16 else timeout_ms);
+ s.io.sleep(.fromMilliseconds(if (timeout_ms == 0) 16 else timeout_ms), .awake) catch {};
pardes.turn.wake();
if (timeout_ms != 0) if (s.test_clock) |*virtual| {
virtual.* = @max(virtual.*, s.core.nextWake() orelse virtual.*);
@@ -909,12 +879,12 @@ pub const Session = struct {
pardes.turn.wake();
if (ready > 0) s.dispatch(fds[0..n], src[0..n]);
// The core's wake ran out; the pump advances it from `now`.
- if (timeout_ms != 0 and monotonicMs() -| now >= timeout_ms) if (s.test_clock) |*virtual| {
+ if (timeout_ms != 0 and std.Io.Clock.awake.now(s.io).toMilliseconds() -| now >= timeout_ms) if (s.test_clock) |*virtual| {
virtual.* = @max(virtual.*, s.core.nextWake() orelse virtual.*);
};
_ = s.drainCompletions(true);
s.takeExits();
- s.expire(monotonicMs());
+ s.expire(std.Io.Clock.awake.now(s.io).toMilliseconds());
s.harvest();
s.reloadWatched();
_ = s.reconcile();
@@ -951,7 +921,6 @@ pub const Session = struct {
}
fn nextWake(s: *const Session, now: i64) ?c_int {
- if (now == 0) return null; // no clock; see `monotonicMs`
var due: ?i64 = null;
for (s.retired_shells) |shell| if (shell.pid != 0) {
due = now + 10;
@@ -998,13 +967,13 @@ pub const Session = struct {
switch (libc.errno(fd)) {
.AGAIN, .INTR, .CONNABORTED => return,
else => {
- s.accept_paused_ms = monotonicMs() + accept_pause_ms;
+ s.accept_paused_ms = std.Io.Clock.awake.now(s.io).toMilliseconds() + accept_pause_ms;
return;
},
}
}
ninep_io.setCloexec(fd);
- setNonblock(fd);
+ _ = setNonblock(fd);
if (comptime darwin) {
const on: c_int = 1;
_ = libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int));
@@ -1016,7 +985,7 @@ pub const Session = struct {
_ = libc.close(fd);
continue;
};
- s.clients[slot] = .{ .fd = fd, .accepted_ms = monotonicMs() };
+ s.clients[slot] = .{ .fd = fd, .accepted_ms = std.Io.Clock.awake.now(s.io).toMilliseconds() };
}
}
@@ -1432,8 +1401,8 @@ test "a detached session's terminal pane takes its shell with it when it closes"
const host = s.host();
host.vtable.close_pty.?(host.ctx, 3);
try std.testing.expect(s.ptys[3].fd < 0);
- const deadline = monotonicMs() + 2000;
- while (monotonicMs() < deadline) {
+ const deadline = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2000;
+ while (std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < deadline) {
s.harvest();
const running = s.ptys[3].pid != 0 or for (s.retired_shells) |shell| {
if (shell.pid == pid) break true;
@@ -1482,12 +1451,12 @@ test "detached worker setup failure completes requests without changing document
pub const setCloexec = ninep_io.setCloexec;
-pub fn setNonblock(fd: c_int) void {
+pub fn setNonblock(fd: c_int) bool {
const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0));
- if (flags < 0) return;
+ if (flags < 0) return false;
var o: libc.O = @bitCast(@as(u32, @bitCast(flags)));
o.NONBLOCK = true;
- _ = libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o)))));
+ return libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o))))) >= 0;
}
pub const nosignal: u32 = if (darwin) 0 else libc.MSG.NOSIGNAL;
@@ -1503,20 +1472,6 @@ fn retire(gpa: std.mem.Allocator, list: *std.ArrayListUnmanaged(u8)) void {
list.clearAndFree(gpa);
}
-pub fn monotonicMs() i64 {
- var ts: libc.timespec = undefined;
- if (libc.clock_gettime(.MONOTONIC, &ts) != 0) return 0;
- return @as(i64, ts.sec) * std.time.ms_per_s + @divTrunc(ts.nsec, std.time.ns_per_ms);
-}
-
-fn nap(ms: u32) void {
- var ts: libc.timespec = .{
- .sec = @intCast(ms / 1000),
- .nsec = @intCast((ms % 1000) * std.time.ns_per_ms),
- };
- _ = libc.nanosleep(&ts, null);
-}
-
pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[:0]const u8 {
if (name.len == 0) return null;
if (std.mem.indexOfAny(u8, name, "/\x00") != null) return null;
@@ -1560,7 +1515,7 @@ fn alive(path: [:0]const u8) bool {
if (fd < 0) return true;
defer _ = libc.close(fd);
ninep_io.setCloexec(fd);
- setNonblock(fd);
+ _ = setNonblock(fd);
const rc = libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr)));
if (rc == 0) return true;
return libc.errno(rc) != .CONNREFUSED;