Kernel: Threads can now be terminated mid execution

This commit is contained in:
Bananymous
2023-04-09 22:18:12 +03:00
parent df6e8a6562
commit f924ac9265
6 changed files with 48 additions and 19 deletions

View File

@@ -23,7 +23,12 @@ namespace Kernel
, m_tty(TTY::current())
{ }
BAN::ErrorOr<void> Process::add_thread(entry_t entry, void* data)
Process::~Process()
{
exit();
}
BAN::ErrorOr<Thread*> Process::add_thread(entry_t entry, void* data)
{
Thread* thread = TRY(Thread::create(entry, data, this));
@@ -32,10 +37,10 @@ namespace Kernel
if (auto res = Scheduler::get().add_thread(thread); res.is_error())
{
m_threads.pop_back();
return res;
return res.release_error();
}
return {};
return thread;
}
void Process::on_thread_exit(Thread& thread)
@@ -46,6 +51,17 @@ namespace Kernel
m_threads.remove(i);
}
void Process::exit()
{
LockGuard _(m_lock);
for (auto* thread : m_threads)
thread->terminate();
while (!m_threads.empty())
PIT::sleep(1);
for (auto& open_fd : m_open_files)
open_fd.inode = nullptr;
}
BAN::ErrorOr<void> Process::init_stdio()
{
if (!m_open_files.empty())

View File

@@ -16,8 +16,8 @@
namespace Kernel
{
extern "C" void start_thread(uintptr_t rsp, uintptr_t rip);
extern "C" void continue_thread(uintptr_t rsp, uintptr_t rip);
extern "C" [[noreturn]] void start_thread(uintptr_t rsp, uintptr_t rip);
extern "C" [[noreturn]] void continue_thread(uintptr_t rsp, uintptr_t rip);
extern "C" uintptr_t read_rip();
static Scheduler* s_instance = nullptr;
@@ -166,14 +166,17 @@ namespace Kernel
Thread& current = current_thread();
if (current.started())
switch (current.state())
{
continue_thread(current.rsp(), current.rip());
}
else
{
current.set_started();
start_thread(current.rsp(), current.rip());
case Thread::State::NotStarted:
current.set_started();
start_thread(current.rsp(), current.rip());
case Thread::State::Executing:
continue_thread(current.rsp(), current.rip());
case Thread::State::Terminating:
ENABLE_INTERRUPTS();
current.on_exit();
ASSERT_NOT_REACHED();
}
ASSERT_NOT_REACHED();