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