Kernel: Allow initial binding of threads to specific processor

This commit is contained in:
Bananymous 2024-09-24 16:27:40 +03:00
parent 348d04f7f5
commit f0e55938c1
2 changed files with 22 additions and 8 deletions

View File

@ -82,6 +82,8 @@ namespace Kernel
void timer_interrupt(); void timer_interrupt();
static BAN::ErrorOr<void> bind_thread_to_processor(Thread*, ProcessorID);
// if thread is already bound, this will never fail
BAN::ErrorOr<void> add_thread(Thread*); BAN::ErrorOr<void> add_thread(Thread*);
void block_current_thread(ThreadBlocker* thread_blocker, uint64_t wake_time_ns); void block_current_thread(ThreadBlocker* thread_blocker, uint64_t wake_time_ns);

View File

@ -567,25 +567,37 @@ namespace Kernel
} }
#endif #endif
BAN::ErrorOr<void> Scheduler::add_thread(Thread* thread) BAN::ErrorOr<void> Scheduler::bind_thread_to_processor(Thread* thread, ProcessorID processor_id)
{ {
ASSERT(thread->m_scheduler_node == nullptr);
auto* new_node = new SchedulerQueue::Node(thread); auto* new_node = new SchedulerQueue::Node(thread);
if (new_node == nullptr) if (new_node == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
const size_t processor_index = s_next_processor_index++ % Processor::count(); ASSERT(processor_id != PROCESSOR_NONE);
const auto processor_id = Processor::id_from_index(processor_index);
new_node->processor_id = processor_id; new_node->processor_id = processor_id;
thread->m_scheduler_node = new_node; thread->m_scheduler_node = new_node;
if (processor_id == Processor::current_id()) return {};
add_thread(new_node); }
BAN::ErrorOr<void> Scheduler::add_thread(Thread* thread)
{
if (thread->m_scheduler_node == nullptr)
{
const size_t processor_index = s_next_processor_index++ % Processor::count();
const auto processor_id = Processor::id_from_index(processor_index);
TRY(bind_thread_to_processor(thread, processor_id));
}
auto* node = thread->m_scheduler_node;
if (node->processor_id == Processor::current_id())
add_thread(node);
else else
{ {
Processor::send_smp_message(processor_id, { Processor::send_smp_message(node->processor_id, {
.type = Processor::SMPMessage::Type::NewThread, .type = Processor::SMPMessage::Type::NewThread,
.new_thread = new_node .new_thread = node
}); });
} }