diff options
| author | sl0p <sl0p@sl0p.foo> | 2026-08-13 22:00:03 +0200 |
|---|---|---|
| committer | sl0p <sl0p@sl0p.foo> | 2026-08-13 22:00:03 +0200 |
| commit | 2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8 (patch) | |
| tree | 7cd6274303ba1da1f8bd57371aec336dd687fb58 /vendor/libghostty-vt/src/datastruct/blocking_queue.zig | |
| download | slosh-2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8.tar.gz slosh-2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8.tar.xz slosh-2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8.zip | |
sl0ptty: M0 — libghostty-vt vendored, one pane, compositor, diff renderer
Design contract in DESIGN.md. libghostty-vt vendored fresh from ghostty
main@bedb6f2 (zig 0.16), built to a 14MB static lib.
M0: pty spawn (no libutil), a lib-vt terminal per pane, a screen cell buffer,
a diff emitter that repaints only what changed, raw mode, SIGWINCH, and a
~120Hz frame cap that coalesces pty reads.
Also the seed of the headless harness: --headless dumps the composited screen
without a tty, so composition is testable in milliseconds.
tests/live_m0.py covers what headless cannot: raw mode, the diff emitter,
resize propagation, and the quit path. 6/6 green.
Diffstat (limited to 'vendor/libghostty-vt/src/datastruct/blocking_queue.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/datastruct/blocking_queue.zig | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/src/datastruct/blocking_queue.zig b/vendor/libghostty-vt/src/datastruct/blocking_queue.zig new file mode 100644 index 0000000..89ce57d --- /dev/null +++ b/vendor/libghostty-vt/src/datastruct/blocking_queue.zig @@ -0,0 +1,261 @@ +//! Blocking queue implementation aimed primarily for message passing +//! between threads. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const compat_thread = @import("../lib/compat/thread.zig"); + +/// Returns a blocking queue implementation for type T. +/// +/// This is tailor made for ghostty usage so it isn't meant to be maximally +/// generic, but I'm happy to make it more generic over time. Traits of this +/// queue that are specific to our usage: +/// +/// - Fixed size. We expect our queue to quickly drain and also not be +/// too large so we prefer a fixed size queue for now. +/// - No blocking pop. We use an external event loop mechanism such as +/// eventfd to notify our waiter that there is no data available so +/// we don't need to implement a blocking pop. +/// - Drain function. Most queues usually pop one at a time. We have +/// a mechanism for draining since on every IO loop our TTY drains +/// the full queue so we can get rid of the overhead of a ton of +/// locks and bounds checking and do a one-time drain. +/// +/// One key usage pattern is that our blocking queues are single producer +/// single consumer (SPSC). This should let us do some interesting optimizations +/// in the future. At the time of writing this, the blocking queue implementation +/// is purposely naive to build something quickly, but we should benchmark +/// and make this more optimized as necessary. +pub fn BlockingQueue( + comptime T: type, + comptime capacity: usize, +) type { + return struct { + const Self = @This(); + + // The type we use for queue size types. We can optimize this + // in the future to be the correct bit-size for our preallocated + // size for this queue. + pub const Size = u32; + + // The bounds of this queue. We recast this to Size so we can do math. + const bounds: Size = @intCast(capacity); + + /// Specifies the timeout for an operation. + pub const Timeout = union(enum) { + /// Fail instantly (non-blocking). + instant: void, + + /// Run forever or until interrupted + forever: void, + + /// Nanoseconds + ns: u64, + }; + + /// Our data. The values are undefined until they are written. + data: [bounds]T = undefined, + + /// The next location to write (next empty loc) and next location + /// to read (next non-empty loc). The number of written elements. + write: Size = 0, + read: Size = 0, + len: Size = 0, + + /// The big mutex that must be held to read/write. + mutex: std.Io.Mutex = .init, + + /// A CV for being notified when the queue is no longer full. This is + /// used for writing. Note we DON'T have a CV for waiting on the + /// queue not being EMPTY because we use external notifiers for that. + cond_not_full: std.Io.Condition = .init, + not_full_waiters: usize = 0, + + /// Allocate the blocking queue on the heap. + pub fn create(alloc: Allocator) Allocator.Error!*Self { + const ptr = try alloc.create(Self); + errdefer alloc.destroy(ptr); + + ptr.* = .{ + .data = undefined, + .len = 0, + .write = 0, + .read = 0, + .mutex = .init, + .cond_not_full = .init, + .not_full_waiters = 0, + }; + + return ptr; + } + + /// Free all the resources for this queue. This should only be + /// called once all producers and consumers have quit. + pub fn destroy(self: *Self, alloc: Allocator) void { + self.* = undefined; + alloc.destroy(self); + } + + /// Push a value to the queue. This returns the total size of the + /// queue (unread items) after the push. A return value of zero + /// means that the push failed. + pub fn push(self: *Self, io: std.Io, value: T, timeout: Timeout) Size { + self.mutex.lockUncancelable(io); + defer self.mutex.unlock(io); + + // The + if (self.full()) { + switch (timeout) { + // If we're not waiting, then we failed to write. + .instant => return 0, + + .forever => { + self.not_full_waiters += 1; + defer self.not_full_waiters -= 1; + self.cond_not_full.waitUncancelable(io, &self.mutex); + }, + + .ns => |ns| { + self.not_full_waiters += 1; + defer self.not_full_waiters -= 1; + compat_thread.waitTimeout( + &self.cond_not_full, + io, + &self.mutex, + .{ + .duration = .{ + .raw = .fromNanoseconds(ns), + .clock = .awake, + }, + }, + ) catch return 0; + }, + } + + // If we're still full, then we failed to write. This can + // happen in situations where we are interrupted. + if (self.full()) return 0; + } + + // Add our data and update our accounting + self.data[self.write] = value; + self.write += 1; + if (self.write >= bounds) self.write -= bounds; + self.len += 1; + + return self.len; + } + + /// Pop a value from the queue without blocking. + pub fn pop(self: *Self, io: std.Io) ?T { + self.mutex.lockUncancelable(io); + defer self.mutex.unlock(io); + + // If we're empty we have nothing + if (self.len == 0) return null; + + // Get the index we're going to read data from and do some + // accounting. We don't copy the value here to avoid copying twice. + const n = self.read; + self.read += 1; + if (self.read >= bounds) self.read -= bounds; + self.len -= 1; + + // If we have consumers waiting on a full queue, notify. + if (self.not_full_waiters > 0) self.cond_not_full.signal(io); + + return self.data[n]; + } + + /// Pop all values from the queue. This will hold the big mutex + /// until `deinit` is called on the return value. This is used if + /// you know you're going to "pop" and utilize all the values + /// quickly to avoid many locks, bounds checks, and cv signals. + pub fn drain(self: *Self, io: std.Io) DrainIterator { + self.mutex.lockUncancelable(io); + return .{ .queue = self }; + } + + pub const DrainIterator = struct { + queue: *Self, + + pub fn next(self: *DrainIterator) ?T { + if (self.queue.len == 0) return null; + + // Read and account + const n = self.queue.read; + self.queue.read += 1; + if (self.queue.read >= bounds) self.queue.read -= bounds; + self.queue.len -= 1; + + return self.queue.data[n]; + } + + pub fn deinit(self: *DrainIterator, io: std.Io) void { + // If we have consumers waiting on a full queue, notify. + if (self.queue.not_full_waiters > 0) self.queue.cond_not_full.signal(io); + + // Unlock + self.queue.mutex.unlock(io); + } + }; + + /// Returns true if the queue is full. This is not public because + /// it requires the lock to be held. + inline fn full(self: *Self) bool { + return self.len == bounds; + } + }; +} + +test "basic push and pop" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + const Q = BlockingQueue(u64, 4); + const q = try Q.create(alloc); + defer q.destroy(alloc); + + // Should have no values + try testing.expect(q.pop(io) == null); + + // Push until we're full + try testing.expectEqual(@as(Q.Size, 1), q.push(io, 1, .{ .instant = {} })); + try testing.expectEqual(@as(Q.Size, 2), q.push(io, 2, .{ .instant = {} })); + try testing.expectEqual(@as(Q.Size, 3), q.push(io, 3, .{ .instant = {} })); + try testing.expectEqual(@as(Q.Size, 4), q.push(io, 4, .{ .instant = {} })); + try testing.expectEqual(@as(Q.Size, 0), q.push(io, 5, .{ .instant = {} })); + + // Pop! + try testing.expect(q.pop(io).? == 1); + try testing.expect(q.pop(io).? == 2); + try testing.expect(q.pop(io).? == 3); + try testing.expect(q.pop(io).? == 4); + try testing.expect(q.pop(io) == null); + + // Drain does nothing + var it = q.drain(io); + try testing.expect(it.next() == null); + it.deinit(io); + + // Verify we can still push + try testing.expectEqual(@as(Q.Size, 1), q.push(io, 1, .{ .instant = {} })); +} + +test "timed push" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + const Q = BlockingQueue(u64, 1); + const q = try Q.create(alloc); + defer q.destroy(alloc); + + // Push + try testing.expectEqual(@as(Q.Size, 1), q.push(io, 1, .{ .instant = {} })); + try testing.expectEqual(@as(Q.Size, 0), q.push(io, 2, .{ .instant = {} })); + + // Timed push should fail + try testing.expectEqual(@as(Q.Size, 0), q.push(io, 2, .{ .ns = 1000 })); +} |
