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