Skip to content

Commit

Permalink
Improve TTY, use BoundedArray instead of ArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Nov 23, 2023
1 parent e7b7c2e commit e57ce38
Show file tree
Hide file tree
Showing 8 changed files with 558 additions and 575 deletions.
1,053 changes: 526 additions & 527 deletions kernel/TTY.zig

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, ret_addr: ?usize) nor
dmesg_writer.print(fmt, .{msg}) catch {};
printStackIterator(dmesg_writer, stack_iter);

serial.print(fmt, .{msg});
printStackIterator(serial.writer, stack_iter);

if (root.tty0) |tty| {
const writer = tty.writer();
writer.print(fmt, .{msg}) catch {};
printStackIterator(writer, stack_iter);
root.term.hideCursor(writer) catch {};
} else {
serial.print(fmt, .{msg});
printStackIterator(serial.writer, stack_iter);
}

arch.halt();
Expand Down
40 changes: 13 additions & 27 deletions kernel/event.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,9 @@ pub const Listener = struct {
// };

pub const Event = struct {
lock: SpinLock,
pending: usize,
listeners: std.ArrayListUnmanaged(Listener),

pub const max_listeners = 32;

pub fn init() !Event {
return .{
.lock = .{},
.pending = 0,
.listeners = try std.ArrayListUnmanaged(Listener).initCapacity(root.allocator, max_listeners),
};
}

pub fn deinit(self: *Event) void {
self.listeners.deinit(root.allocator);
}
lock: SpinLock = .{},
pending: usize = 0,
listeners: std.BoundedArray(Listener, 32) = .{},

pub fn trigger(self: *Event, drop: bool) void {
const old_state = arch.toggleInterrupts(false);
Expand All @@ -47,28 +33,27 @@ pub const Event = struct {
self.lock.lock();
defer self.lock.unlock();

if (self.listeners.items.len == 0) {
if (self.listeners.len == 0) {
if (!drop) {
self.pending += 1;
}
} else {
for (self.listeners.items) |listener| {
for (self.listeners.slice()) |listener| {
const thread = listener.thread;
thread.which_event = listener.which;
sched.enqueue(thread) catch unreachable;
}

self.listeners.clearRetainingCapacity();
self.listeners.len = 0;
}
}
};

pub var int_events: [256]Event = undefined;
pub var int_events = [_]Event{.{}} ** 256;

pub fn init() void {
for (32..0xef) |vec| {
arch.idt.registerHandler(@intCast(vec), intEventHandler);
int_events[vec] = Event.init() catch unreachable;
}
}

Expand All @@ -89,7 +74,7 @@ pub fn awaitEvents(events: []*Event, blocking: bool) ?*Event {

const thread = sched.currentThread();
attachListeners(events, thread);
sched.dequeue(thread); // re-enqueue when?
sched.dequeue(thread); // re-enqueue when? <- Event.trigger
unlockEvents(events);
sched.yieldAwait();

Expand Down Expand Up @@ -120,24 +105,25 @@ fn getFirstPending(events: []*Event) ?*Event {
}

fn attachListeners(events: []*Event, thread: *sched.Thread) void {
thread.attached_events.clearRetainingCapacity();
thread.attached_events.len = 0;

for (events, 0..) |event, i| {
// TODO: replace with append + panic?
event.listeners.appendAssumeCapacity(.{ .thread = thread, .which = i });
thread.attached_events.appendAssumeCapacity(event);
}
}

fn detachListeners(thread: *sched.Thread) void {
for (thread.attached_events.items) |event| {
for (event.listeners.items, 0..) |listener, i| {
for (thread.attached_events.slice()) |event| {
for (event.listeners.slice(), 0..) |listener, i| {
if (listener.thread == thread) {
_ = event.listeners.swapRemove(i); // TODO: weird (loop)
}
}
}

thread.attached_events.clearRetainingCapacity();
thread.attached_events.len = 0;
}

inline fn lockEvents(events: []*Event) void {
Expand Down
8 changes: 4 additions & 4 deletions kernel/lib/term.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ pub const ColorRGB = struct {
try writer.print(csi ++ "48;2;{d};{d};{d}m", .{ bg.r, bg.g, bg.b });
}

pub inline fn setFgStr(comptime str: []const u8) !void {
try setFg(parse(str));
pub inline fn setFgStr(writer: anytype, comptime str: []const u8) !void {
try setFg(writer, parse(str));
}

pub inline fn setBgStr(comptime str: []const u8) !void {
try setBg(parse(str));
pub inline fn setBgStr(writer: anytype, comptime str: []const u8) !void {
try setBg(writer, parse(str));
}

fn parseInt(buf: []const u8) u8 {
Expand Down
6 changes: 3 additions & 3 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ fn main() noreturn {
ps2.init();

const fb = framebuffer_request.response.?.framebuffers()[0];
tty0 = TTY.init(fb.address, fb.width, fb.height, callback) catch unreachable;
tty0 = TTY.init(allocator, fb.address, fb.width, fb.height, callback) catch unreachable;

const boot_info = boot_info_request.response.?;
tty0.?.writer().print("Welcome to Ubik, brought to you by {s} {s} :)\n", .{
try tty0.?.writer().print("Welcome to Ubik, brought to you by {s} {s} :)\n", .{
boot_info.name,
boot_info.version,
}) catch unreachable;
});

arch.disableInterrupts();
std.log.debug("cpu model: {}", .{smp.thisCpu().cpu_model});
Expand Down
6 changes: 1 addition & 5 deletions kernel/sched.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ pub const Thread = struct {
kernel_stack: u64,

which_event: usize,
attached_events: std.ArrayListUnmanaged(*Event),
attached_events: std.BoundedArray(*Event, 32) = .{}, // TODO: use ArrayListUnmanaged?

// running_time: usize, // TODO

pub const max_events = 32;
pub const stack_size = 8 * 1024 * 1024; // 8MiB
pub const stack_pages = stack_size / std.mem.page_size;

Expand All @@ -134,9 +133,7 @@ pub const Thread = struct {
.pf_stack = undefined,
.kernel_stack = undefined,
.which_event = undefined,
.attached_events = try std.ArrayListUnmanaged(*Event).initCapacity(root.allocator, max_events),
};
errdefer thread.attached_events.deinit(root.allocator);

const fpu_storage = try root.allocator.alloc(u8, arch.cpu.fpu_storage_size);
errdefer root.allocator.free(fpu_storage);
Expand Down Expand Up @@ -291,7 +288,6 @@ pub const Thread = struct {
pmm.free(stack, stack_pages);
}
self.stacks.deinit(root.allocator);
self.attached_events.deinit(root.allocator);
const fpu_storage: [*]u8 = @ptrFromInt(self.fpu_storage);
root.allocator.free(fpu_storage[0..arch.cpu.fpu_storage_size]);
root.allocator.destroy(self);
Expand Down
12 changes: 7 additions & 5 deletions kernel/time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ev = @import("event.zig");
const SpinLock = root.SpinLock;
const timespec = root.os.system.timespec;

// TODO: improve, bad_idx is of course ugly
pub const Timer = struct {
idx: usize,
done: bool,
Expand All @@ -21,11 +22,12 @@ pub const Timer = struct {
var timer = try root.allocator.create(Timer);
errdefer root.allocator.destroy(timer);

timer.idx = bad_idx;
timer.when = when;
timer.done = false;
timer.event = try ev.Event.init();
errdefer timer.event.deinit();
timer.* = .{
.idx = bad_idx,
.when = when,
.done = false,
.event = .{},
};

try timer.arm();

Expand Down
2 changes: 1 addition & 1 deletion kernel/vfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub const Node = struct {
refcount: usize, // TODO
lock: SpinLock = .{}, // TODO: use a u64 with flags and lock with atomic OR
// status: i32, // TODO
// event: Event, // TODO
// event: Event = .{}, // TODO
parent: *Node, // undefined for root // TODO
// redirection: ?*Node, // TODO: only for .. and . ?

Expand Down

0 comments on commit e57ce38

Please sign in to comment.