diff --git a/kobold/hal/alarmqueue.zig b/kobold/hal/alarmqueue.zig index e899b27..0714a64 100644 --- a/kobold/hal/alarmqueue.zig +++ b/kobold/hal/alarmqueue.zig @@ -10,37 +10,32 @@ const Spinlock = @import("perlib").Spinlock; const physmem = @import("root").physmem; pub const AlarmQueueNode = struct { - deadline: u64, + deadline: u64 = 0, data: ?*anyopaque = null, - func: *const fn (?*anyopaque) callconv(.C) void, + func: *const fn (?*anyopaque) callconv(.C) void = undefined, }; -const AlarmQueueList = std.DoublyLinkedList(AlarmQueueNode); +pub const AlarmQueueList = std.DoublyLinkedList(AlarmQueueNode); pub const AlarmQueue = struct { lock: Spinlock = .unaquired, list: AlarmQueueList = .{}, timerCounter: u64 = 0, timerNextInterval: u64 = 0, - pub fn addAlarm(self: *AlarmQueue, timeout: u64, func: *const fn (?*anyopaque) callconv(.C) void, data: ?*anyopaque) *AlarmQueueList.Node { + pub fn addAlarm(self: *AlarmQueue, timeout: u64, aqn: *AlarmQueueList.Node) void { const old = hal.arch.intControl(false); self.lock.acquire(); - const node: *AlarmQueueList.Node = @ptrCast(@alignCast(physmem.Allocate(@sizeOf(AlarmQueueList.Node), @alignOf(AlarmQueueList.Node)).?)); - node.data.deadline = self.timerCounter + timeout; - node.data.data = data; - node.data.func = func; - self.list.append(node); + aqn.data.deadline = self.timerCounter + timeout; + self.list.append(aqn); self.schedule(); self.lock.release(); _ = hal.arch.intControl(old); - return node; } pub fn removeAlarm(self: *AlarmQueue, aqn: *AlarmQueueList.Node) void { const old = hal.arch.intControl(false); self.lock.acquire(); self.list.remove(aqn); - physmem.Free(@intFromPtr(aqn), @sizeOf(AlarmQueueList.Node)); self.schedule(); self.lock.release(); _ = hal.arch.intControl(old); @@ -57,7 +52,6 @@ pub const AlarmQueue = struct { i.data.func(i.data.data); self.list.remove(i); ind = next; - physmem.Free(@intFromPtr(i), @sizeOf(AlarmQueueList.Node)); continue; } else if (i.data.deadline < closestDeadline) { closestDeadline = i.data.deadline; diff --git a/kobold/hal/hart.zig b/kobold/hal/hart.zig index ed3dc78..03e6b97 100644 --- a/kobold/hal/hart.zig +++ b/kobold/hal/hart.zig @@ -28,6 +28,7 @@ pub const HartInfo = struct { activeSyscallStack: usize = 0, trapStack: usize = 0, alarmQueue: alarmqueue.AlarmQueue = .{}, + scheduleNode: alarmqueue.AlarmQueueList.Node = .{ .data = .{} }, archData: ArchData = ArchData{}, activeThread: ?*anyopaque = null, schedulePending: bool = false, diff --git a/kobold/kernel/main.zig b/kobold/kernel/main.zig index febcefe..445638b 100644 --- a/kobold/kernel/main.zig +++ b/kobold/kernel/main.zig @@ -63,5 +63,8 @@ pub export fn KoboldInit() void { } pub fn StartMultitasking() noreturn { + const hart = hal.arch.getHart(); + hart.scheduleNode.data.func = &scheduler.rescheduleAlarm; + hart.scheduleNode.data.data = null; scheduler.Schedule(null); } diff --git a/kobold/kernel/scheduler.zig b/kobold/kernel/scheduler.zig index aae616c..fd96636 100644 --- a/kobold/kernel/scheduler.zig +++ b/kobold/kernel/scheduler.zig @@ -31,7 +31,7 @@ fn GetNextThread() *thread.Thread { } } -fn rescheduleAlarm(data: ?*anyopaque) callconv(.C) void { +pub fn rescheduleAlarm(data: ?*anyopaque) callconv(.C) void { _ = data; hal.arch.getHart().schedulePending = true; } @@ -65,7 +65,7 @@ pub fn Schedule(con: ?*hal.arch.Context) noreturn { } hart.activeThread = @alignCast(@ptrCast(thr)); hart.activeSyscallStack = @intFromPtr(thr.kstack.ptr) + (thr.kstack.len - 8); - _ = hart.alarmQueue.addAlarm(10000, &rescheduleAlarm, null); + hart.alarmQueue.addAlarm(10000, &hart.scheduleNode); thr.fContext.Load(); thr.gpContext.Enter(); }