From 0307afe365ff7e2a36d36a76ddcce7e377f84b7d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 2 Jan 2020 17:57:06 +0100 Subject: [PATCH] task auto termination when done --- src/console.zig | 8 ++++++-- src/task.zig | 15 +++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/console.zig b/src/console.zig index 9032378..8d45db0 100644 --- a/src/console.zig +++ b/src/console.zig @@ -12,12 +12,16 @@ fn sleep_for_2() void { fn execute(input: []u8) void { const eql = std.mem.eql; if (eql(u8, input, "clear")) return vga.clear(); - if (eql(u8, input, "x86paging")) return x86.paging.format(); - if (eql(u8, input, "x86memory")) return x86.pmem.format(); + if (eql(u8, input, "paging")) return x86.paging.format(); + if (eql(u8, input, "memory")) return x86.pmem.format(); if (eql(u8, input, "tasks")) return task.format(); if (eql(u8, input, "tasks")) return task.format_short(); if (eql(u8, input, "lspci")) return pci.lspci(); if (eql(u8, input, "sleep2")) return sleep_for_2(); + if (eql(u8, input, "t-sleep2")) { + _ = task.new(@ptrToInt(sleep_for_2)) catch unreachable; + return; + } if (eql(u8, input, "uptime")) return time.uptime(); println("{}: command not found", input); } diff --git a/src/task.zig b/src/task.zig index 92a1580..4355a68 100644 --- a/src/task.zig +++ b/src/task.zig @@ -54,6 +54,10 @@ pub const Task = struct { // allocate a new stack t.esp = (try vmem.malloc(STACK_SIZE)) + STACK_SIZE; + // if the tasks rets from its main function, it will go to terminate + // NOTE: if terminate is called this way it has an incorrect ebp! + t.esp -= 4; + @intToPtr(*usize, t.esp).* = @ptrToInt(task.terminate); // this will be what ret goes to t.esp -= 4; @intToPtr(*usize, t.esp).* = entrypoint; @@ -153,23 +157,18 @@ pub fn unblock(node: *TaskNode) void { ready_tasks.append(node); } -pub fn terminate() void { +pub fn terminate() noreturn { assert(current_task.data.state == .Running); - lock_scheduler(); + lock_scheduler(); current_task.data.state = .Terminated; terminated_tasks.append(current_task); - - // Block this task (note: task switch will be postponed until scheduler lock is released) - - // Make sure the cleaner task isn't paused unblock(cleaner_task); unlock_scheduler(); preempt(); - println("Terminated task was revived, what the fuck?"); - x86.hang(); + unreachable; } pub fn cleaner_loop() noreturn {