kernel: fix single-core preemption points

This commit is contained in:
Liam 2022-07-07 12:34:46 -04:00
parent 21945ae127
commit da07e13e07
6 changed files with 27 additions and 39 deletions

View File

@ -144,39 +144,25 @@ void CpuManager::SingleCoreRunIdleThread() {
}
void CpuManager::PreemptSingleCore(bool from_running_environment) {
{
auto& kernel = system.Kernel();
auto& scheduler = kernel.Scheduler(current_core);
auto& kernel = system.Kernel();
Kernel::KThread* current_thread = scheduler.GetSchedulerCurrentThread();
if (idle_count >= 4 || from_running_environment) {
if (!from_running_environment) {
system.CoreTiming().Idle();
idle_count = 0;
}
kernel.SetIsPhantomModeForSingleCore(true);
system.CoreTiming().Advance();
kernel.SetIsPhantomModeForSingleCore(false);
if (idle_count >= 4 || from_running_environment) {
if (!from_running_environment) {
system.CoreTiming().Idle();
idle_count = 0;
}
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES);
system.CoreTiming().ResetTicks();
scheduler.Unload(scheduler.GetSchedulerCurrentThread());
auto& next_scheduler = kernel.Scheduler(current_core);
// Disable dispatch. We're about to preempt this thread.
Kernel::KScopedDisableDispatch dd{kernel};
Common::Fiber::YieldTo(current_thread->GetHostContext(), *next_scheduler.GetSwitchFiber());
kernel.SetIsPhantomModeForSingleCore(true);
system.CoreTiming().Advance();
kernel.SetIsPhantomModeForSingleCore(false);
}
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES);
system.CoreTiming().ResetTicks();
kernel.Scheduler(current_core).PreemptSingleCore();
// We've now been scheduled again, and we may have exchanged schedulers.
// Reload the scheduler in case it's different.
{
auto& scheduler = system.Kernel().Scheduler(current_core);
scheduler.Reload(scheduler.GetSchedulerCurrentThread());
if (!scheduler.IsIdle()) {
idle_count = 0;
}
if (!kernel.Scheduler(current_core).IsIdle()) {
idle_count = 0;
}
}

View File

@ -5,7 +5,6 @@
#include <array>
#include <atomic>
#include <csetjmp>
#include <functional>
#include <memory>
#include <thread>

View File

@ -103,7 +103,20 @@ void KScheduler::ScheduleOnInterrupt() {
GetCurrentThread(kernel).EnableDispatch();
}
void KScheduler::PreemptSingleCore() {
GetCurrentThread(kernel).DisableDispatch();
auto* thread = GetCurrentThreadPointer(kernel);
auto& previous_scheduler = kernel.Scheduler(thread->GetCurrentCore());
previous_scheduler.Unload(thread);
Common::Fiber::YieldTo(thread->GetHostContext(), *m_switch_fiber);
GetCurrentThread(kernel).EnableDispatch();
}
void KScheduler::RescheduleCurrentCore() {
ASSERT(!kernel.IsPhantomModeForSingleCore());
ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() == 1);
GetCurrentThread(kernel).EnableDispatch();

View File

@ -49,6 +49,7 @@ public:
void SetInterruptTaskRunnable();
void RequestScheduleOnInterrupt();
void PreemptSingleCore();
u64 GetIdleCount() {
return m_state.idle_count;
@ -62,10 +63,6 @@ public:
return m_current_thread.load() == m_idle_thread;
}
std::shared_ptr<Common::Fiber> GetSwitchFiber() {
return m_switch_fiber;
}
KThread* GetPreviousThread() const {
return m_state.prev_thread;
}

View File

@ -1204,12 +1204,6 @@ KScopedDisableDispatch::~KScopedDisableDispatch() {
return;
}
// Skip the reschedule if single-core.
if (!Settings::values.use_multi_core.GetValue()) {
GetCurrentThread(kernel).EnableDispatch();
return;
}
if (GetCurrentThread(kernel).GetDisableDispatchCount() <= 1) {
auto scheduler = kernel.CurrentScheduler();

View File

@ -110,7 +110,6 @@ void SetCurrentThread(KernelCore& kernel, KThread* thread);
[[nodiscard]] KThread* GetCurrentThreadPointer(KernelCore& kernel);
[[nodiscard]] KThread& GetCurrentThread(KernelCore& kernel);
[[nodiscard]] s32 GetCurrentCoreId(KernelCore& kernel);
size_t CaptureBacktrace(void** buffer, size_t max);
class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KWorkerTask>,
public boost::intrusive::list_base_hook<> {