2023-01-05 22:39:56 +01:00
|
|
|
#include "sys/Syscall.h"
|
|
|
|
#include "thread/Scheduler.h"
|
|
|
|
|
2023-03-23 22:25:56 +01:00
|
|
|
Result<u64> sys_exit(Registers*, SyscallArgs args)
|
2023-01-05 22:39:56 +01:00
|
|
|
{
|
2023-03-23 22:25:56 +01:00
|
|
|
u8 status = (u8)args[0];
|
|
|
|
|
|
|
|
Thread* current = Scheduler::current();
|
|
|
|
|
2023-04-28 15:19:01 +02:00
|
|
|
Scheduler::for_each_child(current, [](Thread* child) {
|
2023-05-04 22:58:04 +02:00
|
|
|
child->parent = Scheduler::init_thread();
|
2023-04-28 15:13:53 +02:00
|
|
|
return true;
|
|
|
|
});
|
2023-03-24 17:37:04 +01:00
|
|
|
|
2023-05-04 23:03:31 +02:00
|
|
|
auto* parent = current->parent;
|
|
|
|
if (parent && parent->state == ThreadState::Waiting)
|
|
|
|
{
|
|
|
|
auto child = *parent->child_being_waited_for;
|
|
|
|
if (child == -1 || child == (pid_t)current->id)
|
|
|
|
{
|
|
|
|
parent->child_being_waited_for = (pid_t)current->id;
|
|
|
|
parent->state = ThreadState::Runnable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:25:56 +01:00
|
|
|
current->status = status;
|
|
|
|
current->state = ThreadState::Exited;
|
|
|
|
|
|
|
|
kernel_yield();
|
2023-03-23 22:42:24 +01:00
|
|
|
unreachable();
|
2023-01-05 22:39:56 +01:00
|
|
|
}
|