sylveos

Toy Operating System
Log | Files | Refs

commit c8b11c0d45fd9d0a3b58e2da319595c2cbef619b
parent ad00709e43bdbe9bac83c192a96839aa47eaadfc
Author: Sylvia Ivory <git@sivory.net>
Date:   Tue, 24 Feb 2026 17:33:35 -0800

Exiting works

Diffstat:
Mpi/thread.zig | 6+++++-
Mprograms/threads.zig | 12++++++++++--
2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/pi/thread.zig b/pi/thread.zig @@ -5,7 +5,7 @@ const switching = @import("./switching.zig"); const PSR = @import("./psr.zig").PSR; const mem = @import("./mem.zig"); -pub var tid_counter: usize = 1; +var tid_counter: usize = 1; var ready: bool = false; var threads: std.DoublyLinkedList = .{}; var scheduler_thread: *Thread = undefined; @@ -111,6 +111,10 @@ pub fn get_tid() usize { return get_current().tid; } +pub fn is_ready() bool { + return ready; +} + fn thread_trampoline(func: *const fn (?*anyopaque) callconv(.c) void, arg: ?*anyopaque) callconv(.c) noreturn { // Won't release as next's CPSR will already reenable // _ = mem.enter_critical_section(); diff --git a/programs/threads.zig b/programs/threads.zig @@ -11,6 +11,8 @@ const mem = pi.mem; fn thread_test(arg: ?*anyopaque) callconv(.c) void { _ = arg; + var count: usize = 0; + while (true) { // Don't use format for simpler function const cs = mem.enter_critical_section(); @@ -19,11 +21,17 @@ fn thread_test(arg: ?*anyopaque) callconv(.c) void { uart.write_byte_sync(@truncate(thread.get_tid() + 48)); uart.write_byte_sync('\n'); + count += 1; + cs.exit(); + + if (count > 1024) break; } } noinline fn tick(regs: interrupts.Registers) void { + if (!thread.is_ready()) return; + uart.print("preempting PC=0x{X}\n", .{regs.pc}); thread.preempt(&regs); } @@ -39,7 +47,7 @@ pub fn main() !void { try thread.fork(thread_test, null); try thread.fork(thread_test, null); - uart.print("Forked threads tid={}\n", .{thread.tid_counter}); + uart.print("Forked threads\n", .{}); timer.set_tick(tick); uart.print("Setting tick\n", .{}); @@ -52,7 +60,7 @@ pub fn main() !void { try thread.join(); - uart.print("This should be unreachable\n", .{}); + uart.print("Done!\n", .{}); while (true) {} }