| 10 | #include "task_messages.hpp" |
| 11 | |
| 12 | auto TaskManager::Exit(int exit_code) -> void { |
| 13 | auto& cpu_sched = GetCurrentCpuSched(); |
| 14 | auto* current = GetCurrentTask(); |
| 15 | assert(current != nullptr && "Exit: No current task to exit"); |
| 16 | assert(current->GetStatus() == TaskStatus::kRunning && |
| 17 | "Exit: current task status must be kRunning"); |
| 18 | |
| 19 | ResourceId wait_resource_id{}; |
| 20 | bool should_wake_parent = false; |
| 21 | |
| 22 | { |
| 23 | LockGuard<SpinLock> lock_guard(cpu_sched.lock); |
| 24 | |
| 25 | current->aux->exit_code = exit_code; |
| 26 | bool is_group_leader = current->IsThreadGroupLeader(); |
| 27 | |
| 28 | if (is_group_leader && current->GetThreadGroupSize() > 1) { |
| 29 | klog::Warn( |
| 30 | "Exit: Thread group leader (pid={}, tgid={}) exiting, but group " |
| 31 | "still has {} threads", |
| 32 | current->pid, current->aux->tgid, current->GetThreadGroupSize()); |
| 33 | } |
| 34 | |
| 35 | current->LeaveThreadGroup(); |
| 36 | |
| 37 | if (current->aux->parent_pid != 0) { |
| 38 | current->fsm.Receive(MsgExit{exit_code, true}); |
| 39 | wait_resource_id = |
| 40 | ResourceId(ResourceType::kChildExit, current->aux->parent_pid); |
| 41 | should_wake_parent = true; |
| 42 | |
| 43 | klog::Debug("Exit: pid={} entering zombie, will wake parent={}", |
| 44 | current->pid, current->aux->parent_pid); |
| 45 | } else { |
| 46 | current->fsm.Receive(MsgExit{exit_code, false}); |
| 47 | } |
| 48 | |
| 49 | if (is_group_leader) { |
| 50 | ReparentChildren(current); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (should_wake_parent) { |
| 55 | Wakeup(wait_resource_id); |
| 56 | } |
| 57 | |
| 58 | Schedule(); |
| 59 | |
| 60 | // UNREACHABLE |
| 61 | __builtin_unreachable(); |
| 62 | } |
no test coverage detected