Kernel: Scheduler can now terminate processes threads

This commit is contained in:
Bananymous 2023-04-12 17:49:04 +03:00
parent d1b7249803
commit 6be53668b9
3 changed files with 37 additions and 7 deletions

View File

@ -22,6 +22,8 @@ namespace Kernel
void set_current_thread_sleeping(uint64_t);
[[noreturn]] void set_current_thread_done();
[[noreturn]] void set_current_process_done();
void block_current_thread(Semaphore*);
void unblock_threads(Semaphore*);

View File

@ -1,4 +1,5 @@
#include <BAN/StringView.h>
#include <kernel/CriticalScope.h>
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/LockGuard.h>
#include <kernel/Process.h>
@ -54,15 +55,11 @@ namespace Kernel
void Process::exit()
{
{
LockGuard _(m_lock);
for (auto* thread : m_threads)
thread->terminate();
}
while (!m_threads.empty())
PIT::sleep(1);
CriticalScope _;
m_threads.clear();
for (auto& open_fd : m_open_files)
open_fd.inode = nullptr;
Scheduler::get().set_current_process_done();
}
BAN::ErrorOr<void> Process::init_stdio()

View File

@ -229,6 +229,37 @@ namespace Kernel
ASSERT_NOT_REACHED();
}
#define remove_threads(list, condition) \
for (auto it = list.begin(); it != list.end();) \
{ \
if (condition) \
{ \
delete it->thread; \
it = list.remove(it); \
} \
else \
{ \
it++; \
} \
}
void Scheduler::set_current_process_done()
{
VERIFY_CLI();
pid_t pid = m_current_thread->thread->process()->pid();
remove_threads(m_blocking_threads, it->thread->process()->pid() == pid);
remove_threads(m_sleeping_threads, it->thread->process()->pid() == pid);
remove_threads(m_active_threads, it != m_current_thread && it->thread->process()->pid() == pid);
delete m_current_thread->thread;
remove_and_advance_current_thread();
execute_current_thread();
ASSERT_NOT_REACHED();
}
void Scheduler::block_current_thread(Semaphore* semaphore)
{
VERIFY_STI();