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

344 lines
9.2 KiB
C#
Raw Normal View History

2018-04-24 18:57:39 +00:00
using Ryujinx.Core.Logging;
using System;
2018-02-19 19:37:13 +00:00
using System.Collections.Concurrent;
namespace Ryujinx.Core.OsHle.Handles
{
class KProcessScheduler : IDisposable
{
private ConcurrentDictionary<KThread, SchedulerThread> AllThreads;
private ThreadQueue WaitingToRun;
private int ActiveCores;
private object SchedLock;
2018-04-24 18:57:39 +00:00
private Logger Log;
public KProcessScheduler(Logger Log)
{
2018-04-24 18:57:39 +00:00
this.Log = Log;
AllThreads = new ConcurrentDictionary<KThread, SchedulerThread>();
WaitingToRun = new ThreadQueue();
SchedLock = new object();
}
public void StartThread(KThread Thread)
{
lock (SchedLock)
{
2018-02-19 19:37:13 +00:00
SchedulerThread SchedThread = new SchedulerThread(Thread);
if (!AllThreads.TryAdd(Thread, SchedThread))
{
return;
}
if (AddActiveCore(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 19:37:13 +00:00
lock (SchedLock)
{
2018-04-19 03:19:22 +00:00
if (AllThreads.TryRemove(Thread, out SchedulerThread SchedThread))
{
WaitingToRun.Remove(SchedThread);
SchedThread.Dispose();
2018-04-19 03:19:22 +00:00
}
SchedulerThread NewThread = WaitingToRun.Pop(Thread.ActualCore);
if (NewThread == null)
2018-02-19 19:37:13 +00:00
{
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
RemoveActiveCore(Thread.ActualCore);
2018-02-19 19:37:13 +00:00
return;
}
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)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
Suspend(Thread);
SchedThread.WaitSync.WaitOne();
TryResumingExecution(SchedThread);
}
public bool EnterWait(KThread Thread, int Timeout)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
Suspend(Thread);
bool Result = SchedThread.WaitSync.WaitOne(Timeout);
TryResumingExecution(SchedThread);
return Result;
}
public void WakeUp(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
SchedThread.WaitSync.Set();
}
public void Suspend(KThread Thread)
{
lock (SchedLock)
{
PrintDbgThreadInfo(Thread, "suspended.");
SchedulerThread SchedThread = WaitingToRun.Pop(Thread.ActualCore);
2018-02-19 19:37:13 +00:00
if (SchedThread != null)
{
2018-02-19 19:37:13 +00:00
RunThread(SchedThread);
}
2018-02-19 19:37:13 +00:00
else
{
Log.PrintDebug(LogClass.KernelScheduler, $"Nothing to run on core {Thread.ActualCore}!");
2018-02-19 19:37:13 +00:00
RemoveActiveCore(Thread.ActualCore);
}
}
}
public void Yield(KThread Thread)
{
PrintDbgThreadInfo(Thread, "yielded execution.");
2018-02-19 19:37:13 +00:00
if (IsActive(Thread))
{
lock (SchedLock)
{
SchedulerThread SchedThread = WaitingToRun.Pop(
Thread.ActualCore,
Thread.ActualPriority);
if (SchedThread == null)
{
PrintDbgThreadInfo(Thread, "resumed because theres nothing better to run.");
return;
}
if (SchedThread != null)
{
RunThread(SchedThread);
}
}
}
else
{
//Just stop running the thread if it's not active,
//and run whatever is waiting to run with the higuest priority.
Suspend(Thread);
}
2018-02-19 19:37:13 +00:00
Resume(Thread);
}
public void Resume(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
TryResumingExecution(SchedThread);
}
private void TryResumingExecution(SchedulerThread SchedThread)
{
KThread Thread = SchedThread.Thread;
2018-02-19 19:37:13 +00:00
PrintDbgThreadInfo(Thread, "trying to resume...");
SchedThread.WaitActivity.WaitOne();
lock (SchedLock)
{
if (AddActiveCore(Thread))
{
PrintDbgThreadInfo(Thread, "resuming execution...");
return;
}
WaitingToRun.Push(SchedThread);
PrintDbgThreadInfo(Thread, "entering wait state...");
2018-02-19 19:37:13 +00:00
}
SchedThread.WaitSched.WaitOne();
PrintDbgThreadInfo(Thread, "resuming execution...");
}
2018-02-19 19:37:13 +00:00
private void RunThread(SchedulerThread SchedThread)
{
2018-02-19 19:37:13 +00:00
if (!SchedThread.Thread.Thread.Execute())
{
PrintDbgThreadInfo(SchedThread.Thread, "waked.");
SchedThread.WaitSched.Set();
}
2018-02-19 19:37:13 +00:00
else
{
PrintDbgThreadInfo(SchedThread.Thread, "running.");
2018-02-19 19:37:13 +00:00
}
}
public void Resort(KThread Thread)
{
if (AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
WaitingToRun.Resort(SchedThread);
}
}
private bool IsActive(KThread Thread)
{
if (!AllThreads.TryGetValue(Thread, out SchedulerThread SchedThread))
{
throw new InvalidOperationException();
}
return SchedThread.IsActive;
}
private bool AddActiveCore(KThread Thread)
{
lock (SchedLock)
{
//First, try running it on Ideal Core.
int CoreMask = 1 << Thread.IdealCore;
if ((ActiveCores & CoreMask) == 0)
{
ActiveCores |= CoreMask;
Thread.ActualCore = Thread.IdealCore;
return true;
}
//If that fails, then try running on any core allowed by Core Mask.
CoreMask = Thread.CoreMask & ~ActiveCores;
if (CoreMask != 0)
{
CoreMask &= -CoreMask;
ActiveCores |= CoreMask;
for (int Bit = 0; Bit < 32; Bit++)
{
if (((CoreMask >> Bit) & 1) != 0)
{
Thread.ActualCore = Bit;
return true;
}
}
throw new InvalidOperationException();
}
return false;
}
}
private void RemoveActiveCore(int Core)
{
lock (SchedLock)
{
ActiveCores &= ~(1 << Core);
}
}
private void PrintDbgThreadInfo(KThread Thread, string Message)
{
2018-04-24 18:57:39 +00:00
Log.PrintDebug(LogClass.KernelScheduler, "(" +
"ThreadId = " + Thread.ThreadId + ", " +
"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 19:37:13 +00:00
foreach (SchedulerThread SchedThread in AllThreads.Values)
{
2018-02-19 19:37:13 +00:00
SchedThread.Dispose();
}
}
}
}
}