Kernel: Replace CriticalScopes with SpinLocks in Process/Thread

This commit is contained in:
Bananymous 2024-03-01 01:31:28 +02:00
parent 51e38b7614
commit 65c4f9db5b
4 changed files with 16 additions and 22 deletions

View File

@ -224,8 +224,8 @@ namespace Kernel
BAN::String m_working_directory; BAN::String m_working_directory;
BAN::Vector<Thread*> m_threads; BAN::Vector<Thread*> m_threads;
vaddr_t m_signal_handlers[_SIGMAX + 1] { }; BAN::Atomic<vaddr_t> m_signal_handlers[_SIGMAX + 1] { };
uint64_t m_signal_pending_mask { 0 }; BAN::Atomic<uint64_t> m_signal_pending_mask { 0 };
BAN::Vector<BAN::String> m_cmdline; BAN::Vector<BAN::String> m_cmdline;
BAN::Vector<BAN::String> m_environ; BAN::Vector<BAN::String> m_environ;

View File

@ -118,6 +118,7 @@ namespace Kernel
uint64_t m_signal_pending_mask { 0 }; uint64_t m_signal_pending_mask { 0 };
uint64_t m_signal_block_mask { 0 }; uint64_t m_signal_block_mask { 0 };
SpinLock m_signal_lock;
static_assert(_SIGMAX < 64); static_assert(_SIGMAX < 64);
#if __enable_sse #if __enable_sse

View File

@ -1,6 +1,5 @@
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <kernel/CriticalScope.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/ProcFS/FileSystem.h> #include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
@ -62,18 +61,13 @@ namespace Kernel
Process* Process::create_process(const Credentials& credentials, pid_t parent, pid_t sid, pid_t pgrp) Process* Process::create_process(const Credentials& credentials, pid_t parent, pid_t sid, pid_t pgrp)
{ {
static pid_t s_next_id = 1; static BAN::Atomic<pid_t> s_next_id = 1;
pid_t pid; pid_t pid = s_next_id++;
{
CriticalScope _;
pid = s_next_id;
if (sid == 0 && pgrp == 0) if (sid == 0 && pgrp == 0)
{ {
sid = s_next_id; sid = pid;
pgrp = s_next_id; pgrp = pid;
}
s_next_id++;
} }
ASSERT(sid > 0); ASSERT(sid > 0);
@ -1484,7 +1478,6 @@ namespace Kernel
TRY(validate_pointer_access((void*)handler, sizeof(handler))); TRY(validate_pointer_access((void*)handler, sizeof(handler)));
} }
CriticalScope _;
m_signal_handlers[signal] = (vaddr_t)handler; m_signal_handlers[signal] = (vaddr_t)handler;
return 0; return 0;
} }
@ -1496,10 +1489,9 @@ namespace Kernel
if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX)) if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX))
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
if (pid == Process::current().pid()) if (pid == m_pid)
{ {
CriticalScope _; m_signal_pending_mask |= 1 << signal;
Process::current().m_signal_pending_mask |= 1 << signal;
return 0; return 0;
} }
@ -1512,9 +1504,8 @@ namespace Kernel
found = true; found = true;
if (signal) if (signal)
{ {
CriticalScope _;
process.m_signal_pending_mask |= 1 << signal; process.m_signal_pending_mask |= 1 << signal;
// FIXME: This is super hacky // FIXME: This feels hacky
Scheduler::get().unblock_thread(process.m_threads.front()->tid()); Scheduler::get().unblock_thread(process.m_threads.front()->tid());
} }
return (pid > 0) ? BAN::Iteration::Break : BAN::Iteration::Continue; return (pid > 0) ? BAN::Iteration::Break : BAN::Iteration::Continue;

View File

@ -29,7 +29,7 @@ namespace Kernel
void Thread::terminate() void Thread::terminate()
{ {
CriticalScope _; set_interrupt_state(InterruptState::Disabled);
m_state = Thread::State::Terminated; m_state = Thread::State::Terminated;
if (this == &Thread::current()) if (this == &Thread::current())
Scheduler::get().execute_current_thread(); Scheduler::get().execute_current_thread();
@ -245,10 +245,11 @@ namespace Kernel
void Thread::handle_signal(int signal) void Thread::handle_signal(int signal)
{ {
ASSERT(!interrupts_enabled());
ASSERT(&Thread::current() == this); ASSERT(&Thread::current() == this);
ASSERT(is_userspace()); ASSERT(is_userspace());
SpinLockGuard _(m_signal_lock);
auto& interrupt_stack = *reinterpret_cast<InterruptStack*>(interrupt_stack_base() + interrupt_stack_size() - sizeof(InterruptStack)); auto& interrupt_stack = *reinterpret_cast<InterruptStack*>(interrupt_stack_base() + interrupt_stack_size() - sizeof(InterruptStack));
ASSERT(GDT::is_user_segment(interrupt_stack.cs)); ASSERT(GDT::is_user_segment(interrupt_stack.cs));
@ -338,7 +339,8 @@ namespace Kernel
bool Thread::add_signal(int signal) bool Thread::add_signal(int signal)
{ {
ASSERT(!interrupts_enabled()); SpinLockGuard _(m_signal_lock);
uint64_t mask = 1ull << signal; uint64_t mask = 1ull << signal;
if (!(m_signal_block_mask & mask)) if (!(m_signal_block_mask & mask))
{ {