From 00a210c03479d1b96c2001ef9458015a79f80795 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Mon, 2 Nov 2026 07:31:22 -0600 Subject: [PATCH] Send to a mailbox directly instead of through a sender handle --- src/agent.zig | 5 +-- src/mailbox.zig | 91 ++++++++++++++++++++++--------------------------- src/tui.zig | 22 +++++------- 3 files changed, 52 insertions(+), 66 deletions(-) diff --git a/src/agent.zig b/src/agent.zig index d263522..cd01cb5 100644 --- a/src/agent.zig +++ b/src/agent.zig @@ -42,7 +42,8 @@ pub const Event = union(enum) { /// full, and gives up when the mailbox closes. The receiver owns every string in /// an event and frees it. pub const Mailbox = mailbox.Mailbox(Event, 1024); -pub const Sink = Mailbox.Sender; +/// The send side of the mailbox. The agent only sends; the UI drains it. +pub const Sink = *Mailbox; /// Safety cap for one user prompt. pub const max_requests = 50; @@ -389,7 +390,7 @@ const Collector = struct { } fn sink(self: *Collector) Sink { - return self.mailbox.sender(); + return &self.mailbox; } /// Drains the mailbox, keeping the kind of each event and freeing the event. diff --git a/src/mailbox.zig b/src/mailbox.zig index 6c44b00..e4721f6 100644 --- a/src/mailbox.zig +++ b/src/mailbox.zig @@ -26,15 +26,6 @@ pub fn Mailbox(comptime T: type, comptime size: usize) type { notify: *const fn (ctx: *const anyopaque) void, }; - /// A copyable handle for producers. It stays valid while the mailbox does. - pub const Sender = struct { - mailbox: *Self, - - pub fn send(self: Sender, message: T, timeout: Timeout) bool { - return self.mailbox.push(message, timeout); - } - }; - io: std.Io, wake: ?Wake = null, ring: [size]T = undefined, @@ -54,13 +45,11 @@ pub fn Mailbox(comptime T: type, comptime size: usize) type { return size; } - pub fn sender(self: *Self) Sender { - return .{ .mailbox = self }; - } - - /// Queues a message. Returns false when it was not queued: the mailbox is - /// full and the timeout expired, or it is closed. - pub fn push(self: *Self, message: T, timeout: Timeout) bool { + /// Queues a message and wakes the consumer. Returns false when it was not + /// queued: the mailbox is full and the timeout expired, or it is closed. + /// Producers only call this; draining and closing belong to the consumer + /// thread, and the mailbox must outlive its producers. + pub fn send(self: *Self, message: T, timeout: Timeout) bool { { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); @@ -103,7 +92,7 @@ pub fn Mailbox(comptime T: type, comptime size: usize) type { } /// Removes every queued message under one lock. Call `deinit` on the - /// returned iterator, and do not push to this mailbox from the same + /// returned iterator, and do not send to this mailbox from the same /// thread before then. pub fn drain(self: *Self) Drain { self.mutex.lockUncancelable(self.io); @@ -186,10 +175,10 @@ const TestWaker = struct { test "messages drain in order and the mailbox empties" { var mailbox: TestMailbox = .init(std.testing.io); - const sender = mailbox.sender(); + const producer = &mailbox; - try std.testing.expect(sender.send(1, .instant)); - try std.testing.expect(sender.send(2, .instant)); + try std.testing.expect(producer.send(1, .instant)); + try std.testing.expect(producer.send(2, .instant)); try std.testing.expectEqual(@as(usize, 2), mailbox.count()); var it = mailbox.drain(); @@ -201,12 +190,12 @@ test "messages drain in order and the mailbox empties" { test "the ring wraps around and keeps its order" { var mailbox: TestMailbox = .init(std.testing.io); - const sender = mailbox.sender(); + const producer = &mailbox; var sent: u32 = 0; for (0..3) |_| { for (0..mailbox.capacity()) |_| { - try std.testing.expect(sender.send(sent, .instant)); + try std.testing.expect(producer.send(sent, .instant)); sent += 1; } var it = mailbox.drain(); @@ -220,34 +209,34 @@ test "the ring wraps around and keeps its order" { test "an instant send fails when the mailbox is full" { var mailbox: TestMailbox = .init(std.testing.io); - const sender = mailbox.sender(); + const producer = &mailbox; for (0..mailbox.capacity()) |index| { - try std.testing.expect(sender.send(@intCast(index), .instant)); + try std.testing.expect(producer.send(@intCast(index), .instant)); } - try std.testing.expect(!sender.send(99, .instant)); + try std.testing.expect(!producer.send(99, .instant)); try std.testing.expectEqual(@as(usize, mailbox.capacity()), mailbox.count()); } test "a waiting send completes once the consumer drains" { var mailbox: Mailbox(u32, 1) = .init(std.testing.io); - const sender = mailbox.sender(); - try std.testing.expect(sender.send(1, .instant)); + const producer = &mailbox; + try std.testing.expect(producer.send(1, .instant)); - const Producer = struct { - sender: Mailbox(u32, 1).Sender, + const ProducerThread = struct { + producer: *Mailbox(u32, 1), sent: std.atomic.Value(bool) = .init(false), fn run(self: *@This()) void { - if (self.sender.send(2, .forever)) self.sent.store(true, .release); + if (self.producer.send(2, .forever)) self.sent.store(true, .release); } }; - var producer: Producer = .{ .sender = sender }; - const thread = try std.Thread.spawn(.{}, Producer.run, .{&producer}); + var thread_state: ProducerThread = .{ .producer = &mailbox }; + const thread = try std.Thread.spawn(.{}, ProducerThread.run, .{&thread_state}); // Give the producer time to block on the full mailbox. std.testing.io.sleep(.fromMilliseconds(20), .awake) catch {}; - try std.testing.expect(!producer.sent.load(.acquire)); + try std.testing.expect(!thread_state.sent.load(.acquire)); { var it = mailbox.drain(); @@ -256,17 +245,17 @@ test "a waiting send completes once the consumer drains" { } thread.join(); - try std.testing.expect(producer.sent.load(.acquire)); + try std.testing.expect(thread_state.sent.load(.acquire)); try std.testing.expectEqual(@as(usize, 1), mailbox.count()); } test "a timed send gives up when nothing drains" { var mailbox: Mailbox(u32, 1) = .init(std.testing.io); - const sender = mailbox.sender(); - try std.testing.expect(sender.send(1, .instant)); + const producer = &mailbox; + try std.testing.expect(producer.send(1, .instant)); const started = std.Io.Timestamp.now(std.testing.io, .awake); - try std.testing.expect(!sender.send(2, .{ .ns = 10 * std.time.ns_per_ms })); + try std.testing.expect(!producer.send(2, .{ .ns = 10 * std.time.ns_per_ms })); const waited = started.untilNow(std.testing.io, .awake).nanoseconds; try std.testing.expect(waited >= 5 * std.time.ns_per_ms); } @@ -274,24 +263,24 @@ test "a timed send gives up when nothing drains" { test "the wake callback runs for a queued message and not for a dropped one" { var waker: TestWaker = .{}; var mailbox: Mailbox(u32, 1) = .{ .io = std.testing.io, .wake = waker.wake() }; - const sender = mailbox.sender(); + const producer = &mailbox; - try std.testing.expect(sender.send(1, .instant)); + try std.testing.expect(producer.send(1, .instant)); try std.testing.expectEqual(@as(usize, 1), waker.seen()); - try std.testing.expect(!sender.send(2, .instant)); + try std.testing.expect(!producer.send(2, .instant)); try std.testing.expectEqual(@as(usize, 1), waker.seen()); } test "a closed mailbox refuses messages but drains what it holds" { var mailbox: TestMailbox = .init(std.testing.io); - const sender = mailbox.sender(); + const producer = &mailbox; - try std.testing.expect(sender.send(7, .instant)); + try std.testing.expect(producer.send(7, .instant)); mailbox.close(); try std.testing.expect(mailbox.isClosed()); - try std.testing.expect(!sender.send(8, .instant)); + try std.testing.expect(!producer.send(8, .instant)); var it = mailbox.drain(); defer it.deinit(); @@ -301,24 +290,24 @@ test "a closed mailbox refuses messages but drains what it holds" { test "closing releases a producer waiting for room" { var mailbox: Mailbox(u32, 1) = .{ .io = std.testing.io }; - const sender = mailbox.sender(); - try std.testing.expect(sender.send(1, .instant)); + const producer = &mailbox; + try std.testing.expect(producer.send(1, .instant)); - const Producer = struct { - sender: Mailbox(u32, 1).Sender, + const ProducerThread = struct { + producer: *Mailbox(u32, 1), accepted: std.atomic.Value(bool) = .init(true), fn run(self: *@This()) void { - if (!self.sender.send(2, .forever)) self.accepted.store(false, .release); + if (!self.producer.send(2, .forever)) self.accepted.store(false, .release); } }; - var producer: Producer = .{ .sender = sender }; - const thread = try std.Thread.spawn(.{}, Producer.run, .{&producer}); + var thread_state: ProducerThread = .{ .producer = &mailbox }; + const thread = try std.Thread.spawn(.{}, ProducerThread.run, .{&thread_state}); std.testing.io.sleep(.fromMilliseconds(20), .awake) catch {}; mailbox.close(); thread.join(); - try std.testing.expect(!producer.accepted.load(.acquire)); + try std.testing.expect(!thread_state.accepted.load(.acquire)); try std.testing.expectEqual(@as(usize, 1), mailbox.count()); } diff --git a/src/tui.zig b/src/tui.zig index 433e74e..0e0a4ec 100644 --- a/src/tui.zig +++ b/src/tui.zig @@ -238,7 +238,7 @@ pub fn run( defer agent_mailbox.close(); var tick_mailbox: TickMailbox = .{ .io = io, .wake = .{ .ctx = &loop, .notify = wakeLoop } }; - var run_context: RunContext = .{ .agent_state = &agent_state, .sink = agent_mailbox.sender() }; + var run_context: RunContext = .{ .agent_state = &agent_state, .sink = &agent_mailbox }; var worker: Worker(*RunContext) = .{ .io = io, .gpa = gpa, @@ -267,7 +267,7 @@ pub fn run( }; defer ctx.cmds.deinit(gpa); - var ticker: Ticker = .init(io, tick_mailbox.sender()); + var ticker: Ticker = .init(io, &tick_mailbox); try ticker.start(); defer ticker.stop(); @@ -376,14 +376,14 @@ const TickMailbox = mailbox.Mailbox(vxfw.Tick, 1); const Ticker = struct { io: std.Io, - sink: TickMailbox.Sender, + sink: *TickMailbox, tick: ?vxfw.Tick = null, mutex: std.Io.Mutex = .init, condition: std.Io.Condition = .init, stopping: bool = false, thread: ?std.Thread = null, - fn init(io: std.Io, sink: TickMailbox.Sender) Ticker { + fn init(io: std.Io, sink: *TickMailbox) Ticker { return .{ .io = io, .sink = sink }; } @@ -692,10 +692,6 @@ const TickQueue = struct { self.condition.broadcast(self.io); } - fn sender(self: *TickQueue) TickMailbox.Sender { - return self.mailbox.sender(); - } - /// Waits for the next tick, then drains the mailbox. fn next(self: *TickQueue, timeout: std.Io.Duration) !vxfw.Tick { self.mutex.lockUncancelable(self.io); @@ -729,7 +725,7 @@ test "the ticker queues the armed tick when its deadline passes" { var widget_state: RearmingWidget = .{}; var queue: TickQueue = undefined; queue.init(); - var ticker = Ticker.init(testing.io, queue.sender()); + var ticker = Ticker.init(testing.io, &queue.mailbox); try ticker.start(); defer ticker.stop(); @@ -745,7 +741,7 @@ test "the ticker queues nothing while it has no armed tick" { var widget_state: RearmingWidget = .{}; var queue: TickQueue = undefined; queue.init(); - var ticker = Ticker.init(testing.io, queue.sender()); + var ticker = Ticker.init(testing.io, &queue.mailbox); try ticker.start(); defer ticker.stop(); @@ -764,7 +760,7 @@ test "a moved deadline takes effect before the old one expires" { var widget_state: RearmingWidget = .{}; var queue: TickQueue = undefined; queue.init(); - var ticker = Ticker.init(testing.io, queue.sender()); + var ticker = Ticker.init(testing.io, &queue.mailbox); try ticker.start(); defer ticker.stop(); @@ -779,7 +775,7 @@ test "stopping the ticker interrupts a long wait and queues nothing" { var widget_state: RearmingWidget = .{}; var queue: TickQueue = undefined; queue.init(); - var ticker = Ticker.init(testing.io, queue.sender()); + var ticker = Ticker.init(testing.io, &queue.mailbox); try ticker.start(); ticker.arm(tickIn(10_000, widget_state.widget())); @@ -793,7 +789,7 @@ test "a tick re-arms the ticker through the command the widget returns" { var widget_state: RearmingWidget = .{}; var queue: TickQueue = undefined; queue.init(); - var ticker = Ticker.init(testing.io, queue.sender()); + var ticker = Ticker.init(testing.io, &queue.mailbox); try ticker.start(); defer ticker.stop(); -- knotmirror 0.0.1