2018-09-18 16:36:43 -07:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
|
2018-12-17 21:33:36 -08:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
partial class KScheduler
|
|
|
|
{
|
|
|
|
private const int RoundRobinTimeQuantumMs = 10;
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private int _currentCore;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
public bool MultiCoreScheduling { get; set; }
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public HleCoreManager CoreManager { get; private set; }
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private bool _keepPreempting;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-09-23 11:11:46 -07:00
|
|
|
public void StartAutoPreemptionThread()
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Thread preemptionThread = new Thread(PreemptCurrentThread);
|
2018-09-23 11:11:46 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_keepPreempting = true;
|
2018-09-23 11:11:46 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
preemptionThread.Start();
|
2018-09-23 11:11:46 -07:00
|
|
|
}
|
|
|
|
|
2018-09-18 16:36:43 -07:00
|
|
|
public void ContextSwitch()
|
|
|
|
{
|
|
|
|
lock (CoreContexts)
|
|
|
|
{
|
|
|
|
if (MultiCoreScheduling)
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
int selectedCount = 0;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
for (int core = 0; core < CpuCoresCount; core++)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KCoreContext coreContext = CoreContexts[core];
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (coreContext.ContextSwitchNeeded && (coreContext.CurrentThread?.Context.IsCurrentThread() ?? false))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
coreContext.ContextSwitch();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (coreContext.CurrentThread?.Context.IsCurrentThread() ?? false)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
selectedCount++;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (selectedCount == 0)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-11-28 14:18:09 -08:00
|
|
|
CoreManager.Reset(Thread.CurrentThread);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
2018-12-06 03:16:24 -08:00
|
|
|
else if (selectedCount == 1)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
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
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = CoreContexts[_currentCore].CurrentThread;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
bool hasThreadExecuting = currentThread != null;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (hasThreadExecuting)
|
2018-09-18 16:36:43 -07: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-06 03:16:24 -08:00
|
|
|
if (!currentThread.Context.IsCurrentThread())
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread.Context.RequestInterrupt();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -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.
|
2018-12-06 03:16:24 -08:00
|
|
|
for (int core = 0; core < 4; core++)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_currentCore = (_currentCore + 1) % CpuCoresCount;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KCoreContext coreContext = CoreContexts[_currentCore];
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
coreContext.UpdateCurrentThread();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (coreContext.CurrentThread != null)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
coreContext.CurrentThread.ClearExclusive();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
CoreManager.Set(coreContext.CurrentThread.Context.Work);
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
coreContext.CurrentThread.Context.Execute();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//If nothing was running before, then we are on a "external"
|
|
|
|
//HLE thread, we don't need to wait.
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!hasThreadExecuting)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
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.
|
2018-12-06 03:16:24 -08:00
|
|
|
while (_keepPreempting)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
|
|
|
lock (CoreContexts)
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
KThread currentThread = CoreContexts[_currentCore].CurrentThread;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
currentThread?.Context.RequestInterrupt();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PreemptThreads();
|
|
|
|
|
|
|
|
Thread.Sleep(RoundRobinTimeQuantumMs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public void ExitThread(KThread thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
thread.Context.StopExecution();
|
2018-09-18 16:36:43 -07:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
CoreManager.Exit(thread.Context.Work);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public void RemoveThread(KThread thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
CoreManager.RemoveThread(thread.Context.Work);
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|