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