Skip to content

Commit

Permalink
rbewrbwe
Browse files Browse the repository at this point in the history
Signed-off-by: TalonFloof <[email protected]>
  • Loading branch information
TalonFloof committed Nov 18, 2024
1 parent c0ef2f7 commit 655e3e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
18 changes: 6 additions & 12 deletions kobold/hal/alarmqueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions kobold/hal/hart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions kobold/kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions kobold/kernel/scheduler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}

0 comments on commit 655e3e6

Please sign in to comment.