mirror of
https://git.h3cjp.net/H3cJP/yuzu.git
synced 2024-11-14 13:42:56 +00:00
kernel/thread: Deduplicate scheduler switching code
The code in both places was the same verbatim, so we can extract it to a function to deduplicate the logic.
This commit is contained in:
parent
65bd03d74c
commit
f9db75fe40
|
@ -142,36 +142,7 @@ void Thread::ResumeFromWait() {
|
||||||
|
|
||||||
status = ThreadStatus::Ready;
|
status = ThreadStatus::Ready;
|
||||||
|
|
||||||
std::optional<s32> new_processor_id = GetNextProcessorId(affinity_mask);
|
ChangeScheduler();
|
||||||
if (!new_processor_id) {
|
|
||||||
new_processor_id = processor_id;
|
|
||||||
}
|
|
||||||
if (ideal_core != -1 &&
|
|
||||||
Core::System::GetInstance().Scheduler(ideal_core).GetCurrentThread() == nullptr) {
|
|
||||||
new_processor_id = ideal_core;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASSERT(*new_processor_id < 4);
|
|
||||||
|
|
||||||
// Add thread to new core's scheduler
|
|
||||||
auto* next_scheduler = &Core::System::GetInstance().Scheduler(*new_processor_id);
|
|
||||||
|
|
||||||
if (*new_processor_id != processor_id) {
|
|
||||||
// Remove thread from previous core's scheduler
|
|
||||||
scheduler->RemoveThread(this);
|
|
||||||
next_scheduler->AddThread(this, current_priority);
|
|
||||||
}
|
|
||||||
|
|
||||||
processor_id = *new_processor_id;
|
|
||||||
|
|
||||||
// If the thread was ready, unschedule from the previous core and schedule on the new core
|
|
||||||
scheduler->UnscheduleThread(this, current_priority);
|
|
||||||
next_scheduler->ScheduleThread(this, current_priority);
|
|
||||||
|
|
||||||
// Change thread's scheduler
|
|
||||||
scheduler = next_scheduler;
|
|
||||||
|
|
||||||
Core::System::GetInstance().CpuCore(processor_id).PrepareReschedule();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -364,42 +335,45 @@ void Thread::UpdatePriority() {
|
||||||
void Thread::ChangeCore(u32 core, u64 mask) {
|
void Thread::ChangeCore(u32 core, u64 mask) {
|
||||||
ideal_core = core;
|
ideal_core = core;
|
||||||
affinity_mask = mask;
|
affinity_mask = mask;
|
||||||
|
ChangeScheduler();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Thread::ChangeScheduler() {
|
||||||
if (status != ThreadStatus::Ready) {
|
if (status != ThreadStatus::Ready) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto& system = Core::System::GetInstance();
|
||||||
std::optional<s32> new_processor_id{GetNextProcessorId(affinity_mask)};
|
std::optional<s32> new_processor_id{GetNextProcessorId(affinity_mask)};
|
||||||
|
|
||||||
if (!new_processor_id) {
|
if (!new_processor_id) {
|
||||||
new_processor_id = processor_id;
|
new_processor_id = processor_id;
|
||||||
}
|
}
|
||||||
if (ideal_core != -1 &&
|
if (ideal_core != -1 && system.Scheduler(ideal_core).GetCurrentThread() == nullptr) {
|
||||||
Core::System::GetInstance().Scheduler(ideal_core).GetCurrentThread() == nullptr) {
|
|
||||||
new_processor_id = ideal_core;
|
new_processor_id = ideal_core;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSERT(*new_processor_id < 4);
|
ASSERT(*new_processor_id < 4);
|
||||||
|
|
||||||
// Add thread to new core's scheduler
|
// Add thread to new core's scheduler
|
||||||
auto* next_scheduler = &Core::System::GetInstance().Scheduler(*new_processor_id);
|
auto& next_scheduler = system.Scheduler(*new_processor_id);
|
||||||
|
|
||||||
if (*new_processor_id != processor_id) {
|
if (*new_processor_id != processor_id) {
|
||||||
// Remove thread from previous core's scheduler
|
// Remove thread from previous core's scheduler
|
||||||
scheduler->RemoveThread(this);
|
scheduler->RemoveThread(this);
|
||||||
next_scheduler->AddThread(this, current_priority);
|
next_scheduler.AddThread(this, current_priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
processor_id = *new_processor_id;
|
processor_id = *new_processor_id;
|
||||||
|
|
||||||
// If the thread was ready, unschedule from the previous core and schedule on the new core
|
// If the thread was ready, unschedule from the previous core and schedule on the new core
|
||||||
scheduler->UnscheduleThread(this, current_priority);
|
scheduler->UnscheduleThread(this, current_priority);
|
||||||
next_scheduler->ScheduleThread(this, current_priority);
|
next_scheduler.ScheduleThread(this, current_priority);
|
||||||
|
|
||||||
// Change thread's scheduler
|
// Change thread's scheduler
|
||||||
scheduler = next_scheduler;
|
scheduler = &next_scheduler;
|
||||||
|
|
||||||
Core::System::GetInstance().CpuCore(processor_id).PrepareReschedule();
|
system.CpuCore(processor_id).PrepareReschedule();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Thread::AllWaitObjectsReady() {
|
bool Thread::AllWaitObjectsReady() {
|
||||||
|
|
|
@ -374,6 +374,8 @@ private:
|
||||||
explicit Thread(KernelCore& kernel);
|
explicit Thread(KernelCore& kernel);
|
||||||
~Thread() override;
|
~Thread() override;
|
||||||
|
|
||||||
|
void ChangeScheduler();
|
||||||
|
|
||||||
Core::ARM_Interface::ThreadContext context{};
|
Core::ARM_Interface::ThreadContext context{};
|
||||||
|
|
||||||
u32 thread_id = 0;
|
u32 thread_id = 0;
|
||||||
|
|
Loading…
Reference in a new issue