task auto termination when done

This commit is contained in:
Jack Halford 2020-01-02 17:57:06 +01:00
parent a01e9a5f2a
commit 0307afe365
2 changed files with 13 additions and 10 deletions

View file

@ -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);
}

View file

@ -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 {