2018-09-18 16:36:43 -07:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
partial class KScheduler
|
|
|
|
{
|
|
|
|
private const int RoundRobinTimeQuantumMs = 10;
|
|
|
|
|
|
|
|
private int CurrentCore;
|
|
|
|
|
|
|
|
public bool MultiCoreScheduling { get; set; }
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
public HleCoreManager CoreManager { get; private set; }
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
private bool KeepPreempting;
|
|
|
|
|
2018-09-23 11:11:46 -07:00
|
|
|
public void StartAutoPreemptionThread()
|
|
|
|
{
|
|
|
|
Thread PreemptionThread = new Thread(PreemptCurrentThread);
|
|
|
|
|
|
|
|
KeepPreempting = true;
|
|
|
|
|
|
|
|
PreemptionThread.Start();
|
|
|
|
}
|
|
|
|
|
2018-09-18 16:36:43 -07:00
|
|
|
public void ContextSwitch()
|
|
|
|
{
|
|
|
|
lock (CoreContexts)
|
|
|
|
{
|
|
|
|
if (MultiCoreScheduling)
|
|
|
|
{
|
|
|
|
int SelectedCount = 0;
|
|
|
|
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
KCoreContext CoreContext = CoreContexts[Core];
|
|
|
|
|
|
|
|
if (CoreContext.ContextSwitchNeeded && (CoreContext.CurrentThread?.Context.IsCurrentThread() ?? false))
|
|
|
|
{
|
|
|
|
CoreContext.ContextSwitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CoreContext.CurrentThread?.Context.IsCurrentThread() ?? false)
|
|
|
|
{
|
|
|
|
SelectedCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SelectedCount == 0)
|
|
|
|
{
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Reset(Thread.CurrentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
else if (SelectedCount == 1)
|
|
|
|
{
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Set(Thread.CurrentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Thread scheduled in more than one core!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KThread CurrentThread = CoreContexts[CurrentCore].CurrentThread;
|
|
|
|
|
|
|
|
bool HasThreadExecuting = CurrentThread != null;
|
|
|
|
|
|
|
|
if (HasThreadExecuting)
|
|
|
|
{
|
|
|
|
//If this is not the thread that is currently executing, we need
|
|
|
|
//to request an interrupt to allow safely starting another thread.
|
|
|
|
if (!CurrentThread.Context.IsCurrentThread())
|
|
|
|
{
|
|
|
|
CurrentThread.Context.RequestInterrupt();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Reset(CurrentThread.Context.Work);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//Advance current core and try picking a thread,
|
|
|
|
//keep advancing if it is null.
|
|
|
|
for (int Core = 0; Core < 4; Core++)
|
|
|
|
{
|
|
|
|
CurrentCore = (CurrentCore + 1) % CpuCoresCount;
|
|
|
|
|
|
|
|
KCoreContext CoreContext = CoreContexts[CurrentCore];
|
|
|
|
|
|
|
|
CoreContext.UpdateCurrentThread();
|
|
|
|
|
|
|
|
if (CoreContext.CurrentThread != null)
|
|
|
|
{
|
|
|
|
CoreContext.CurrentThread.ClearExclusive();
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Set(CoreContext.CurrentThread.Context.Work);
|
2018-09-18 16:36:43 -07: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.
|
|
|
|
if (!HasThreadExecuting)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Wait(Thread.CurrentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
while (KeepPreempting)
|
|
|
|
{
|
|
|
|
lock (CoreContexts)
|
|
|
|
{
|
|
|
|
KThread CurrentThread = CoreContexts[CurrentCore].CurrentThread;
|
|
|
|
|
|
|
|
CurrentThread?.Context.RequestInterrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreemptThreads();
|
|
|
|
|
|
|
|
Thread.Sleep(RoundRobinTimeQuantumMs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
public void ExitThread(KThread Thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
Thread.Context.StopExecution();
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Exit(Thread.Context.Work);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveThread(KThread Thread)
|
|
|
|
{
|
|
|
|
CoreManager.RemoveThread(Thread.Context.Work);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|