ps: initial process subsystem

This commit is contained in:
Loïc Grégoire
2026-06-18 18:54:38 -04:00
parent 42b10b886e
commit e50b1e0167
9 changed files with 92 additions and 73 deletions
+6 -29
View File
@@ -3,6 +3,7 @@ const std = @import("std");
const r = @import("root");
const ke = r.ke;
const mm = r.mm;
const ps = r.ps;
const sintab = @import("sintab.zig");
var pixel_buffer: [*]u8 = undefined;
@@ -30,20 +31,8 @@ fn plot_pixel(x: i64, y: i64, color: u32) void {
@as(*u32, @ptrCast(@alignCast(&pixel_buffer[pixel_offset]))).* = color;
}
fn read_tsc() u64 {
var lo: u32 = 0;
var hi: u32 = 0;
asm volatile ("rdtsc"
: [lo] "={eax}" (lo),
[hi] "={edx}" (hi),
);
return (@as(u64, hi) << 32) | @as(u64, lo);
}
fn rand_tsc_based() u32 {
const tsc = read_tsc();
const tsc = ke.time.read_time();
const lo: u32 = @truncate(tsc);
const hi: u32 = @truncate(tsc >> 32);
return lo ^ hi;
@@ -163,26 +152,14 @@ fn particle(param: ?*anyopaque) void {
ke.sched.block();
}
fn make_thread(entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) *ke.Thread {
var ret: *ke.Thread = mm.zone.gpa.create(ke.Thread) catch @panic("oom");
const stack = mm.heap.alloc(r.kib(16)) catch @panic("wtf");
ret.init(@intFromPtr(stack), r.kib(16), entry, arg);
ret.priority = ke.Thread.Priority.default;
ret.base_priority = ke.Thread.Priority.default;
return ret;
}
fn spawn_particle(arg: ?*anyopaque) void {
const thread = make_thread(&particle, arg);
ke.sched.enqueue(thread);
const t = ps.thread.create_kernel(ke.Thread.Priority.default, &particle, arg) catch @panic("OOM");
ke.sched.enqueue(&t.kern);
}
fn spawn_explodeable() void {
const thread = make_thread(&explodeable, null);
ke.sched.enqueue(thread);
const t = ps.thread.create_kernel(ke.Thread.Priority.default, &explodeable, null) catch @panic("OOM");
ke.sched.enqueue(&t.kern);
}
fn explodeable(_: ?*anyopaque) void {
+6 -37
View File
@@ -4,11 +4,10 @@ const pl = r.pl;
const ke = r.ke;
const mm = r.mm;
const ex = r.ex;
const ps = r.ps;
const amd64 = @import("arch");
const turnstile = ke.private.turnstile;
var thread0: ke.Thread = undefined;
var mtx: ke.private.mutex.Mutex = .init();
var shared_counter: usize = 0;
@@ -20,48 +19,18 @@ fn mutex_test(_: ?*anyopaque) void {
}
}
fn make_thread(entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) *ke.Thread {
var ret: *ke.Thread = mm.zone.gpa.create(ke.Thread) catch @panic("oom");
const stack = mm.heap.alloc(r.kib(64)) catch @panic("wtf");
std.log.info("Stack = {*}", .{stack});
ret.init(@intFromPtr(stack), r.kib(64), entry, arg);
ret.priority = ke.Thread.Priority.default;
ret.base_priority = ret.priority;
ret.turnstile = mm.zone.gpa.create(ke.private.turnstile.Turnstile) catch @panic("oom");
ret.turnstile.* = .{
.link = undefined,
.next_free = null,
.obj = undefined,
.owner = null,
.boost_link = undefined,
.donated = null,
.queues = undefined,
.waiters = 0,
};
ret.turnstile.queues[0].init();
ret.turnstile.queues[1].init();
ret.turnstile_waiter = null;
return ret;
}
pub fn init(boot_info: *pl.BootInfo) void {
mm.init(boot_info);
pl.late_init(boot_info);
mm.late_init();
ke.sched.late_init();
ke.private.turnstile.init_turnstiles();
ps.init();
if (false) {
if (boot_info.framebuffer != null) {
const sp = mm.heap.alloc(r.kib(16)) catch @panic("oom");
thread0.init(@intFromPtr(sp), r.kib(16), ex.fireworks.start, boot_info);
ke.sched.enqueue(&thread0);
const t = ps.thread.create_kernel(ke.Thread.Priority.default, ex.fireworks.start, boot_info) catch @panic("oom");
ke.sched.enqueue(&t.kern);
}
while (true) {}
@@ -70,8 +39,8 @@ pub fn init(boot_info: *pl.BootInfo) void {
const ipl = ke.ipl.raise(.Dispatch);
for (0..10) |_| {
const t = make_thread(mutex_test, null);
ke.sched.enqueue(t);
const t = ps.thread.create_kernel(ke.Thread.Priority.default, mutex_test, null) catch @panic("oom");
ke.sched.enqueue(&t.kern);
}
ke.ipl.lower(ipl);