Ryujinx/Ryujinx.Core/OsHle/Handles/KProcessScheduler.cs

342 lines
9.3 KiB
C#
Raw Normal View History

2018-04-24 20:57:39 +02:00
using Ryujinx.Core.Logging;
using System;
2018-02-19 20:37:13 +01:00
using System.Collections.Concurrent;
using System.Threading;
namespace Ryujinx.Core.OsHle.Handles
{
class KProcessScheduler : IDisposable
{
private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
private ThreadQueue WaitingToRun;
private KThread[] CoreThreads;
private object SchedLock;
2018-04-24 20:57:39 +02:00
private Logger Log;
public KProcessScheduler(Logger Log)
{
2018-04-24 20:57:39 +02:00
this.Log = Log;
AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
WaitingToRun = new ThreadQueue();
CoreThreads = new KThread[4];
SchedLock = new object();
}
public void StartThread(KThread Thread)
{
lock (SchedLock)
{
2018-02-19 20:37:13 +01:00
SchedulerThread SchedThread = new SchedulerThread(Thread);
if (!AllThreads.TryAdd(Thread, SchedThread))
{
return;
}
if (TryAddToCore(Thread))
{
Thread.Thread.Execute();
PrintDbgThreadInfo(Thread, "running.");
}
else
{
WaitingToRun.Push(SchedThread);
PrintDbgThreadInfo(Thread, "waiting to run.");
}
}
}
public void RemoveThread(KThread Thread)
{
PrintDbgThreadInfo(Thread, "exited.");
2018-02-19 20:37:13 +01:00
lock (SchedLock)
{
2018-04-19 05:19:22 +02:00
if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
{
WaitingToRun.Remove(SchedThread);
SchedThread.Dispose();
2018-04-19 05:19:22 +02:00
}
int ActualCore = Thread.ActualCore;
SchedulerThread NewThread = WaitingToRun.Pop(ActualCore);
if (NewThread == null)
2018-02-19 20:37:13 +01:00
{
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {ActualCore}!");
CoreThreads[ActualCore] = null;
2018-02-19 20:37:13 +01:00
return;
}
NewThread.Thread.ActualCore = ActualCore;
RunThread(NewThread);
}
}
public void SetThreadActivity(KThread Thread, bool Active)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
SchedThread.IsActive = Active;
if (Active)
{
SchedThread.WaitActivity.Set();
}
else
{
SchedThread.WaitActivity.Reset();
}
}
public void EnterWait(KThread Thread, int TimeoutMs = Timeout.Infinite)
{
SchedulerThread SchedThread = AllThreads[Thread];
Suspend(Thread);
SchedThread.WaitSync.WaitOne(TimeoutMs);
TryResumingExecution(SchedThread);
}
public void WakeUp(KThread Thread)
{
AllThreads[Thread].WaitSync.Set();
}
public void TryToRun(KThread Thread)
{
lock (SchedLock)
{
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
if (WaitingToRun.HasThread(SchedThread) && TryAddToCore(Thread))
{
RunThread(SchedThread);
}
else
{
SetReschedule(Thread.ProcessorId);
}
}
}
}
public void Suspend(KThread Thread)
{
lock (SchedLock)
{
PrintDbgThreadInfo(Thread, "suspended.");
AllThreads[Thread].NeedsReschedule = false;
int ActualCore = Thread.ActualCore;
SchedulerThread SchedThread = WaitingToRun.Pop(ActualCore);
2018-02-19 20:37:13 +01:00
if (SchedThread != null)
{
SchedThread.Thread.ActualCore = ActualCore;
CoreThreads[ActualCore] = SchedThread.Thread;
2018-02-19 20:37:13 +01:00
RunThread(SchedThread);
}
2018-02-19 20:37:13 +01:00
else
{
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
2018-02-19 20:37:13 +01:00
CoreThreads[ActualCore] = null;
}
}
}
public void SetReschedule(int Core)
{
lock (SchedLock)
{
KThread Thread = CoreThreads[Core];
if (Thread != null && AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
SchedThread.NeedsReschedule = true;
}
}
}
public void Reschedule(KThread Thread)
{
SchedulerThread SchedThread = AllThreads[Thread];
2018-02-19 20:37:13 +01:00
bool NeedsReschedule;
lock (SchedLock)
{
NeedsReschedule = SchedThread.NeedsReschedule;
SchedThread.NeedsReschedule = false;
}
if (NeedsReschedule)
{
PrintDbgThreadInfo(Thread, "yielded execution.");
lock (SchedLock)
{
int ActualCore = Thread.ActualCore;
SchedulerThread NewThread = WaitingToRun.Pop(ActualCore, Thread.ActualPriority);
if (NewThread == null)
{
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
return;
}
NewThread.Thread.ActualCore = ActualCore;
CoreThreads[ActualCore] = NewThread.Thread;
RunThread(NewThread);
}
TryResumingExecution(SchedThread);
}
}
public void Resume(KThread Thread)
{
TryResumingExecution(AllThreads[Thread]);
}
private void TryResumingExecution(SchedulerThread SchedThread)
{
KThread Thread = SchedThread.Thread;
2018-02-19 20:37:13 +01:00
PrintDbgThreadInfo(Thread, "trying to resume...");
SchedThread.WaitActivity.WaitOne();
lock (SchedLock)
{
if (TryAddToCore(Thread))
{
PrintDbgThreadInfo(Thread, "resuming execution...");
return;
}
WaitingToRun.Push(SchedThread);
SetReschedule(Thread.ProcessorId);
PrintDbgThreadInfo(Thread, "entering wait state...");
2018-02-19 20:37:13 +01:00
}
SchedThread.WaitSched.WaitOne();
PrintDbgThreadInfo(Thread, "resuming execution...");
}
2018-02-19 20:37:13 +01:00
private void RunThread(SchedulerThread SchedThread)
{
2018-02-19 20:37:13 +01:00
if (!SchedThread.Thread.Thread.Execute())
{
PrintDbgThreadInfo(SchedThread.Thread, "waked.");
SchedThread.WaitSched.Set();
}
2018-02-19 20:37:13 +01:00
else
{
PrintDbgThreadInfo(SchedThread.Thread, "running.");
2018-02-19 20:37:13 +01:00
}
}
public void Resort(KThread Thread)
{
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
WaitingToRun.Resort(SchedThread);
}
}
private bool TryAddToCore(KThread Thread)
{
//First, try running it on Ideal Core.
int IdealCore = Thread.IdealCore;
if (IdealCore != -1 && CoreThreads[IdealCore] == null)
{
Thread.ActualCore = IdealCore;
CoreThreads[IdealCore] = Thread;
return true;
}
//If that fails, then try running on any core allowed by Core Mask.
int CoreMask = Thread.CoreMask;
for (int Core = 0; Core < CoreThreads.Length; Core++, CoreMask >>= 1)
{
if ((CoreMask & 1) != 0 && CoreThreads[Core] == null)
{
Thread.ActualCore = Core;
CoreThreads[Core] = Thread;
return true;
}
}
return false;
}
private void PrintDbgThreadInfo(KThread Thread, string Message)
{
2018-04-24 20:57:39 +02:00
Log.PrintDebug(LogClass.KernelScheduler, "(" +
"ThreadId = " + Thread.ThreadId + ", " +
"CoreMask = 0x" + Thread.CoreMask.ToString("x1") + ", " +
"ActualCore = " + Thread.ActualCore + ", " +
"IdealCore = " + Thread.IdealCore + ", " +
"ActualPriority = " + Thread.ActualPriority + ", " +
"WantedPriority = " + Thread.WantedPriority + ") " + Message);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool Disposing)
{
if (Disposing)
{
2018-02-19 20:37:13 +01:00
foreach (SchedulerThread SchedThread in AllThreads.Values)
{
2018-02-19 20:37:13 +01:00
SchedThread.Dispose();
}
}
}
}
}