diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 2dd05353..27498097 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -262,46 +262,40 @@ void Scheduler::reap_task(Task* task) Interrupts::pop(); } for (int i = 0; i < TASK_MAX_FDS; i++) { exiting_task->files[i].close(); } + if (exiting_task->id == (free_tid - 1)) free_tid--; // If we are the last spawned thread, free our PID. delete exiting_task; } +void sched_common_exit(Context* context, int64_t status) +{ + if (sched_current_task->id == 1) sched_current_task->state = sched_current_task->Exited; + else + sched_current_task->state = sched_current_task->Dying; + sched_current_task->exit_status = status; + if (sched_current_task->id != 1) + { + sched_for_each_child(sched_current_task, [](Task* child) { + if (child->state != child->Exited) child->ppid = 1; + return true; + }); + } + else { reboot(); } + Scheduler::task_yield(context); +} + void Scheduler::task_exit(Context* context, int64_t status) { ASSERT(Interrupts::is_in_handler()); kdbgln("exit: task %ld finished running, used %ld ms of cpu time", sched_current_task->id, sched_current_task->cpu_time); - if (sched_current_task->id == 1) sched_current_task->state = sched_current_task->Exited; - else - sched_current_task->state = sched_current_task->Dying; - sched_current_task->exit_status = status; - if (sched_current_task->id != 1) - { - sched_for_each_child(sched_current_task, [](Task* child) { - if (child->state != child->Exited) child->ppid = 1; - return true; - }); - } - else { reboot(); } - task_yield(context); + sched_common_exit(context, status); } void Scheduler::task_misbehave(Context* context, int64_t status) { ASSERT(Interrupts::is_in_handler()); kdbgln("exit: task %ld misbehaved, used %ld ms of cpu time", sched_current_task->id, sched_current_task->cpu_time); - if (sched_current_task->id == 1) sched_current_task->state = sched_current_task->Exited; - else - sched_current_task->state = sched_current_task->Dying; - sched_current_task->exit_status = status; - if (sched_current_task->id != 1) - { - sched_for_each_child(sched_current_task, [](Task* child) { - if (child->state != child->Exited) child->ppid = 1; - return true; - }); - } - else { reboot(); } - task_yield(context); + sched_common_exit(context, status); } void Scheduler::reap_tasks()