1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-01 00:32:03 +00:00
Ryujinx/Ryujinx.HLE/HOS/Kernel/HleScheduler.cs

149 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Threading;
namespace Ryujinx.HLE.HOS.Kernel
{
partial class KScheduler
{
private const int RoundRobinTimeQuantumMs = 10;
2018-12-01 20:01:59 +00:00
private int _currentCore;
public bool MultiCoreScheduling { get; set; }
public HleCoreManager CoreManager { get; private set; }
2018-12-01 20:01:59 +00:00
private bool _keepPreempting;
public void StartAutoPreemptionThread()
{
2018-12-01 20:01:59 +00:00
Thread preemptionThread = new Thread(PreemptCurrentThread);
2018-12-01 20:01:59 +00:00
_keepPreempting = true;
2018-12-01 20:01:59 +00:00
preemptionThread.Start();
}
public void ContextSwitch()
{
lock (CoreContexts)
{
if (MultiCoreScheduling)
{
2018-12-01 20:01:59 +00:00
int selectedCount = 0;
2018-12-01 20:24:37 +00:00
for (int core = 0; core < CpuCoresCount; core++)
{
2018-12-01 20:01:59 +00:00
KCoreContext coreContext = CoreContexts[core];
2018-12-01 20:01:59 +00:00
if (coreContext.ContextSwitchNeeded && (coreContext.CurrentThread?.Context.IsCurrentThread() ?? false))
{
2018-12-01 20:01:59 +00:00
coreContext.ContextSwitch();
}
2018-12-01 20:01:59 +00:00
if (coreContext.CurrentThread?.Context.IsCurrentThread() ?? false)
{
2018-12-01 20:01:59 +00:00
selectedCount++;
}
}
2018-12-01 20:01:59 +00:00
if (selectedCount == 0)
{
CoreManager.Reset(Thread.CurrentThread);
}
2018-12-01 20:01:59 +00:00
else if (selectedCount == 1)
{
CoreManager.Set(Thread.CurrentThread);
}
else
{
throw new InvalidOperationException("Thread scheduled in more than one core!");
}
}
else
{
2018-12-01 20:01:59 +00:00
KThread currentThread = CoreContexts[_currentCore].CurrentThread;
2018-12-01 20:01:59 +00:00
bool hasThreadExecuting = currentThread != null;
2018-12-01 20:01:59 +00:00
if (hasThreadExecuting)
{
//If this is not the thread that is currently executing, we need
//to request an interrupt to allow safely starting another thread.
2018-12-01 20:01:59 +00:00
if (!currentThread.Context.IsCurrentThread())
{
2018-12-01 20:01:59 +00:00
currentThread.Context.RequestInterrupt();
return;
}
2018-12-01 20:01:59 +00:00
CoreManager.Reset(currentThread.Context.Work);
}
//Advance current core and try picking a thread,
//keep advancing if it is null.
2018-12-01 20:01:59 +00:00
for (int core = 0; core < 4; core++)
{
2018-12-01 20:01:59 +00:00
_currentCore = (_currentCore + 1) % CpuCoresCount;
2018-12-01 20:01:59 +00:00
KCoreContext coreContext = CoreContexts[_currentCore];
2018-12-01 20:01:59 +00:00
coreContext.UpdateCurrentThread();
2018-12-01 20:01:59 +00:00
if (coreContext.CurrentThread != null)
{
2018-12-01 20:01:59 +00:00
coreContext.CurrentThread.ClearExclusive();
2018-12-01 20:01:59 +00:00
CoreManager.Set(coreContext.CurrentThread.Context.Work);
2018-12-01 20:01:59 +00:00
coreContext.CurrentThread.Context.Execute();
break;
}
}
//If nothing was running before, then we are on a "external"
//HLE thread, we don't need to wait.
2018-12-01 20:01:59 +00:00
if (!hasThreadExecuting)
{
return;
}
}
}
CoreManager.Wait(Thread.CurrentThread);
}
private void PreemptCurrentThread()
{
//Preempts current thread every 10 milliseconds on a round-robin fashion,
//when multi core scheduling is disabled, to try ensuring that all threads
//gets a chance to run.
2018-12-01 20:01:59 +00:00
while (_keepPreempting)
{
lock (CoreContexts)
{
2018-12-01 20:01:59 +00:00
KThread currentThread = CoreContexts[_currentCore].CurrentThread;
2018-12-01 20:01:59 +00:00
currentThread?.Context.RequestInterrupt();
}
PreemptThreads();
Thread.Sleep(RoundRobinTimeQuantumMs);
}
}
2018-12-01 20:01:59 +00:00
public void ExitThread(KThread thread)
{
2018-12-01 20:01:59 +00:00
thread.Context.StopExecution();
2018-12-01 20:01:59 +00:00
CoreManager.Exit(thread.Context.Work);
}
2018-12-01 20:01:59 +00:00
public void RemoveThread(KThread thread)
{
2018-12-01 20:01:59 +00:00
CoreManager.RemoveThread(thread.Context.Work);
}
}
}