From 60ad6a6ab5d75af15b3b27c1725a46b9bf17d3d8 Mon Sep 17 00:00:00 2001 From: TalonFloof <105325988+TalonFloof@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:15:57 -0600 Subject: [PATCH] jytjytjty Signed-off-by: TalonFloof <105325988+TalonFloof@users.noreply.github.com> --- kobold/hal/alarmqueue.zig | 23 +++++++++++++++-------- kobold/hal/hart.zig | 4 ++-- kobold/hal/x86_64/timer.zig | 4 ++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/kobold/hal/alarmqueue.zig b/kobold/hal/alarmqueue.zig index 915cc33..ffe30ec 100644 --- a/kobold/hal/alarmqueue.zig +++ b/kobold/hal/alarmqueue.zig @@ -12,27 +12,34 @@ const physmem = @import("root").physmem; pub const AlarmQueueNode = struct { deadline: u64, data: ?*anyopaque = null, - func: *fn(?*anyopaque) callconv(.C) void, + func: *fn (?*anyopaque) callconv(.C) void, }; const AlarmQueueList = std.DoublyLinkedList(AlarmQueueNode); pub const AlarmQueue = struct { lock: Spinlock = .unaquired, list: AlarmQueueList = .{}, + timerCounter: u64 = 0, + timerNextInterval: u64 = 0, - pub fn addAlarm(timeout: u64, func: *fn(?*anyopaque) callconv(.C) void, data: ?*anyopaque) void { - const node = @ptrCast(@alignCast(physmem.Allocate(@sizeOf(AlarmQueueList.Node),@alignOf(AlarmQueueList.node)).?)); + pub fn addAlarm(self: *AlarmQueue, timeout: u64, func: *fn (?*anyopaque) callconv(.C) void, data: ?*anyopaque) 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 = hal.arch.getHart().timerCounter + timeout; node.data.data = data; node.data.func = func; + self.schedule(); + self.lock.release(); + _ = hal.arch.intControl(old); } - fn recalibrate() void { + fn schedule(self: *AlarmQueue) void { const elapsedTime = hal.arch.getHart().timerNextInterval - hal.arch.getRemainingTime(); - var ind = list.first; - while(ind != null) { - + var closestDeadline: u64 = 0xffff_ffff_ffff_ffff; + var ind = self.list.first; + while (ind != null) { ind = ind.?.next; } } -}; \ No newline at end of file +}; diff --git a/kobold/hal/hart.zig b/kobold/hal/hart.zig index 2731d3d..42c612c 100644 --- a/kobold/hal/hart.zig +++ b/kobold/hal/hart.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const alarmqueue = @import("alarmqueue.zig"); pub const HartInfo = struct { pub const ArchData: type = switch (builtin.cpu.arch) { @@ -25,8 +26,7 @@ pub const HartInfo = struct { activeContextStack: usize = 0, activeSyscallStack: usize = 0, trapStack: usize = 0, - timerCounter: u64 = 0, // In microseconds - timerNextInterval: u64 = 0, // The inital set time of the timer, in microseconds + alarmQueue: alarmqueue.AlarmQueue = .{}, archData: ArchData = ArchData{}, hartID: usize = 0, diff --git a/kobold/hal/x86_64/timer.zig b/kobold/hal/x86_64/timer.zig index f04a6d8..623a07e 100644 --- a/kobold/hal/x86_64/timer.zig +++ b/kobold/hal/x86_64/timer.zig @@ -81,5 +81,5 @@ pub fn setDeadline(microsecs: usize) void { pub fn getRemainingUs() usize { const count = 0xffffffff - apic.read(0x390); - returns @intFromFloat(@as(f64, @floatFromInt(count)) * (@as(f64, @floatFromInt(ticksPerSecond)) / 1000000.0)); -} \ No newline at end of file + return @intFromFloat(@as(f64, @floatFromInt(count)) * (@as(f64, @floatFromInt(ticksPerSecond)) / 1000000.0)); +}